diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-08-03 15:07:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-03 15:07:43 (GMT) |
commit | d2c7b25afba3d86ee20331d9fb722a6fe1f768ac (patch) | |
tree | 6a20929a39b8273374e147d4a1e1d815faa19c30 | |
parent | 93fcf7587888e93ff7e45f4422c315f8a5f1ba0d (diff) | |
download | cpython-d2c7b25afba3d86ee20331d9fb722a6fe1f768ac.zip cpython-d2c7b25afba3d86ee20331d9fb722a6fe1f768ac.tar.gz cpython-d2c7b25afba3d86ee20331d9fb722a6fe1f768ac.tar.bz2 |
[3.12] gh-107576: Ensure `__orig_bases__` are our own in `get_original_bases` (GH-107584) (#107592)
gh-107576: Ensure `__orig_bases__` are our own in `get_original_bases` (GH-107584)
(cherry picked from commit ed4a978449c856372d1a7cd389f91cafe2581c87)
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Co-authored-by: Chris Bouchard <chris@upliftinglemma.net>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r-- | Lib/test/test_types.py | 13 | ||||
-rw-r--r-- | Lib/types.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst | 3 |
3 files changed, 20 insertions, 7 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 8174494..f2efee9 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1397,6 +1397,7 @@ class ClassCreationTests(unittest.TestCase): class B(typing.Generic[T]): pass class C(B[int]): pass class D(B[str], float): pass + self.assertEqual(types.get_original_bases(A), (object,)) self.assertEqual(types.get_original_bases(B), (typing.Generic[T],)) self.assertEqual(types.get_original_bases(C), (B[int],)) @@ -1409,6 +1410,18 @@ class ClassCreationTests(unittest.TestCase): self.assertEqual(types.get_original_bases(E), (list[T],)) self.assertEqual(types.get_original_bases(F), (list[int],)) + class FirstBase(typing.Generic[T]): pass + class SecondBase(typing.Generic[T]): pass + class First(FirstBase[int]): pass + class Second(SecondBase[int]): pass + class G(First, Second): pass + self.assertEqual(types.get_original_bases(G), (First, Second)) + + class First_(typing.Generic[T]): pass + class Second_(typing.Generic[T]): pass + class H(First_, Second_): pass + self.assertEqual(types.get_original_bases(H), (First_, Second_)) + class ClassBasedNamedTuple(typing.NamedTuple): x: int diff --git a/Lib/types.py b/Lib/types.py index 6110e6e..b4aa19c 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -165,14 +165,11 @@ def get_original_bases(cls, /): assert get_original_bases(int) == (object,) """ try: - return cls.__orig_bases__ + return cls.__dict__.get("__orig_bases__", cls.__bases__) except AttributeError: - try: - return cls.__bases__ - except AttributeError: - raise TypeError( - f'Expected an instance of type, not {type(cls).__name__!r}' - ) from None + raise TypeError( + f"Expected an instance of type, not {type(cls).__name__!r}" + ) from None class DynamicClassAttribute: diff --git a/Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst b/Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst new file mode 100644 index 0000000..67677dd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst @@ -0,0 +1,3 @@ +Fix :func:`types.get_original_bases` to only return +:attr:`!__orig_bases__` if it is present on ``cls`` directly. Patch by +James Hilton-Balfe. |