Skip to content

gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults #130192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

jacobtylerwalls
Copy link
Contributor

@jacobtylerwalls jacobtylerwalls commented Feb 16, 2025

Before
inspect.Signature.bind() failed to raise TypeError for positional-only arguments passed by keyword (due to a regression handling the case where a default is defined).
After
This is fixed.

Regression in 9c15202.

Interestingly, we did have a test case for this, but it unexpectedly passed because after calling bind(), the test helper also calls the underlying function, making assertRaises(TypeError, ... succeed regardless of the behavior of bind().

Closes #130164

@@ -5349,7 +5353,7 @@ def test(a_po, b_po, c_po=3, /, foo=42, *, bar=50, **kwargs):
self.assertEqual(self.call(test, 1, 2, c_po=4),
(1, 2, 3, 42, 50, {'c_po': 4}))

with self.assertRaisesRegex(TypeError, "missing 2 required positional arguments"):
with self.assertRaisesRegex(TypeError, "missing a required positional-only argument: 'a_po'"):
Copy link
Contributor Author

@jacobtylerwalls jacobtylerwalls Feb 16, 2025

Choose a reason for hiding this comment

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

Some of these cases could be moved to the existing method test_signature_bind_posonly_kwargs() if desired?

@jacobtylerwalls jacobtylerwalls changed the title gh-130164: Fix inspect.signature.bind() handling of positional-only args without defaults gh-130164: Fix inspect.Signature.bind() handling of positional-only args without defaults Feb 16, 2025
Copy link

@dfremont dfremont left a comment

Choose a reason for hiding this comment

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

Thanks for the very quick fix, @jacobtylerwalls! The new logic looks correct to me and does fix the bug I reported (the exception message is slightly different than a direct call to the function, but I at least don't care about that).

@skirpichev
Copy link
Member

CC @picnixz

@jacobtylerwalls
Copy link
Contributor Author

Thanks for the review (and the original report!)

(the exception message is slightly different than a direct call to the function, but I at least don't care about that).

Right, this was something I was trying to keep to in the original PR (#103404), but during review discussion there I became convinced it was of only marginal benefit. (This time around it seemed too complicated to be worth it, on first look anyway.)

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 serhiy-storchaka added the needs backport to 3.13 bugs and security fixes label Feb 18, 2025
@serhiy-storchaka serhiy-storchaka merged commit dab456d into python:main Feb 18, 2025
49 checks passed
@miss-islington-app
Copy link

Thanks @jacobtylerwalls 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 Feb 18, 2025
…only args without defaults (pythonGH-130192)

Follow-up to 9c15202.
(cherry picked from commit dab456d)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Feb 18, 2025

GH-130271 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 Feb 18, 2025
@jacobtylerwalls jacobtylerwalls deleted the fix-130164 branch February 18, 2025 16:27
serhiy-storchaka pushed a commit that referenced this pull request Apr 8, 2025
…-only args without defaults (GH-130192) (GH-130271)

Follow-up to 9c15202.
(cherry picked from commit dab456d)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.12 only security fixes label Apr 8, 2025
@miss-islington-app
Copy link

Thanks @jacobtylerwalls for the PR, and @serhiy-storchaka 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 Apr 8, 2025
…only args without defaults (pythonGH-130192)

Follow-up to 9c15202.
(cherry picked from commit dab456d)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Apr 8, 2025

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

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Apr 8, 2025
serhiy-storchaka pushed a commit that referenced this pull request Apr 8, 2025
…-only args without defaults (GH-130192) (GH-132259)

Follow-up to 9c15202.
(cherry picked from commit dab456d)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
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.

Signature.bind allows certain positional-only parameters as keywords
4 participants