diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-12-05 20:44:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-05 20:44:01 (GMT) |
commit | 2b318ce1c988b7b6e3caf293d55f289e066b6e0f (patch) | |
tree | 1f0089d63fb9e383c60d294f2fbd99eb0bc24614 | |
parent | 446be166861b2f08f87f74018113dd98ca5fca02 (diff) | |
download | cpython-2b318ce1c988b7b6e3caf293d55f289e066b6e0f.zip cpython-2b318ce1c988b7b6e3caf293d55f289e066b6e0f.tar.gz cpython-2b318ce1c988b7b6e3caf293d55f289e066b6e0f.tar.bz2 |
bpo-45664: Fix resolve_bases() and new_class() for GenericAlias instance as a base (GH-29298)
-rw-r--r-- | Lib/test/test_types.py | 16 | ||||
-rw-r--r-- | Lib/types.py | 2 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-10-28-23-40-54.bpo-45664.7dqtxQ.rst | 2 |
3 files changed, 19 insertions, 1 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index b1218ab..3dfda5c 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -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 diff --git a/Lib/types.py b/Lib/types.py index ce1c28d..679c7f6 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -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 index 0000000..573a569 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-10-28-23-40-54.bpo-45664.7dqtxQ.rst @@ -0,0 +1,2 @@ +Fix :func:`types.resolve_bases` and :func:`types.new_class` for +:class:`types.GenericAlias` instance as a base. |