Property return type not inferred when using `field_name = property(func)`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When using the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)
To Reproduce
from __future__ import annotations
from typing import Any
def func1(self: Any) -> int:
return 3
def func2(self: object) -> int:
return 3
def func3(self: Test) -> int:
return 3
class Test:
def func4(self) -> int:
return 3
test1 = property(func1)
test2 = property(func2)
test3 = property(func3)
test4 = property(func4)
reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)
# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file
Expected Behavior
I'd expect mypy to understand the property return types.
It also seems that there is no way around this.
The following gives "Incompatible types in assignment":
class Test:
test4: int = property(func4)
and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)
Actual Behavior
We're getting Any instead.
Your Environment
- Mypy version used: 0.981
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.9.13
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 report names no repository file or test. Start by running the supplied Python reproduction with mypy 0.981 and inspect property/descriptor handling and the typeshed property definition; done means reveal_type reports each property's int return type without an incompatible assignment.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100