python / python/cpython

Incorrect tests for get_type_hints()

Aberta
#137,228 0 comentários 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:

A few tests for typing.get_type_hints() test behavior that I feel is incorrect. Fixing it may be more trouble than it's worth, but I thought I'd record these.

  1. test_get_type_hints_generic

https://github.com/python/cpython/blob/5236b0281b91a874b14cf15f3fdef9b7beffb22f/Lib/test/test_typing.py#L8765

This tests that the a key for the TypedDict is T | None, where T is the TypeVar of that name defined in test_typing.py. However, the relevant TypedDict field is defined in a separate module _typed_dict_helper that has its own T TypeVar. get_type_hints() should pick up the T from the helper module, not the one from test_typing.

This also affects another assertion in the same function.

  1. test_get_type_hints_classes

https://github.com/python/cpython/blob/5236b0281b91a874b14cf15f3fdef9b7beffb22f/Lib/test/test_typing.py#L6636

This calls get_type_hints() on a class defined like this:

class B(Generic[T]):
    class A(Generic[T]):
        pass

    my_inner_a1: 'B.A'
    my_inner_a2: A
    my_outer_a: 'A'  # unless somebody calls get_type_hints with localns=B.__dict__

It checks that the unquoted A refers to the nested class and the quoted A refers to a different A class in the outer scope. But the fact that the name was quoted shouldn't influence scoping logic; it should resolve to the same name whether or not it's quoted.


I have a change that fixes both of these tests, but I'm not convinced we can afford to change this.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-137230

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 com test_get_type_hints_generic e test_get_type_hints_classes em Lib/test/test_typing.py e, em seguida, inspecione as definições referenciadas em _typed_dict_helper. Execute os testes de typing focados e revise o PR vinculado gh-137230 antes de prosseguir. Considera-se concluído quando as asserções e as expectativas de resolução de nomes refletirem o comportamento pretendido de get_type_hints() e os testes relevantes passarem.

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

Avaliação

Stack de tecnologia
python
Domínio
testing-qa
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
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.