mypy incorrectly flags a type-var error when a descriptor overrides a protocol property in a different file.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
To Reproduce
- Create an
app.pyfile with this inside:
from __future__ import annotations
from typing import Protocol
# PROTOCOL
class ServiceProto(Protocol):
@property
def value(self) -> int: ...
# DESCRIPTOR
class Descriptor:
def __get__(self, instance: ServiceImpl, owner: type[ServiceImpl]) -> int:
return 5
# IMPLEMENTATION
class ServiceImpl:
value = Descriptor()
class App[ServiceT: ServiceProto]:
def __init__(self, service: ServiceT) -> None:
self.service = service
- create a second file (important) with this inside (mine is named
run.py):
from __future__ import annotations
from .app import App, ServiceImpl
def bootstrap() -> App[ServiceImpl]:
return App(ServiceImpl())
Expected Behavior
No errors
Actual Behavior
mypy src/
run.py:5: error: Type argument "ServiceImpl" of "App" must be a subtype of "ServiceProto" [type-var]
run.py:6: error: Value of type variable "ServiceT" of "App" cannot be "ServiceImpl" [type-var]
Found 2 errors in 1 file (checked 3 source files)
Your Environment
- Mypy version used: mypy 1.18.2 (compiled: yes)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: Python 3.13.7 (via uv)
Weird Behavior
Merge both files, remove redundant imports. No more errors.
PS: Feel free to edit the title. This was a hard one to describe in one sentence without knowing the root cause.
PPS: This is a toy example with integers to illustrate the bug. My real use case is more complex: the descriptor is a lazy instantiator that works like cached_property (the descriptor is given a class object on instantiation, it instantiates and caches an instance of that class on the owning service instance, then serves the cached instance on further accesses).
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
Reproduziere den Bericht mit den getrennten Dateien app.py und run.py und führe anschließend mypy src/ mit Python 3.13.7 und mypy 1.18.2 aus. Verfolge, wie der descriptor-gestützte ServiceImpl dateiübergreifend gegen ServiceProto geprüft wird und wie App[ServiceImpl] die type-var errors erzeugt. Als erledigt gilt die Aufgabe, wenn das Zwei-Dateien-Beispiel keine Fehler meldet und das Verhalten weiterhin durch einen geeigneten Regressionstest abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100