Skip to content

Commit eb1eb0d

Browse files
ambvgvanrossum
authored andcommitted
Update typing.py from python/typing (3.6.1 PyPI release) (#3059)
1 parent 1ffa0c6 commit eb1eb0d

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

lib-typing/2.7/test_typing.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ def test_cannot_instantiate(self):
9999
with self.assertRaises(TypeError):
100100
type(Any)()
101101

102-
def test_cannot_subscript(self):
103-
with self.assertRaises(TypeError):
104-
Any[int]
105-
106102
def test_any_is_subclass(self):
107103
# These expressions must simply not fail.
108104
typing.Match[Any]
@@ -642,6 +638,41 @@ class C(B[int]):
642638
c.bar = 'abc'
643639
self.assertEqual(c.__dict__, {'bar': 'abc'})
644640

641+
def test_subscripted_generics_as_proxies(self):
642+
T = TypeVar('T')
643+
class C(Generic[T]):
644+
x = 'def'
645+
self.assertEqual(C[int].x, 'def')
646+
self.assertEqual(C[C[int]].x, 'def')
647+
C[C[int]].x = 'changed'
648+
self.assertEqual(C.x, 'changed')
649+
self.assertEqual(C[str].x, 'changed')
650+
C[List[str]].z = 'new'
651+
self.assertEqual(C.z, 'new')
652+
self.assertEqual(C[Tuple[int]].z, 'new')
653+
654+
self.assertEqual(C().x, 'changed')
655+
self.assertEqual(C[Tuple[str]]().z, 'new')
656+
657+
class D(C[T]):
658+
pass
659+
self.assertEqual(D[int].x, 'changed')
660+
self.assertEqual(D.z, 'new')
661+
D.z = 'from derived z'
662+
D[int].x = 'from derived x'
663+
self.assertEqual(C.x, 'changed')
664+
self.assertEqual(C[int].z, 'new')
665+
self.assertEqual(D.x, 'from derived x')
666+
self.assertEqual(D[str].z, 'from derived z')
667+
668+
def test_abc_registry_kept(self):
669+
T = TypeVar('T')
670+
class C(Generic[T]): pass
671+
C.register(int)
672+
self.assertIsInstance(1, C)
673+
C[int]
674+
self.assertIsInstance(1, C)
675+
645676
def test_false_subclasses(self):
646677
class MyMapping(MutableMapping[str, str]): pass
647678
self.assertNotIsInstance({}, MyMapping)

lib-typing/2.7/typing.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,16 @@ def __copy__(self):
12431243
self.__parameters__, self.__args__, self.__origin__,
12441244
self.__extra__, self.__orig_bases__)
12451245

1246+
def __setattr__(self, attr, value):
1247+
# We consider all the subscripted genrics as proxies for original class
1248+
if (
1249+
attr.startswith('__') and attr.endswith('__') or
1250+
attr.startswith('_abc_')
1251+
):
1252+
super(GenericMeta, self).__setattr__(attr, value)
1253+
else:
1254+
super(GenericMeta, _gorg(self)).__setattr__(attr, value)
1255+
12461256

12471257
# Prevent checks for Generic to crash when defining Generic.
12481258
Generic = None
@@ -1900,7 +1910,7 @@ def NamedTuple(typename, fields):
19001910
19011911
Usage::
19021912
1903-
Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1913+
Employee = typing.NamedTuple('Employee', [('name', str), ('id', int)])
19041914
19051915
This is equivalent to::
19061916

lib-typing/3.2/test_typing.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ def test_cannot_instantiate(self):
102102
with self.assertRaises(TypeError):
103103
type(Any)()
104104

105-
def test_cannot_subscript(self):
106-
with self.assertRaises(TypeError):
107-
Any[int]
108-
109105
def test_any_works_with_alias(self):
110106
# These expressions must simply not fail.
111107
typing.Match[Any]
@@ -674,6 +670,41 @@ class C(B[int]):
674670
c.bar = 'abc'
675671
self.assertEqual(c.__dict__, {'bar': 'abc'})
676672

673+
def test_subscripted_generics_as_proxies(self):
674+
T = TypeVar('T')
675+
class C(Generic[T]):
676+
x = 'def'
677+
self.assertEqual(C[int].x, 'def')
678+
self.assertEqual(C[C[int]].x, 'def')
679+
C[C[int]].x = 'changed'
680+
self.assertEqual(C.x, 'changed')
681+
self.assertEqual(C[str].x, 'changed')
682+
C[List[str]].z = 'new'
683+
self.assertEqual(C.z, 'new')
684+
self.assertEqual(C[Tuple[int]].z, 'new')
685+
686+
self.assertEqual(C().x, 'changed')
687+
self.assertEqual(C[Tuple[str]]().z, 'new')
688+
689+
class D(C[T]):
690+
pass
691+
self.assertEqual(D[int].x, 'changed')
692+
self.assertEqual(D.z, 'new')
693+
D.z = 'from derived z'
694+
D[int].x = 'from derived x'
695+
self.assertEqual(C.x, 'changed')
696+
self.assertEqual(C[int].z, 'new')
697+
self.assertEqual(D.x, 'from derived x')
698+
self.assertEqual(D[str].z, 'from derived z')
699+
700+
def test_abc_registry_kept(self):
701+
T = TypeVar('T')
702+
class C(Generic[T]): ...
703+
C.register(int)
704+
self.assertIsInstance(1, C)
705+
C[int]
706+
self.assertIsInstance(1, C)
707+
677708
def test_false_subclasses(self):
678709
class MyMapping(MutableMapping[str, str]): pass
679710
self.assertNotIsInstance({}, MyMapping)

lib-typing/3.2/typing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,16 @@ def __copy__(self):
11581158
self.__parameters__, self.__args__, self.__origin__,
11591159
self.__extra__, self.__orig_bases__)
11601160

1161+
def __setattr__(self, attr, value):
1162+
# We consider all the subscripted genrics as proxies for original class
1163+
if (
1164+
attr.startswith('__') and attr.endswith('__') or
1165+
attr.startswith('_abc_')
1166+
):
1167+
super(GenericMeta, self).__setattr__(attr, value)
1168+
else:
1169+
super(GenericMeta, _gorg(self)).__setattr__(attr, value)
1170+
11611171

11621172
# Prevent checks for Generic to crash when defining Generic.
11631173
Generic = None

0 commit comments

Comments
 (0)