python / python/typeshed

"class property" should be Generic[T]

Open
#4,731 7 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: improvement
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

Currently property.__get__ returns Any, which loose information:

https://github.com/python/typeshed/blob/c58a93b928b07a5123fa8ceebe6087fa262fe238/stdlib/3/builtins.pyi#L877

This would be more correct for property.__get__ to be generic and return T. Similar to cached_property:

https://github.com/python/typeshed/blob/1dd1b701c97e4bff9b8e05fb58ce8d3dd27aa213/stdlib/3/functools.pyi#L113

class A:

  @cached_property
  def f(self) -> int:  # f is `cached_property[int]`, f.__get__() -> int
    ...

  @property
  def g(self) -> int:  # f is `property[Any]`, g.__get__() -> Any
    ...

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 with stdlib/3/builtins.pyi at the property definition referenced in the issue, then compare its get typing with stdlib/3/functools.pyi and cached_property. Confirm how the generic type is inferred from a property's getter annotation. Done means property.get preserves and returns that type instead of Any, with the existing stub behavior otherwise unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.