From 062dedf22ddd55353faa638196dd854d5b2ef25d Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 2 Jun 2023 10:29:44 +0800 Subject: [PATCH 1/2] Fixed inconsistent signature on derived classes --- Lib/inspect.py | 23 ++++++++++++----------- Lib/test/test_inspect.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 15eefdb6570be4..a550202bb0d49b 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2581,17 +2581,18 @@ def _signature_from_callable(obj, *, factory_method = None new = _signature_get_user_defined_method(obj, '__new__') init = _signature_get_user_defined_method(obj, '__init__') - # Now we check if the 'obj' class has an own '__new__' method - if '__new__' in obj.__dict__: - factory_method = new - # or an own '__init__' method - elif '__init__' in obj.__dict__: - factory_method = init - # If not, we take inherited '__new__' or '__init__', if present - elif new is not None: - factory_method = new - elif init is not None: - factory_method = init + + # Go through the MRO and see if any class has user-defined + # pure Python __new__ or __init__ method + for base in obj.__mro__: + # Now we check if the 'obj' class has an own '__new__' method + if new is not None and '__new__' in base.__dict__: + factory_method = new + break + # or an own '__init__' method + elif init is not None and '__init__' in base.__dict__: + factory_method = init + break if factory_method is not None: sig = _get_signature_of(factory_method) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 6a49e3b5530e16..d89953ab60f022 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -3927,6 +3927,24 @@ def __signature__(): ('b', 2, ..., 'positional_or_keyword')), ...)) + def test_signature_on_derived_classes(self): + # gh-105080: Make sure that signatures are consistent on derived classes + + class B: + def __new__(self, *args, **kwargs): + return super().__new__(self) + def __init__(self, value): + self.value = value + + class D1(B): + def __init__(self, value): + super().__init__(value) + + class D2(D1): + pass + + self.assertEqual(inspect.signature(D2), inspect.signature(D1)) + class TestParameterObject(unittest.TestCase): def test_signature_parameter_kinds(self): From 8654fba89a63a5cbc60c0c9e120a6f1a1a6f047c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 2 Jun 2023 02:38:27 +0000 Subject: [PATCH 2/2] =?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-06-02-02-38-26.gh-issue-105080.2imGMg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-06-02-02-38-26.gh-issue-105080.2imGMg.rst diff --git a/Misc/NEWS.d/next/Library/2023-06-02-02-38-26.gh-issue-105080.2imGMg.rst b/Misc/NEWS.d/next/Library/2023-06-02-02-38-26.gh-issue-105080.2imGMg.rst new file mode 100644 index 00000000000000..efe8365a7644be --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-02-02-38-26.gh-issue-105080.2imGMg.rst @@ -0,0 +1 @@ +Fixed inconsistent signature on derived classes for :func:`inspect.signature`