summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_typing.py
diff options
context:
space:
mode:
authorIvan Levkivskyi <levkivskyi@gmail.com>2017-12-05 02:43:58 (GMT)
committerNed Deily <nad@python.org>2017-12-05 02:43:58 (GMT)
commit29bc19321018ec6e58f9f4da9c18c42e9a9c580e (patch)
treed3cf4a2d5c487821321a092a17f00f681287f156 /Lib/test/test_typing.py
parentca7562a7fea61646c1b40ace04deecf9903d34a1 (diff)
downloadcpython-29bc19321018ec6e58f9f4da9c18c42e9a9c580e.zip
cpython-29bc19321018ec6e58f9f4da9c18c42e9a9c580e.tar.gz
cpython-29bc19321018ec6e58f9f4da9c18c42e9a9c580e.tar.bz2
[bpo-28556] Minor fixes for typing module (#4710)
Diffstat (limited to 'Lib/test/test_typing.py')
-rw-r--r--Lib/test/test_typing.py50
1 files changed, 48 insertions, 2 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index a3b6eb9..4ae1ebf 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -37,6 +37,9 @@ except ImportError:
from test import mod_generics_cache
+PY36 = sys.version_info[:2] >= (3, 6)
+
+
class BaseTestCase(TestCase):
def assertIsSubclass(self, cls, class_or_tuple, msg=None):
@@ -633,6 +636,27 @@ class GenericTests(BaseTestCase):
with self.assertRaises(TypeError):
Generic[T, S, T]
+ @skipUnless(PY36, "__init_subclass__ support required")
+ def test_init_subclass(self):
+ class X(typing.Generic[T]):
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__(**kwargs)
+ cls.attr = 42
+ class Y(X):
+ pass
+ self.assertEqual(Y.attr, 42)
+ with self.assertRaises(AttributeError):
+ X.attr
+ X.attr = 1
+ Y.attr = 2
+ class Z(Y):
+ pass
+ class W(X[int]):
+ pass
+ self.assertEqual(Y.attr, 2)
+ self.assertEqual(Z.attr, 42)
+ self.assertEqual(W.attr, 42)
+
def test_repr(self):
self.assertEqual(repr(SimpleMapping),
__name__ + '.' + 'SimpleMapping')
@@ -1080,6 +1104,30 @@ class GenericTests(BaseTestCase):
self.assertTrue(t is copy(t))
self.assertTrue(t is deepcopy(t))
+ def test_copy_generic_instances(self):
+ T = TypeVar('T')
+ class C(Generic[T]):
+ def __init__(self, attr: T) -> None:
+ self.attr = attr
+
+ c = C(42)
+ self.assertEqual(copy(c).attr, 42)
+ self.assertEqual(deepcopy(c).attr, 42)
+ self.assertIsNot(copy(c), c)
+ self.assertIsNot(deepcopy(c), c)
+ c.attr = 1
+ self.assertEqual(copy(c).attr, 1)
+ self.assertEqual(deepcopy(c).attr, 1)
+ ci = C[int](42)
+ self.assertEqual(copy(ci).attr, 42)
+ self.assertEqual(deepcopy(ci).attr, 42)
+ self.assertIsNot(copy(ci), ci)
+ self.assertIsNot(deepcopy(ci), ci)
+ ci.attr = 1
+ self.assertEqual(copy(ci).attr, 1)
+ self.assertEqual(deepcopy(ci).attr, 1)
+ self.assertEqual(ci.__orig_class__, C[int])
+
def test_weakref_all(self):
T = TypeVar('T')
things = [Any, Union[T, int], Callable[..., T], Tuple[Any, Any],
@@ -1580,8 +1628,6 @@ else:
asyncio = None
AwaitableWrapper = AsyncIteratorWrapper = ACM = object
-PY36 = sys.version_info[:2] >= (3, 6)
-
PY36_TESTS = """
from test import ann_module, ann_module2, ann_module3
from typing import AsyncContextManager