python / python/mypy

Infer descriptor type

Open
#10,246 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-descriptors
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.