python / python/typing

[spec] Formalize excluded Protocol members

オープン
#1,833 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic: documentation
主要言語
Python
スター
1.8k
フォーク
302
平均マージ
23時間
マージ済み PR(30日)
8

説明

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__.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/typing.py の EXCLUDED_ATTRIBUTES 定義から始め、PEP 544、typing specification、およびリンクされている pyright と mypy の例と比較します。どの Protocol メンバーに正式な仕様が必要かを判断し、異なる checker の結果に明確な解決が得られるよう、意図された動作を文書化します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。