-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-114053: Fix bad interaction of PEP-695, PEP-563 and get_type_hints
#118009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3024,7 +3024,9 @@ Introspection helpers | |
|
|
||
| This is often the same as ``obj.__annotations__``. In addition, | ||
| forward references encoded as string literals are handled by evaluating | ||
| them in ``globals`` and ``locals`` namespaces. For a class ``C``, return | ||
| them in ``globals``, ``locals`` and (where applicable) | ||
| :ref:`type parameter <annotation-scopes>` namespaces. | ||
| For a class ``C``, return | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't usually add |
||
| a dictionary constructed by merging all the ``__annotations__`` along | ||
| ``C.__mro__`` in reverse order. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| class A[T, *Ts, **P]: | ||
| x: T | ||
| y: Ts | ||
| z: P | ||
|
|
||
|
|
||
| class B[T, *Ts, **P]: | ||
| T = int | ||
| Ts = str | ||
| P = bytes | ||
| x: T | ||
| y: Ts | ||
| z: P | ||
|
|
||
|
|
||
| def generic_function[T, *Ts, **P]( | ||
| x: T, *y: *Ts, z: P.args, zz: P.kwargs | ||
| ) -> None: ... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,15 +399,16 @@ def inner(*args, **kwds): | |
|
|
||
| return decorator | ||
|
|
||
| def _eval_type(t, globalns, localns, recursive_guard=frozenset()): | ||
|
|
||
| def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset()): | ||
| """Evaluate all forward references in the given type t. | ||
|
|
||
| For use of globalns and localns see the docstring for get_type_hints(). | ||
| recursive_guard is used to prevent infinite recursion with a recursive | ||
| ForwardRef. | ||
| """ | ||
| if isinstance(t, ForwardRef): | ||
| return t._evaluate(globalns, localns, recursive_guard) | ||
| return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard) | ||
| if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)): | ||
| if isinstance(t, GenericAlias): | ||
| args = tuple( | ||
|
|
@@ -421,7 +422,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()): | |
| t = t.__origin__[args] | ||
| if is_unpacked: | ||
| t = Unpack[t] | ||
| ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__) | ||
|
|
||
| ev_args = tuple( | ||
| _eval_type( | ||
| a, globalns, localns, type_params, recursive_guard=recursive_guard | ||
| ) | ||
| for a in t.__args__ | ||
| ) | ||
| if ev_args == t.__args__: | ||
| return t | ||
| if isinstance(t, GenericAlias): | ||
|
|
@@ -974,7 +981,7 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False): | |
| self.__forward_is_class__ = is_class | ||
| self.__forward_module__ = module | ||
|
|
||
| def _evaluate(self, globalns, localns, recursive_guard): | ||
| def _evaluate(self, globalns, localns, type_params, *, recursive_guard): | ||
| if self.__forward_arg__ in recursive_guard: | ||
| return self | ||
| if not self.__forward_evaluated__ or localns is not globalns: | ||
|
|
@@ -989,13 +996,21 @@ def _evaluate(self, globalns, localns, recursive_guard): | |
| sys.modules.get(self.__forward_module__, None), '__dict__', globalns | ||
| ) | ||
| type_ = _type_check( | ||
| eval(self.__forward_code__, globalns, localns), | ||
| eval( | ||
| self.__forward_code__, | ||
| globalns, | ||
| {param.__name__: param for param in type_params} | localns | ||
|
||
| ), | ||
| "Forward references must evaluate to types.", | ||
| is_argument=self.__forward_is_argument__, | ||
| allow_special_forms=self.__forward_is_class__, | ||
| ) | ||
| self.__forward_value__ = _eval_type( | ||
| type_, globalns, localns, recursive_guard | {self.__forward_arg__} | ||
| type_, | ||
| globalns, | ||
| localns, | ||
| type_params, | ||
| recursive_guard=(recursive_guard | {self.__forward_arg__}), | ||
| ) | ||
| self.__forward_evaluated__ = True | ||
| return self.__forward_value__ | ||
|
|
@@ -2334,7 +2349,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False): | |
| value = type(None) | ||
| if isinstance(value, str): | ||
| value = ForwardRef(value, is_argument=False, is_class=True) | ||
| value = _eval_type(value, base_globals, base_locals) | ||
| value = _eval_type(value, base_globals, base_locals, base.__type_params__) | ||
| hints[name] = value | ||
| return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} | ||
|
|
||
|
|
@@ -2360,6 +2375,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False): | |
| raise TypeError('{!r} is not a module, class, method, ' | ||
| 'or function.'.format(obj)) | ||
| hints = dict(hints) | ||
| type_params = getattr(obj, "__type_params__", ()) | ||
| for name, value in hints.items(): | ||
| if value is None: | ||
| value = type(None) | ||
|
|
@@ -2371,7 +2387,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False): | |
| is_argument=not isinstance(obj, types.ModuleType), | ||
| is_class=False, | ||
| ) | ||
| hints[name] = _eval_type(value, globalns, localns) | ||
| hints[name] = _eval_type(value, globalns, localns, type_params) | ||
| return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix erroneous :exc:`NameError` when calling :func:`typing.get_type_hints` on | ||
| a class that made use of :pep:`695` type parameters in a module that had | ||
| ``from __future__ import annotations`` at the top of the file. Patch by Alex | ||
| Waygood. |
Uh oh!
There was an error while loading. Please reload this page.