Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-45664: Fix resolve_bases() and new_class() for GenericAlias instance as a base #29298

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.