Skip to content

Conversation

yanyongyu
Copy link
Contributor

@yanyongyu yanyongyu commented Aug 16, 2025

This PR fixes the inspect.signature behavior when inspecting a class that uses descriptor (like classmethod/staticmethod/custom) with a wrapped __init__.

This PR also adds more test cases not covered in PR #132055.

@python-cla-bot
Copy link

python-cla-bot bot commented Aug 16, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 16, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@serhiy-storchaka serhiy-storchaka self-requested a review August 16, 2025 19:15
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR, @yanyongyu. LGTM. 👍

I only have few minor suggestions for tests.


with self.subTest('partial'):
class C:
__init__ = identity(functools.partial(lambda x, a, b: None, 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this be functools.partial(identity(lambda x, a, b: None), 2)? Although the "partial" and "partialmethod" subtests are not affected by all these changes, so I am not sure that it makes sense to keep them. If they do not fail, we cannot be sure that they are meaningful.

Copy link
Contributor Author

@yanyongyu yanyongyu Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing out the error, I will correct this.

These test cases are based on the ones below (test_signature_on_class_with_init and test_signature_on_class_with_new), and I believe they need to be this detailed to catch potential bugs from changes made in inspect.signature's behavior.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka
Copy link
Member

I added also tests for a wrapped metaclass' __call__. This is more rare than custom __new__ or __init__, but it uses the same mechanism. And renamed the decorators -- because identity is usually a name of a function that returns its argument, this may be confusing.

@serhiy-storchaka serhiy-storchaka merged commit 025a213 into python:main Aug 28, 2025
45 checks passed
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Aug 28, 2025
@miss-islington-app
Copy link

Thanks @yanyongyu for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @yanyongyu for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 28, 2025
…t__ or __new__ (pythonGH-137862)

Fixed several cases where __init__, __new__ or metaclass` __call__ is a descriptor
that returns a wrapped function.
(cherry picked from commit 025a213)

Co-authored-by: Ju4tCode <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 28, 2025
…t__ or __new__ (pythonGH-137862)

Fixed several cases where __init__, __new__ or metaclass` __call__ is a descriptor
that returns a wrapped function.
(cherry picked from commit 025a213)

Co-authored-by: Ju4tCode <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Aug 28, 2025

GH-138224 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Aug 28, 2025
@bedevere-app
Copy link

bedevere-app bot commented Aug 28, 2025

GH-138225 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Aug 28, 2025
@yanyongyu yanyongyu deleted the fix-issue-137317 branch August 28, 2025 15:07
serhiy-storchaka pushed a commit to miss-islington/cpython that referenced this pull request Aug 28, 2025
…t__ or __new__ (pythonGH-137862)

Fixed several cases where __init__, __new__ or metaclass` __call__ is a descriptor
that returns a wrapped function.
(cherry picked from commit 025a213)

Co-authored-by: Ju4tCode <[email protected]>
serhiy-storchaka pushed a commit that referenced this pull request Aug 28, 2025
…it__ or __new__ (GH-137862) (GH-138225)

Fixed several cases where __init__, __new__ or metaclass` __call__ is a descriptor
that returns a wrapped function.
(cherry picked from commit 025a213)

Co-authored-by: Ju4tCode <[email protected]>
lkollar pushed a commit to lkollar/cpython that referenced this pull request Sep 9, 2025
…t__ or __new__ (pythonGH-137862)

Fixed several cases where __init__, __new__ or metaclass` __call__ is a descriptor
that returns a wrapped function.
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.

2 participants