Skip to content

Commit

Permalink
bpo-45664: Fix resolve_bases() and new_class() for GenericAlias insta…
Browse files Browse the repository at this point in the history
…nce as a base (GH-29298)

(cherry picked from commit 2b318ce)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
miss-islington and serhiy-storchaka committed Dec 5, 2021
1 parent abceb66 commit cb68c0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,17 @@ def __mro_entries__(self, bases):
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
Expand Down Expand Up @@ -1351,6 +1362,11 @@ def __mro_entries__(self, bases):
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
Expand Down
2 changes: 1 addition & 1 deletion Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`types.resolve_bases` and :func:`types.new_class` for
:class:`types.GenericAlias` instance as a base.

0 comments on commit cb68c0a

Please sign in to comment.