]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45664: Fix resolve_bases() and new_class() for GenericAlias instance as a base...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 5 Dec 2021 20:44:01 +0000 (22:44 +0200)
committerGitHub <noreply@github.com>
Sun, 5 Dec 2021 20:44:01 +0000 (22:44 +0200)
Lib/test/test_types.py
Lib/types.py
Misc/NEWS.d/next/Library/2021-10-28-23-40-54.bpo-45664.7dqtxQ.rst [new file with mode: 0644]

index b1218abc8af5ecc5a580817e3db6488e304ace04..3dfda5cb9566369a666a82445ed88e002b491a59 100644 (file)
@@ -1236,6 +1236,17 @@ class ClassCreationTests(unittest.TestCase):
         self.assertEqual(D.__orig_bases__, (c,))
         self.assertEqual(D.__mro__, (D, A, object))
 
+    def test_new_class_with_mro_entry_genericalias(self):
+        L1 = types.new_class('L1', (typing.List[int],), {})
+        self.assertEqual(L1.__bases__, (list, typing.Generic))
+        self.assertEqual(L1.__orig_bases__, (typing.List[int],))
+        self.assertEqual(L1.__mro__, (L1, list, typing.Generic, object))
+
+        L2 = types.new_class('L2', (list[int],), {})
+        self.assertEqual(L2.__bases__, (list,))
+        self.assertEqual(L2.__orig_bases__, (list[int],))
+        self.assertEqual(L2.__mro__, (L2, list, object))
+
     def test_new_class_with_mro_entry_none(self):
         class A: pass
         class B: pass
@@ -1351,6 +1362,11 @@ class ClassCreationTests(unittest.TestCase):
         for bases in [x, y, z, t]:
             self.assertIs(types.resolve_bases(bases), bases)
 
+    def test_resolve_bases_with_mro_entry(self):
+        self.assertEqual(types.resolve_bases((typing.List[int],)),
+                         (list, typing.Generic))
+        self.assertEqual(types.resolve_bases((list[int],)), (list,))
+
     def test_metaclass_derivation(self):
         # issue1294232: correct metaclass calculation
         new_calls = []  # to check the order of __new__ calls
index ce1c28d5625a251200d34db04040525e9067d972..679c7f638b3100abcb069c8bb05a5331a67c1631 100644 (file)
@@ -82,7 +82,7 @@ def resolve_bases(bases):
     updated = False
     shift = 0
     for i, base in enumerate(bases):
-        if isinstance(base, type):
+        if isinstance(base, type) and not isinstance(base, GenericAlias):
             continue
         if not hasattr(base, "__mro_entries__"):
             continue
diff --git a/Misc/NEWS.d/next/Library/2021-10-28-23-40-54.bpo-45664.7dqtxQ.rst b/Misc/NEWS.d/next/Library/2021-10-28-23-40-54.bpo-45664.7dqtxQ.rst
new file mode 100644 (file)
index 0000000..573a569
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`types.resolve_bases` and :func:`types.new_class` for
+:class:`types.GenericAlias` instance as a base.