Skip to content

bpo-46539: Pass status of special typeforms to forward references #30926

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

Merged
merged 6 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2870,6 +2870,13 @@ def foo(a: 'Callable[..., T]'):
self.assertEqual(get_type_hints(foo, globals(), locals()),
{'a': Callable[..., T]})

def test_special_forms_forward(self):

class C:
a: Annotated['ClassVar[int]', (3, 5)] = 4

self.assertEqual(get_type_hints(C, globals())['a'], ClassVar[int])

def test_syntax_error(self):

with self.assertRaises(SyntaxError):
Expand Down
12 changes: 6 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ def _idfunc(_, x):
# legitimate imports of those modules.


def _type_convert(arg, module=None):
def _type_convert(arg, module=None, *, allow_special_forms=False):
"""For converting None to type(None), and strings to ForwardRef."""
if arg is None:
return type(None)
if isinstance(arg, str):
return ForwardRef(arg, module=module)
return ForwardRef(arg, module=module, is_class=allow_special_forms)
return arg


Expand All @@ -169,7 +169,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
if is_argument:
invalid_generic_forms += (Final,)

arg = _type_convert(arg, module=module)
arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms)
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")
Expand Down Expand Up @@ -661,7 +661,7 @@ class ForwardRef(_Final, _root=True):

__slots__ = ('__forward_arg__', '__forward_code__',
'__forward_evaluated__', '__forward_value__',
'__forward_is_argument__', '__forward_is_class__',
'__forward_is_argument__', '__forward_allow_special_forms__',
'__forward_module__')

def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
Expand All @@ -676,7 +676,7 @@ def __init__(self, arg, is_argument=True, module=None, *, is_class=False):
self.__forward_evaluated__ = False
self.__forward_value__ = None
self.__forward_is_argument__ = is_argument
self.__forward_is_class__ = is_class
self.__forward_allow_special_forms__ = is_class
self.__forward_module__ = module

def _evaluate(self, globalns, localns, recursive_guard):
Expand All @@ -697,7 +697,7 @@ def _evaluate(self, globalns, localns, recursive_guard):
eval(self.__forward_code__, globalns, localns),
"Forward references must evaluate to types.",
is_argument=self.__forward_is_argument__,
allow_special_forms=self.__forward_is_class__,
allow_special_forms=self.__forward_allow_special_forms__,
)
self.__forward_value__ = _eval_type(
type_, globalns, localns, recursive_guard | {self.__forward_arg__}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typing: Pass on status of special forms to forward references. Patch by Gregory Beauregard.