Property return type not inferred when using `field_name = property(func)`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Der Bericht nennt keine Repository-Datei und keinen Test. Beginne damit, die bereitgestellte Python-Reproduktion mit mypy 0.981 auszuführen, und untersuche die Behandlung von property/descriptor sowie die property-Definition in typeshed; als erledigt gilt die Aufgabe, wenn reveal_type den int-Rückgabetyp jeder property meldet, ohne eine inkompatible Zuweisung.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100