python / python/cpython

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

Aberta
#145,688 1 comentário 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib topic-typing type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece em Lib/typing.py, em _get_protocol_attrs, a função identificada no relatório, e execute a reprodução mínima fornecida com Python 3.14. O trabalho estará concluído quando uma classe definida pelo usuário chamada Protocol ou Generic expuser seus membros de protocolo declarados por meio de typing.get_protocol_members; verifique gh-145724 antes de começar, pois o issue vincula um pull request existente.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.