python / python/cpython

Incorrect tests for get_type_hints()

Abierto
#137,228 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib topic-typing type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con test_get_type_hints_generic y test_get_type_hints_classes en Lib/test/test_typing.py y, después, inspecciona las definiciones referenciadas en _typed_dict_helper. Ejecuta las pruebas de typing específicas y revisa el PR enlazado gh-137230 antes de continuar. Se considera terminado cuando las aserciones y las expectativas de resolución de nombres reflejen el comportamiento previsto de get_type_hints() y las pruebas relevantes pasen.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
testing-qa
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.