gh-112903: Handle non-types in _BaseGenericAlias.__mro_entries__() (#115191)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Carl Meyer 2024-02-09 12:19:09 -07:00 committed by GitHub
parent 5a173efa69
commit a225520af9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 92 additions and 1 deletions

View file

@ -4920,6 +4920,75 @@ class GenericTests(BaseTestCase):
class C(List[int], B): ...
self.assertEqual(C.__mro__, (C, list, B, Generic, object))
def test_multiple_inheritance_non_type_with___mro_entries__(self):
class GoodEntries:
def __mro_entries__(self, bases):
return (object,)
class A(List[int], GoodEntries()): ...
self.assertEqual(A.__mro__, (A, list, Generic, object))
def test_multiple_inheritance_non_type_without___mro_entries__(self):
# Error should be from the type machinery, not from typing.py
with self.assertRaisesRegex(TypeError, r"^bases must be types"):
class A(List[int], object()): ...
def test_multiple_inheritance_non_type_bad___mro_entries__(self):
class BadEntries:
def __mro_entries__(self, bases):
return None
# Error should be from the type machinery, not from typing.py
with self.assertRaisesRegex(
TypeError,
r"^__mro_entries__ must return a tuple",
):
class A(List[int], BadEntries()): ...
def test_multiple_inheritance___mro_entries___returns_non_type(self):
class BadEntries:
def __mro_entries__(self, bases):
return (object(),)
# Error should be from the type machinery, not from typing.py
with self.assertRaisesRegex(
TypeError,
r"^bases must be types",
):
class A(List[int], BadEntries()): ...
def test_multiple_inheritance_with_genericalias(self):
class A(typing.Sized, list[int]): ...
self.assertEqual(
A.__mro__,
(A, collections.abc.Sized, Generic, list, object),
)
def test_multiple_inheritance_with_genericalias_2(self):
T = TypeVar("T")
class BaseSeq(typing.Sequence[T]): ...
class MySeq(List[T], BaseSeq[T]): ...
self.assertEqual(
MySeq.__mro__,
(
MySeq,
list,
BaseSeq,
collections.abc.Sequence,
collections.abc.Reversible,
collections.abc.Collection,
collections.abc.Sized,
collections.abc.Iterable,
collections.abc.Container,
Generic,
object,
),
)
def test_init_subclass_super_called(self):
class FinalException(Exception):
pass