Skip to content

gh-89547: Add support for nesting special forms like Final #116092

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

Closed
Show file tree
Hide file tree
Changes from all 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
38 changes: 26 additions & 12 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4634,8 +4634,6 @@ def test_fail_with_bare_union(self):
List[Union]
with self.assertRaises(TypeError):
Tuple[Optional]
with self.assertRaises(TypeError):
ClassVar[ClassVar[int]]
with self.assertRaises(TypeError):
List[ClassVar[int]]

Expand Down Expand Up @@ -5989,16 +5987,6 @@ class F:
for clazz in [C, D, E, F]:
self.assertEqual(get_type_hints(clazz), expected_result)

def test_nested_classvar_fails_forward_ref_check(self):
class E:
foo: 'typing.ClassVar[typing.ClassVar[int]]' = 7
class F:
foo: ClassVar['ClassVar[int]'] = 7

for clazz in [E, F]:
with self.assertRaises(TypeError):
get_type_hints(clazz)

def test_meta_no_type_check(self):
depr_msg = (
"'typing.no_type_check_decorator' is deprecated "
Expand Down Expand Up @@ -8611,6 +8599,32 @@ class C:
self.assertEqual(get_type_hints(C, globals())['classvar'], ClassVar[int])
self.assertEqual(get_type_hints(C, globals())['const'], Final[int])

def test_special_forms_nesting(self):
class CF:
x: ClassVar[Final[int]]

class FC:
x: Final[ClassVar[int]]

class ACF:
x: Annotated[ClassVar[Final[int]], "a decoration"]

class CAF:
x: ClassVar[Annotated[Final[int], "a decoration"]]

class AFC:
x: Annotated[Final[ClassVar[int]], "a decoration"]

class FAC:
x: Final[Annotated[ClassVar[int], "a decoration"]]

self.assertEqual(get_type_hints(CF, globals())['x'], ClassVar[Final[int]])
self.assertEqual(get_type_hints(FC, globals())['x'], Final[ClassVar[int]])
self.assertEqual(get_type_hints(ACF, globals())['x'], ClassVar[Final[int]])
self.assertEqual(get_type_hints(CAF, globals())['x'], ClassVar[Final[int]])
self.assertEqual(get_type_hints(AFC, globals())['x'], Final[ClassVar[int]])
self.assertEqual(get_type_hints(FAC, globals())['x'], Final[ClassVar[int]])

def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError, "Cannot subclass .*Annotated"):
class C(Annotated):
Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class Starship:
Note that ClassVar is not a class itself, and should not
be used with isinstance() or issubclass().
"""
item = _type_check(parameters, f'{self} accepts only single type.')
item = _type_check(parameters, f'{self} accepts only single type.', allow_special_forms=True)
return _GenericAlias(self, (item,))

@_SpecialForm
Expand All @@ -661,7 +661,7 @@ class FastConnector(Connection):

There is no runtime checking of these properties.
"""
item = _type_check(parameters, f'{self} accepts only single type.')
item = _type_check(parameters, f'{self} accepts only single type.', allow_special_forms=True)
return _GenericAlias(self, (item,))

@_SpecialForm
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for nested typing special forms like Final[ClassVar[int]].