[spec] Formalize excluded Protocol members
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
At runtime, the following members are currently excluded by Protocol: (source)
_TYPING_INTERNALS = frozenset({
'__parameters__', '__orig_bases__', '__orig_class__',
'_is_protocol', '_is_runtime_protocol', '__protocol_attrs__',
'__non_callable_proto_members__', '__type_params__',
})
_SPECIAL_NAMES = frozenset({
'__abstractmethods__', '__annotations__', '__dict__', '__doc__',
'__init__', '__module__', '__new__', '__slots__',
'__subclasshook__', '__weakref__', '__class_getitem__',
'__match_args__', '__static_attributes__', '__firstlineno__',
'__annotate__',
})
# These special attributes will be not collected as protocol members.
EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS | _SPECIAL_NAMES | {'_MutableMapping__marker'}
However, neither PEP 544 nor the typing spec formalize this list, which leads to diverging behavior between different type checkers:
Code sample in pyright playground, mypy playground
from typing import TypeIs, Protocol
class MyProtocol(Protocol):
@classmethod
def __subclasshook__(cls, other: type, /) -> TypeIs[type["MyProtocol"]]:
...
x: MyProtocol = int(1) # mypy: ✅ pyright: ❌
Code sample in pyright playground, mypy playground
from typing import Protocol
class MyProtocol(Protocol):
__abstractmethods__: frozenset[str]
x: MyProtocol = int(1) # mypy: ✅ pyright: ❌
A real-world example where this matters is for instance a Protocol for NamedTuple Instances, that checks whether types.get_original_bases(cls) includes typing.NamedTuple inside __subclasshook__.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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 der EXCLUDED_ATTRIBUTES-Definition in Lib/typing.py und vergleiche sie mit PEP 544, der typing specification sowie den verlinkten pyright- und mypy-Beispielen. Bestimme, welche Protocol-Mitglieder eine formale Spezifikation erfordern, und dokumentiere das beabsichtigte Verhalten, damit die unterschiedlichen Ergebnisse der Checker eindeutig aufgelöst werden können.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100