Protocol named "Protocol" or "Generic" has no members.
Nobody has claimed this yet.
- 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:
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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