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

Open
#95,123 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Reproduce the issue's Annotated and forward-reference examples across the listed Python versions, then inspect the _eval_type path mentioned in the report. Compare behavior with and without Annotated and determine the intended get_type_hints result; done means the behavior is consistent or its supported semantics are made explicit with regression coverage.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.