`isinstance` on multiple-inheritance class erases type hints
Offen
Dieses Issue hat noch niemand übernommen.
3.12
3.13
3.14
stdlib
topic-typing
type-bug
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
Steps to reproduce:
- Create a runtime checkable protocol
- Create one base class with type hints
- Create a second base class that satisfies the protocol
- Create a derived class that inherits from both the base classes
get_type_hintson the derived class and see they are correct- check the derived class
isinstanceof the protocol get_type_hintson the derived class and see that they are a blank dictionary
from abc import abstractmethod
from typing import Protocol, get_type_hints, runtime_checkable
@runtime_checkable
class Hello(Protocol):
@abstractmethod
def hello(self) -> str: ...
class Base:
a: str
class Base2:
def hello(self) -> str:
return "hello"
class Thing(Base, Base2): ...
def test_hints():
t = Thing()
assert get_type_hints(t) == {"a": str}
assert isinstance(t, Hello)
assert get_type_hints(t) == {"a": str}
CPython versions tested on:
3.11, 3.13
Operating systems tested on:
Linux
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, den bereitgestellten Reproducer unter CPython auszuführen und get_type_hints vor und nach der isinstance-Prüfung zu vergleichen. Verfolge die an dieser Prüfung beteiligten get_type_hints- und runtime-checkable-Protocol-Pfade. Als erledigt gilt es, wenn die abgeleitete Thing-Instanz nach isinstance(t, Hello) weiterhin {"a": str} meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100