diff options
author | Ivan Levkivskyi <levkivskyi@gmail.com> | 2017-02-24 03:03:28 (GMT) |
---|---|---|
committer | Mariatta <Mariatta@users.noreply.github.com> | 2017-02-24 03:03:28 (GMT) |
commit | abb3b8ad94d699c8560d94ee9bac9c917b382abe (patch) | |
tree | 8032e4fe1bee448b36cfacad7c0f98579a09ca1f | |
parent | 1aceb024172ea3d6f9dd6e90f4fbe63ea1fb054e (diff) | |
download | cpython-abb3b8ad94d699c8560d94ee9bac9c917b382abe.zip cpython-abb3b8ad94d699c8560d94ee9bac9c917b382abe.tar.gz cpython-abb3b8ad94d699c8560d94ee9bac9c917b382abe.tar.bz2 |
Update to typing: treat subscripted generics as proxies (#265)
-rw-r--r-- | Lib/test/test_typing.py | 27 | ||||
-rw-r--r-- | Lib/typing.py | 7 |
2 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 64d8276..3a82b59 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -674,6 +674,33 @@ class GenericTests(BaseTestCase): c.bar = 'abc' self.assertEqual(c.__dict__, {'bar': 'abc'}) + def test_subscripted_generics_as_proxies(self): + T = TypeVar('T') + class C(Generic[T]): + x = 'def' + self.assertEqual(C[int].x, 'def') + self.assertEqual(C[C[int]].x, 'def') + C[C[int]].x = 'changed' + self.assertEqual(C.x, 'changed') + self.assertEqual(C[str].x, 'changed') + C[List[str]].z = 'new' + self.assertEqual(C.z, 'new') + self.assertEqual(C[Tuple[int]].z, 'new') + + self.assertEqual(C().x, 'changed') + self.assertEqual(C[Tuple[str]]().z, 'new') + + class D(C[T]): + pass + self.assertEqual(D[int].x, 'changed') + self.assertEqual(D.z, 'new') + D.z = 'from derived z' + D[int].x = 'from derived x' + self.assertEqual(C.x, 'changed') + self.assertEqual(C[int].z, 'new') + self.assertEqual(D.x, 'from derived x') + self.assertEqual(D[str].z, 'from derived z') + def test_false_subclasses(self): class MyMapping(MutableMapping[str, str]): pass self.assertNotIsInstance({}, MyMapping) diff --git a/Lib/typing.py b/Lib/typing.py index efe358f..fc2ed94 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1158,6 +1158,13 @@ class GenericMeta(TypingMeta, abc.ABCMeta): self.__parameters__, self.__args__, self.__origin__, self.__extra__, self.__orig_bases__) + def __setattr__(self, attr, value): + # We consider all the subscripted genrics as proxies for original class + if attr.startswith('__') and attr.endswith('__'): + super(GenericMeta, self).__setattr__(attr, value) + else: + super(GenericMeta, _gorg(self)).__setattr__(attr, value) + # Prevent checks for Generic to crash when defining Generic. Generic = None |