python / python/cpython

Protocol named "Protocol" or "Generic" has no members.

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

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

stdlib topic-typing type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

If a protocol is named Protocol or Generic then it will ignore any defined members.

when you get lazy with naming in your unit tests this makes for quite a head scratcher...

Minimal Reproduction
import typing


class Protocol(typing.Protocol):
    a: int


members = typing.get_protocol_members(Protocol)

assert members == {"a"}, f"Expected members to be {{'a'}}, got {members}"
Output:
Traceback (most recent call last):
  File ".../test_protocol.py", line 10, in <module>
    assert members == {"a"}, f"Expected members to be {{'a'}}, got {members}"
           ^^^^^^^^^^^^^^^^
AssertionError: Expected members to be {'a'}, got frozenset()
Expected Output:

Nothing, script should succeed without error.

Investigation

The problem appears to stem from the _get_protocol_attrs function used by _ProtocolMeta which is defined as follows:

https://github.com/python/cpython/blob/171133aa84cd2fa8738bdbb0c76435645810e8d3/Lib/typing.py#L1879-L1899

The if statement on line 1887 is too loose in what it matches.

Possibile Fix

The check for base needs to be more specific, either using the instance itself:

if base in {Protocol, Generic}:
    continue

or checking the module too, in order to ensure its the correct class being excluded:

if base.__name__ in {'Protocol', 'Generic'} and base.__module__ == 'typing':
    continue
CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-145724

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

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

はじめの一歩

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

調査の方向性

レポートで特定されている関数 _get_protocol_attrs のある Lib/typing.py から始め、提供された最小再現を Python 3.14 で実行します。ユーザー定義クラス Protocol または Generic が、宣言されたプロトコルメンバーを typing.get_protocol_members を通じて公開すれば完了です。開始前に gh-145724 を確認してください。この issue には既存の pull request へのリンクがあります。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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