Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Jun 2, 2023

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

For the code above, inspect.signature(D2) will give a different result than inspect.signature(D1), which is counter-intuitive. This also affects help(D2) vs help(D1).

We should keep the consistency for the signatures for derived classes (of course when the signature is not changed).

This is due to a corner case in inspect.signature() where only the direct defined __init__ and __new__ method on the class takes priority, then __new__ is always used if defined on any base classes. This fix searches __new__ and __init__ based on MRO and prioritize __new__ if both are defined on a base class.

@gaogaotiantian gaogaotiantian changed the title Fixed inconsistent signature on derived classes gh-105080: Fixed inconsistent signature on derived classes Jun 2, 2023
@gaogaotiantian gaogaotiantian requested a review from carljm June 2, 2023 02:37
@carljm carljm merged commit 9ad199b into python:main Jun 2, 2023
@carljm carljm added the needs backport to 3.12 only security fixes label Jun 2, 2023
@miss-islington
Copy link
Contributor

Thanks @gaogaotiantian for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 2, 2023
@bedevere-bot
Copy link

GH-105257 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jun 2, 2023
carljm pushed a commit that referenced this pull request Jun 2, 2023
…-105217) (#105257)

gh-105080: Fixed inconsistent signature on derived classes (GH-105217)
(cherry picked from commit 9ad199b)

Co-authored-by: Tian Gao <[email protected]>
@gaogaotiantian gaogaotiantian deleted the fix-inconsistent-signature branch June 3, 2023 03:48
@carljm carljm added the needs backport to 3.11 only security fixes label Jun 3, 2023
@miss-islington
Copy link
Contributor

Thanks @gaogaotiantian for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @gaogaotiantian and @carljm, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 9ad199ba36791711f596393ca9a20dbf118ef858 3.11

gaogaotiantian added a commit to gaogaotiantian/cpython that referenced this pull request Jun 4, 2023
gaogaotiantian added a commit to gaogaotiantian/cpython that referenced this pull request Jun 4, 2023
@bedevere-bot
Copy link

GH-105274 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants