Infer descriptor type
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
For the common case of a descriptor which behaves like a normal attribute, infer:
class X:
y: int = descriptor()
to:
class X:
y: descriptor[int] = descriptor()
if descriptor is a Generic with one parameter. Extra checks should also be done to make sure that the descriptor is properly typed to behave like an attribute of that type (__get__ and __set__ expect that type on an instance).
Pitch
Type annotations apply to object instances by default, so it's strange to see y: descriptor[int] as type annotation while x.y returns an int.
You could also see a normal attribute as an invisible descriptor whose behavior is to set __dict__[name] (and raise AttributeError on the class), so this could be a way to unify these things.
It's also related to:
https://github.com/python/mypy/issues/7724
https://github.com/python/mypy/issues/5481
I implemented it partially as a plugin but only for my custom descriptors.
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.
Research direction
The issue does not name implementation files, tests, or an entry point, so first review the descriptor-related discussions in issues #7724 and #5481 and the existing plugin behavior described here. Done would require a decided approach for inferring the generic descriptor type and validating its get and set signatures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100