From f8614b4204344c2a7ced039e4f1de18b8c3b87ca Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Thu, 3 Aug 2023 12:18:31 +0100 Subject: [PATCH 1/8] gh-107576: Ensure `__orig_bases__` are our own in get_original_bases --- Lib/test/test_types.py | 13 +++++++++++++ Lib/types.py | 14 +++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 81744940f25b82..8adc2de0e970e0 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1397,6 +1397,7 @@ class A: pass 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 F(list[int]): pass 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(G), (First, Second)) + class ClassBasedNamedTuple(typing.NamedTuple): x: int diff --git a/Lib/types.py b/Lib/types.py index 6110e6e1de7249..a187ae24243dc2 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -164,15 +164,15 @@ class Baz(list[str]): ... assert get_original_bases(Spam) == (TypedDict,) assert get_original_bases(int) == (object,) """ - try: + if "__orig_bases__" in cls.__dict__: # GH-107576: don't return the parent's return cls.__orig_bases__ + + try: + return 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: From 861bc9788f021c1a744658fdd172794eced4c7e9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 3 Aug 2023 11:31:18 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst 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 00000000000000..2a645700b30234 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst @@ -0,0 +1 @@ +Fix :func:`types.get_original_bases` to only return :attr:`object.__orig_bases__` if it is present on `cls` directly. Patch by James Hilton-Balfe. From a93cedcc09283a5c20603187578674aa5510f648 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Thu, 3 Aug 2023 12:32:03 +0100 Subject: [PATCH 3/8] cls should be double backtick-ed --- .../next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 2a645700b30234..9418a5bb48f9e4 100644 --- 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 @@ -1 +1 @@ -Fix :func:`types.get_original_bases` to only return :attr:`object.__orig_bases__` if it is present on `cls` directly. Patch by James Hilton-Balfe. +Fix :func:`types.get_original_bases` to only return :attr:`object.__orig_bases__` if it is present on ``cls`` directly. Patch by James Hilton-Balfe. From 6b9e8438fcdd945b67d056de8a4d79baa34b5bef Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Thu, 3 Aug 2023 13:07:50 +0100 Subject: [PATCH 4/8] Be more defensive --- Lib/types.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/types.py b/Lib/types.py index a187ae24243dc2..04612f122131a0 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -164,8 +164,13 @@ class Baz(list[str]): ... assert get_original_bases(Spam) == (TypedDict,) assert get_original_bases(int) == (object,) """ - if "__orig_bases__" in cls.__dict__: # GH-107576: don't return the parent's - return cls.__orig_bases__ + try: + cls_dict = cls.__dict__ + except AttributeError: + pass + else: + if "__orig_bases__" in cls_dict: # GH-107576: don't return the parent's + return cls.__orig_bases__ try: return cls.__bases__ From 3f6552efb8fb35d97518a87c6dc41dce2987138c Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Thu, 3 Aug 2023 14:16:04 +0100 Subject: [PATCH 5/8] Update Lib/test/test_types.py Co-authored-by: Chris Bouchard --- Lib/test/test_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 8adc2de0e970e0..f2efee90dc0240 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1420,7 +1420,7 @@ class G(First, Second): pass class First_(typing.Generic[T]): pass class Second_(typing.Generic[T]): pass class H(First_, Second_): pass - self.assertEqual(types.get_original_bases(G), (First, Second)) + self.assertEqual(types.get_original_bases(H), (First_, Second_)) class ClassBasedNamedTuple(typing.NamedTuple): x: int From 55476cdc92f357244b688a23702977771b7e96cf Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Thu, 3 Aug 2023 14:24:38 +0100 Subject: [PATCH 6/8] Simplify the checking Co-authored-by: AlexWaygood --- Lib/types.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Lib/types.py b/Lib/types.py index 04612f122131a0..b4aa19cec40c89 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -165,15 +165,7 @@ class Baz(list[str]): ... assert get_original_bases(int) == (object,) """ try: - cls_dict = cls.__dict__ - except AttributeError: - pass - else: - if "__orig_bases__" in cls_dict: # GH-107576: don't return the parent's - return cls.__orig_bases__ - - try: - return cls.__bases__ + return cls.__dict__.get("__orig_bases__", cls.__bases__) except AttributeError: raise TypeError( f"Expected an instance of type, not {type(cls).__name__!r}" From d526ac18029a942fb32127f45b25f429dbe97ad4 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Thu, 3 Aug 2023 14:32:17 +0100 Subject: [PATCH 7/8] Update Misc/NEWS.d/next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst Co-authored-by: Alex Waygood --- .../Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 index 9418a5bb48f9e4..f8cdcdfd49ff17 100644 --- 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 @@ -1 +1,3 @@ -Fix :func:`types.get_original_bases` to only return :attr:`object.__orig_bases__` if it is present on ``cls`` directly. Patch by James Hilton-Balfe. +Fix :func:`types.get_original_bases` to only return +:attr:`!object.__orig_bases__` if it is present on ``cls`` directly. Patch by +James Hilton-Balfe. From 3490051030e8e38cedb78271d51cfe4f0613267f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 3 Aug 2023 14:33:36 +0100 Subject: [PATCH 8/8] Update 2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst --- .../next/Library/2023-08-03-11-31-11.gh-issue-107576.pO_s9I.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index f8cdcdfd49ff17..67677dd3c8ed24 100644 --- 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 @@ -1,3 +1,3 @@ Fix :func:`types.get_original_bases` to only return -:attr:`!object.__orig_bases__` if it is present on ``cls`` directly. Patch by +:attr:`!__orig_bases__` if it is present on ``cls`` directly. Patch by James Hilton-Balfe.