Infer attributes from __new__
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Start by reading mypy's semantic-analysis handling of new and assignments to the object created by object.new. Trace how super(...) calls are handled, then verify that attributes assigned through the created object are recognized on instances such as x.foo, including in unannotated methods.
Written by the indexing model from the issue text.
Description
In this example (from #982) we define an attribute via assignment in __new__:
class C(object):
def __new__(cls, foo=None):
obj = object.__new__(cls)
obj.foo = foo
return obj
x = C(foo=12)
print(x.foo)
Currently mypy doesn't recognize attribute foo and complains about x.foo. To implement this, we could do these things:
- Infer the fact that
objis something similar toselfbecause of the way is created viaobject.__new__. This should happen during semantic analysis so that this will work in unannotated method as well. - Infer attributes from assignments via
objbecause it is classified as similar toself. - Also recognize
object.__new__calls viasuper(...).
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·