Fix ClassVar as string fails when getting type hints (GH-6824)

This commit is contained in:
Nina Zakharenko 2018-05-16 12:27:03 -04:00 committed by Łukasz Langa
parent 8b94b41ab7
commit 2d2d3b170b
3 changed files with 38 additions and 6 deletions

View file

@ -1599,6 +1599,30 @@ class ForwardRefTests(BaseTestCase):
# verify that @no_type_check never affects bases
self.assertEqual(get_type_hints(C.meth), {'x': int})
def test_no_type_check_forward_ref_as_string(self):
class C:
foo: typing.ClassVar[int] = 7
class D:
foo: ClassVar[int] = 7
class E:
foo: 'typing.ClassVar[int]' = 7
class F:
foo: 'ClassVar[int]' = 7
expected_result = {'foo': typing.ClassVar[int]}
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):
@no_type_check_decorator