mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-89547: Support for nesting special forms like Final (#116096)
This commit is contained in:
parent
4fa95c6ec3
commit
d308d33e09
4 changed files with 32 additions and 14 deletions
|
@ -4655,8 +4655,6 @@ class GenericTests(BaseTestCase):
|
|||
List[Union]
|
||||
with self.assertRaises(TypeError):
|
||||
Tuple[Optional]
|
||||
with self.assertRaises(TypeError):
|
||||
ClassVar[ClassVar[int]]
|
||||
with self.assertRaises(TypeError):
|
||||
List[ClassVar[int]]
|
||||
|
||||
|
@ -6014,16 +6012,6 @@ class ForwardRefTests(BaseTestCase):
|
|||
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 "
|
||||
|
@ -8716,6 +8704,34 @@ class AnnotatedTests(BaseTestCase):
|
|||
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):
|
||||
# These are uncommon types and are to ensure runtime
|
||||
# is lax on validation. See gh-89547 for more context.
|
||||
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue