python / python/cpython

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

Aperta
#145,688 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-typing type-bug
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:

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.