Protocol named "Protocol" or "Generic" has no members.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in Lib/typing.py, in _get_protocol_attrs, la funzione identificata nel report, ed esegui la riproduzione minima fornita con Python 3.14. Il lavoro è completato quando una classe definita dall’utente denominata Protocol o Generic espone i membri del protocollo dichiarati tramite typing.get_protocol_members; controlla gh-145724 prima di iniziare perché l’issue collega una pull request esistente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100