Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantiangaogaotiantian commented Jun 2, 2023

classB: def__new__(self, *args, **kwargs): returnsuper().__new__(self) def__init__(self, value): self.value=valueclassD1(B): def__init__(self, value): super().__init__(value) classD2(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.

@gaogaotiantiangaogaotiantian changed the title Fixed inconsistent signature on derived classesgh-105080: Fixed inconsistent signature on derived classesJun 2, 2023
@carljmcarljm merged commit 9ad199b into python:mainJun 2, 2023
@carljmcarljm 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
…honGH-105217) (cherry picked from commit 9ad199b) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@bedevere-bot
Copy link

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

@bedevere-botbedevere-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 <gaogaotiantian@hotmail.com>
@gaogaotiantiangaogaotiantian deleted the fix-inconsistent-signature branch June 3, 2023 03:48
@carljmcarljm 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
…es (pythonGH-105217). (cherry picked from commit 9ad199b) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
gaogaotiantian added a commit to gaogaotiantian/cpython that referenced this pull request Jun 4, 2023
…es (pythonGH-105217). (cherry picked from commit 9ad199b) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
@bedevere-bot
Copy link

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

@bedevere-botbedevere-bot removed the needs backport to 3.11 only security fixes label Jun 4, 2023
Sign up for freeto 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

@gaogaotiantian@miss-islington@bedevere-bot@carljm@sunmy2019