bpo-41370: Evaluate strings as forward refs in PEP 585 generics by NiklasRosenstein · Pull Request #30900 · python/cpython

This Pull Requests suggests a change to typing._eval_type() that considers strings in PEP 585 generics (ie. instances of types.GenericAlias) as forward references. This is necessary because ForwardRef is not and likely will not be implemented in C, and thus strings used as forward references in PEP 585 are not currently evaluated by typing.get_type_hints().

A test case that uses assertIs() currently fails because in the current state _eval_type() creates a copy of the same generic alias with transformed arguments, thus for any PEP 585 generic alias x, the comparison x is typing.get_type_hints(func)['X'] will evaluate to False, assuming func has a field/argument annotation X: x. I think that this can be worked around, and happy to propose an updated implementation.

I created this PR as a proof of concept until a there is consent that in spirit this change to get_type_hints() is something that should happen. So for now I'll keep it as it is.

======================================================================
FAIL: test_get_type_hints_annotated (__main__.GetTypeHintTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/niklas/git/cpython/Lib/test/test_typing.py", line 3298, in test_get_type_hints_annotated
    self.assertIs(
    ^^^^^^^^^^^^^^
AssertionError: tuple[typing.Annotated[~T, (1, 0)], ...] is not tuple[typing.Annotated[~T, (1, 0)], ...]