python / python/cpython

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

Open
#145,688 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/typing.py at _get_protocol_attrs, the function identified in the report, and run the provided minimal reproduction with Python 3.14. Done means a user-defined class named Protocol or Generic exposes its declared protocol members through typing.get_protocol_members; check gh-145724 before starting because the issue links an existing pull request.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.