python / python/mypy

Consider adding a plugin for `.__dict__`

Open
#15,964 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

In runtime we know that __dict__ on any object contains the same fields as instance itself.
So, we can predict what is going to be inside this __dict__:

>>> class A:
...    def __init__(self, x: int, y: str) -> None:
...        self.x = x
...        self.y = y
... 
>>> A(1, 'a').__dict__
{'x': 1, 'y': 'a'}

Pitch

I think it might be very useful to infer A(1, 'a').__dict__ as TypedDict({'x': int, 'y': str}).

However:

  1. Performance might be an issue
  2. There are cases where this is not possible due to typing and runtime resons (__getattr__, fallback_to_any, etc)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining how mypy plugins and TypedDict inference are currently handled, then assess whether an object's dict can be inferred from its instance fields. Account for the stated performance concerns and exceptions such as getattr and fallback_to_any; done would require a defined design and supporting behavior for the feasible cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.