Mypy is expecting `type[Never]` in context when it shouldn't
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
I have written (well, copied, from here) a class classproperty to handle implementing class properties. It works perfectly fine, however when I try and type it as follows (which I think is valid), mypy raises the below error
class classproperty[T, C]:
def __init__(self, func: Callable[[type[C]], T]):
self.func = func
def __get__(self, _obj: C, owner: type[C]) -> T:
return self.func(owner)
class Test:
@classproperty
def my_property(cls):
return 1
error: Argument 1 to "classproperty" has incompatible type "Callable[[Test], Any]"; expected "Callable[[type[Never]], Any]" [arg-type]
I don't think this is correct, based on my understanding of descriptors and __get__. I asked a question on stackoverflow about this and it was suggested that this is caused by incorrect assumptions about how __get__ is called
To Reproduce
Gist link here
Expected Behavior
Mypy should pass
Actual Behavior
error: Argument 1 to "classproperty" has incompatible type "Callable[[Test], Any]"; expected "Callable[[type[Never]], Any]" [arg-type]
Your Environment
- Python 3.13.1
mypy==1.15.0
No further config
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
Beginne mit dem verlinkten mypy-play-Gist und reduziere das classproperty-Beispiel unter Python 3.13.1 und mypy 1.15 auf einen fokussierten Reproducer. Lies die Behandlung von Deskriptoren und generischer Inferenz, die an der gemeldeten Erwartung von type[Never] beteiligt ist. Als erledigt gilt die Aufgabe, wenn mypy das Beispiel akzeptiert und ein Regressionstest das erwartete Verhalten abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100