summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_genericalias.py
diff options
context:
space:
mode:
authorMatthew Rahtz <matthew.rahtz@gmail.com>2022-03-12 12:20:12 (GMT)
committerGitHub <noreply@github.com>2022-03-12 12:20:12 (GMT)
commitaf2277e461aee4eb96affd06b4af25aad31c81ea (patch)
tree236c10cc5d6a6f577373a95ecc53023752d21283 /Lib/test/test_genericalias.py
parent75174371e6cac935b598a68c1113f6db1e0d6ed8 (diff)
downloadcpython-af2277e461aee4eb96affd06b4af25aad31c81ea.zip
cpython-af2277e461aee4eb96affd06b4af25aad31c81ea.tar.gz
cpython-af2277e461aee4eb96affd06b4af25aad31c81ea.tar.bz2
bpo-43224: Implement PEP 646 changes to genericaliasobject.c (GH-31019)
Specifically, prepare for starring of tuples via a new genericalias iter type. GenericAlias also partially supports the iterator protocol after this change. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_genericalias.py')
-rw-r--r--Lib/test/test_genericalias.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index 1407657..39c56f2 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -169,6 +169,24 @@ class BaseTest(unittest.TestCase):
self.assertEqual(repr(list[str]), 'list[str]')
self.assertEqual(repr(list[()]), 'list[()]')
self.assertEqual(repr(tuple[int, ...]), 'tuple[int, ...]')
+ x1 = tuple[
+ tuple( # Effectively the same as starring; TODO
+ tuple[int]
+ )
+ ]
+ self.assertEqual(repr(x1), 'tuple[*tuple[int]]')
+ x2 = tuple[
+ tuple( # Ditto TODO
+ tuple[int, str]
+ )
+ ]
+ self.assertEqual(repr(x2), 'tuple[*tuple[int, str]]')
+ x3 = tuple[
+ tuple( # Ditto TODO
+ tuple[int, ...]
+ )
+ ]
+ self.assertEqual(repr(x3), 'tuple[*tuple[int, ...]]')
self.assertTrue(repr(MyList[int]).endswith('.BaseTest.test_repr.<locals>.MyList[int]'))
self.assertEqual(repr(list[str]()), '[]') # instances should keep their normal repr
@@ -182,6 +200,7 @@ class BaseTest(unittest.TestCase):
def test_parameters(self):
from typing import List, Dict, Callable
+
D0 = dict[str, int]
self.assertEqual(D0.__args__, (str, int))
self.assertEqual(D0.__parameters__, ())
@@ -197,6 +216,7 @@ class BaseTest(unittest.TestCase):
D2b = dict[T, T]
self.assertEqual(D2b.__args__, (T, T))
self.assertEqual(D2b.__parameters__, (T,))
+
L0 = list[str]
self.assertEqual(L0.__args__, (str,))
self.assertEqual(L0.__parameters__, ())
@@ -219,6 +239,45 @@ class BaseTest(unittest.TestCase):
self.assertEqual(L5.__args__, (Callable[[K, V], K],))
self.assertEqual(L5.__parameters__, (K, V))
+ T1 = tuple[
+ tuple( # Ditto TODO
+ tuple[int]
+ )
+ ]
+ self.assertEqual(
+ T1.__args__,
+ tuple( # Ditto TODO
+ tuple[int]
+ )
+ )
+ self.assertEqual(T1.__parameters__, ())
+
+ T2 = tuple[
+ tuple( # Ditto TODO
+ tuple[T]
+ )
+ ]
+ self.assertEqual(
+ T2.__args__,
+ tuple( # Ditto TODO
+ tuple[T]
+ )
+ )
+ self.assertEqual(T2.__parameters__, (T,))
+
+ T4 = tuple[
+ tuple( # Ditto TODO
+ tuple[int, str]
+ )
+ ]
+ self.assertEqual(
+ T4.__args__,
+ tuple( # Ditto TODO
+ tuple[int, str]
+ )
+ )
+ self.assertEqual(T4.__parameters__, ())
+
def test_parameter_chaining(self):
from typing import List, Dict, Union, Callable
self.assertEqual(list[T][int], list[int])
@@ -249,6 +308,19 @@ class BaseTest(unittest.TestCase):
def test_equality(self):
self.assertEqual(list[int], list[int])
self.assertEqual(dict[str, int], dict[str, int])
+ self.assertEqual((*tuple[int],)[0], (*tuple[int],)[0])
+ self.assertEqual(
+ tuple[
+ tuple( # Effectively the same as starring; TODO
+ tuple[int]
+ )
+ ],
+ tuple[
+ tuple( # Ditto TODO
+ tuple[int]
+ )
+ ]
+ )
self.assertNotEqual(dict[str, int], dict[str, str])
self.assertNotEqual(list, list[int])
self.assertNotEqual(list[int], list)
@@ -346,6 +418,24 @@ class BaseTest(unittest.TestCase):
with self.assertRaises(TypeError):
Bad(list, int, bad=int)
+ def test_iter_creates_starred_tuple(self):
+ t = tuple[int, str]
+ iter_t = iter(t)
+ x = next(iter_t)
+ self.assertEqual(repr(x), '*tuple[int, str]')
+
+ def test_calling_next_twice_raises_stopiteration(self):
+ t = tuple[int, str]
+ iter_t = iter(t)
+ next(iter_t)
+ with self.assertRaises(StopIteration):
+ next(iter_t)
+
+ def test_del_iter(self):
+ t = tuple[int, str]
+ iter_x = iter(t)
+ del iter_x
+
if __name__ == "__main__":
unittest.main()