Skip to content

Commit 99fa708

Browse files
authored
Backport CPython PR 107584 (#275)
1 parent 688fbd2 commit 99fa708

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Release 4.8.0 (???)
22

33
- Drop support for Python 3.7 (including PyPy-3.7). Patch by Alex Waygood.
4+
- Fix bug where `get_original_bases()` would return incorrect results when
5+
called on a concrete subclass of a generic class. Patch by Alex Waygood
6+
(backporting https://github.com/python/cpython/pull/107584, by James
7+
Hilton-Balfe).
48

59
# Release 4.7.1 (July 2, 2023)
610

Diff for: src/test_typing_extensions.py

+19
Original file line numberDiff line numberDiff line change
@@ -5704,6 +5704,25 @@ class F(list[int]): pass
57045704
self.assertEqual(get_original_bases(E), (list[T],))
57055705
self.assertEqual(get_original_bases(F), (list[int],))
57065706

5707+
@skipIf(
5708+
sys.version_info[:3] == (3, 12, 0) and sys.version_info[3] in {"alpha", "beta"},
5709+
"Early versions of py312 had a bug"
5710+
)
5711+
def test_concrete_subclasses_of_generic_classes(self):
5712+
T = TypeVar("T")
5713+
5714+
class FirstBase(Generic[T]): pass
5715+
class SecondBase(Generic[T]): pass
5716+
class First(FirstBase[int]): pass
5717+
class Second(SecondBase[int]): pass
5718+
class G(First, Second): pass
5719+
self.assertEqual(get_original_bases(G), (First, Second))
5720+
5721+
class First_(Generic[T]): pass
5722+
class Second_(Generic[T]): pass
5723+
class H(First_, Second_): pass
5724+
self.assertEqual(get_original_bases(H), (First_, Second_))
5725+
57075726
def test_namedtuples(self):
57085727
# On 3.12, this should work well with typing.NamedTuple and typing_extensions.NamedTuple
57095728
# On lower versions, it will only work fully with typing_extensions.NamedTuple

Diff for: src/typing_extensions.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -2570,14 +2570,11 @@ class Baz(list[str]): ...
25702570
assert get_original_bases(int) == (object,)
25712571
"""
25722572
try:
2573-
return cls.__orig_bases__
2573+
return cls.__dict__.get("__orig_bases__", cls.__bases__)
25742574
except AttributeError:
2575-
try:
2576-
return cls.__bases__
2577-
except AttributeError:
2578-
raise TypeError(
2579-
f'Expected an instance of type, not {type(cls).__name__!r}'
2580-
) from None
2575+
raise TypeError(
2576+
f'Expected an instance of type, not {type(cls).__name__!r}'
2577+
) from None
25812578

25822579

25832580
# NewType is a class on Python 3.10+, making it pickleable

0 commit comments

Comments
 (0)