get_type_hints change in behavior for Annotated with from future import annotations 3.9+

Abierto
#95,123 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

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

Línea de trabajo

Reproduce los ejemplos de Annotated y forward-reference del issue en las versiones de Python indicadas y, después, inspecciona la ruta _eval_type mencionada en el informe. Compara el comportamiento con y sin Annotated y determina el resultado previsto de get_type_hints; se considera terminado cuando el comportamiento sea coherente o cuando su semántica admitida se haga explícita mediante cobertura de regresión.

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

Descripción

stdlib topic-typing type-bug

Bug report

from __future__ import annotations

from typing import Sequence, Union
from typing_extensions import Annotated, get_type_hints

Config = Union[str, Sequence["Config"]]
class Foo:
 x: Annotated[Config, "hi"]

print(Config)
print(get_type_hints(Foo)["x"])

The output prints are different in 3.8.13 vs 3.9.13/3.10.4/3.11.0b1. In 3.8.7 the output is,

typing.Union[str, typing.Sequence[ForwardRef('Config')]]
typing.Union[str, typing.Sequence[ForwardRef('Config')]]

In 3.9.13/3.10.4/3.11.0b1 the output is,

typing.Union[str, typing.Sequence[ForwardRef('Config')]]
typing.Union[str, typing.Sequence[typing.Union[str, typing.Sequence[ForwardRef('Config')]]]]

get_type_hints is doing 1 type expansion on forward reference. There's second inconsistency here which is that Annotated changes output in 3.9+. I'd expect get_type_hints(Foo, include_extras=False) to have same type with or without Annotated. If I drop Annotated and instead test

Config = Union[str, Sequence["Config"]]
class Foo:
 x: Config

print(Config)
print(get_type_hints(Foo)["x"])

the 1 type expansion goes away and output is same as 3.8.7 output.

The from __future__ import annotations is important or you can also manually quote and do x: "Config".

The exact behavior is niche and would be understandable if this was documented as undefined implementation detail similar to how cross module aliases are awkward to work with at runtime.

Expansion affected runtime internal serialization tool I was using and I ended up needing to do some workarounds to fix tests. I haven't tried testing if pydantic/similar libraries handle this case well or not. Expansion also affects documentation produced by a tool like sphinx-autodoc-typehints.

Your environment
I'm on mac, although hope this isn't platform specific. I also tried testing from typing import Annotated instead of typing_extensions for 3.9+ and it didn't resolve inconsistency. My first guess is _eval_type is where this behavior change comes from. Newest version I tested up to was 3.11.0b1.

Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Merge medio
1 d 9 h
PR fusionados (30 d)
558

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.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.