Skip to content

Commit b01fcb6

Browse files
committed
pythongh-112903: Fix MRO resolution for GenericAlias types
1 parent c1652d6 commit b01fcb6

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/test/test_typing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,6 +4886,20 @@ class B(Generic[S]): ...
48864886
class C(List[int], B): ...
48874887
self.assertEqual(C.__mro__, (C, list, B, Generic, object))
48884888

4889+
def test_multiple_inheritance_gh112903(self):
4890+
# https://github.com/python/cpython/issues/112903
4891+
K = TypeVar("K")
4892+
V = TypeVar("V")
4893+
4894+
class BaseMap(typing.Mapping[K, V]): ...
4895+
class MutableMap(BaseMap[K, V], typing.MutableMapping[K, V]): ...
4896+
# This should not raise:
4897+
class MyMap1(Dict[K, V], MutableMap[K, V]): ...
4898+
class MyMap2(MutableMap[K, V], Dict[K, V]): ...
4899+
4900+
self.assertIn(Generic, MyMap1.__mro__)
4901+
self.assertIn(Generic, MyMap2.__mro__)
4902+
48894903
def test_init_subclass_super_called(self):
48904904
class FinalException(Exception):
48914905
pass

Lib/typing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,10 @@ def __mro_entries__(self, bases):
11371137
res.append(self.__origin__)
11381138
i = bases.index(self)
11391139
for b in bases[i+1:]:
1140-
if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
1140+
if (
1141+
isinstance(b, (_BaseGenericAlias, GenericAlias))
1142+
or issubclass(b, Generic)
1143+
):
11411144
break
11421145
else:
11431146
res.append(Generic)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix MRO resolution issue for generic aliases in 3.13

0 commit comments

Comments
 (0)