Type confusion with protocol attributes when explicitly subclassing protocol
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 type hinting an attribute on a protocol, and then explicitly subclassing the protocol in a class and performing assignment on the attribute, mypy appears to lose the type information on the attribute. In one case, mypy determines the type is whatever can be inferred from the assignment, and in another case, mypy cannot determine the type.
I've looked at Python's documentation for explicitly subclassing a Protocol (docs) as well as the documentation in mypy for Protocols (docs) and I believe this should work.
To Reproduce
Case 1: Incompatible types in assignment
from typing import Protocol
class InventoryItemProtocol(Protocol):
val: int | None
class InventoryItemMixin(InventoryItemProtocol):
def set(self) -> None:
self.val = 1
class InventoryItem(InventoryItemMixin):
def __init__(self) -> None:
self.val: int | None = None
Case 2: Cannot determine type
from typing import Protocol
class ValProtocol(Protocol):
val: int | None
class Runner(ValProtocol):
def run(self) -> None:
if not self.val:
raise ValueError
self.val = 1
Expected Behavior
Both programs pass type checks.
Actual Behavior
Case 1
sandbox_protocol1.py:15: error: Incompatible types in assignment (expression has type "int | None", base class "InventoryItemMixin" defined the type as "int") [assignment]
Found 1 error in 1 file (checked 1 source file)
Case 2
sandbox_protocol2.py:10: error: Cannot determine type of "val" [has-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
mypy --version
mypy 1.17.1 (compiled: yes)
python --version
Python 3.12.2
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 damit, die beiden Protocol-Beispiele aus dem Issue mit mypy 1.17.1 auszuführen und die Fehler mit dem erwarteten Verhalten zu vergleichen. Verfolge, wie explizit unterklassifizierte Protocol-Attribute inferiert und validiert werden; abgeschlossen ist die Aufgabe, wenn beide Beispiele die Typprüfung bestehen, ohne den deklarierten Attributtyp zu verlieren.
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
- 45/100