[spec] Formalize excluded Protocol members
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 35/100
- Issue 類型
- 文件
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
從 Lib/typing.py 中的 EXCLUDED_ATTRIBUTES 定義開始,將其與 PEP 544、typing specification 以及連結的 pyright 和 mypy 範例進行比較。確定哪些 Protocol 成員需要正式規範,並記錄預期行為,以便對 checker 產生的不同結果給出明確的解決方案。
由索引模型根據 Issue 內容生成。
描述
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__.
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 38 分鐘
- 30 天內合併 PR
- 6
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
-
Update reference/generics.html with PEP695 type parameter syntax可能已有人在做 @BHUVANSH855 於 182 天前認領。 未關閉topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
-
Finals and Literals: Spec contradiction可能已有人在做 @srittau 於 35 天前認領。 未關閉topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
modelcontextprotocol/python-sdk#3648 ·
維護者通常 1 天內回覆
-
docs good first issue
難度 2/5 1-3 小時 新手友好度 78/100
VenetoStato/giorgio#6 ·
-
難度 1/5 1 小時以內 新手友好度 70/100
EclipseFdn/open-vsx.org#13831 ·
維護者通常 1 天內回覆
-
feature request
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 2 天內回覆