You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
typing.get_type_hints() raises on invalid types only if they're wrapped in a ForwardRef #133959
>>> import typing
>>> def f(x: typing.ClassVar[int]): pass
...
>>> typing.get_type_hints(f)
{'x': typing.ClassVar[int]}
>>> def f(x: "typing.ClassVar[int]"): pass
...
>>> typing.get_type_hints(f)
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
typing.get_type_hints(f)
~~~~~~~~~~~~~~~~~~~~~^^^
File "/Users/jelle/py/cpython/Lib/typing.py", line 2390, in get_type_hints
hints[name] = _eval_type(value, globalns, localns, type_params, format=format, owner=obj)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jelle/py/cpython/Lib/typing.py", line 448, in _eval_typereturn evaluate_forward_ref(t, globals=globalns, locals=localns,
type_params=type_params, owner=owner,
_recursive_guard=recursive_guard, format=format)
File "/Users/jelle/py/cpython/Lib/typing.py", line 993, in evaluate_forward_ref
type_ = _type_check(
value,
...<2 lines>...
allow_special_forms=forward_ref.__forward_is_class__,
)
File "/Users/jelle/py/cpython/Lib/typing.py", line 204, in _type_checkraiseTypeError(f"{arg} is not valid as type argument")
TypeError: typing.ClassVar[int] is not valid as type argument
(On main but also reproduced on 3.13.)
This seems inconsistent and I don't think it's helpful for users. We should either raise in both cases or neither. I prefer to not raise in either case, because it's less disruptive for callers (they already have to deal with ClassVar etc. potentially appearing in annotations), and provides more information.
Activity
pythongh-133960: Improve typing.evaluate_forward_ref
gh-133960: Improve typing.evaluate_forward_ref (#133961)
pythongh-133960: Improve typing.evaluate_forward_ref (pythonGH-133961)
[3.14] gh-133960: Improve typing.evaluate_forward_ref (GH-133961) (#1…
pythongh-133960: Improve typing.evaluate_forward_ref (python#133961)
pythongh-133960: Improve typing.evaluate_forward_ref (python#133961)
pythongh-133960: Improve typing.evaluate_forward_ref (python#133961)