Skip to content

Commit

Permalink
Update to typing: treat subscripted generics as proxies (python#265)
Browse files Browse the repository at this point in the history
(cherry picked from commit abb3b8a)
  • Loading branch information
ilevkivskyi authored and Mariatta committed Feb 24, 2017
1 parent e297414 commit 2ad381f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,33 @@ class C(B[int]):
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)
Expand Down
7 changes: 7 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,13 @@ def __copy__(self):
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
Expand Down

0 comments on commit 2ad381f

Please sign in to comment.