Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-127610: Added validation for more than one var positional and var keyword parameters in inspect.Signature#127657
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ghost commentedDec 5, 2024 • edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
And please add news. This is tiny, but user-visible change.
Lib/inspect.py Outdated
| var_positional_count+=1 | ||
| ifvar_positional_count>1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It seems,*_count actually didn't count anything, isn't? I suggest use bool values instead and rename variables to something likeseen_var_positional.
Lib/inspect.py Outdated
| ifkind==_VAR_POSITIONAL: | ||
| var_positional_count+=1 | ||
| ifvar_positional_count>1: | ||
| msg='more than one var positional parameter' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Perglossary, probably it is better to use "var-positional" (and "var-keyword") terms.
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM, with a small nitpick.
Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
…tv_NP.rstCo-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You do not actually needseen_var_positional andseen_var_keyword.seen_var_positional is equal totopkind >= _VAR_POSITIONAL (before settingtopkind = kind).
You can addif kind == top_kind and kind in (_VAR_POSITIONAL, _VAR_KEYWORD) beforeelif kind > top_kind. Usekind.description to format the error message.
| second_args=args.replace(name="second_args") | ||
| withself.assertRaisesRegex(ValueError,'more than one var-positional parameter'): | ||
| S((args,second_args)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Add also a test for the case when there are other parameters (keyword-only or var-keyword) between two var-positional parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
In this case we will get error:ValueError: wrong parameter order: keyword-only parameter before variadic positional parameter. Should we catch it in the test?
My test:S((args, ko, second_args))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Technically, there are two errors: "keyword-only parameter before variadic positional parameter" and "more than one variadic positional parameter". Which of them are preferable to report? I think the latter, therefore we should change the order of checks. And tests are needed to ensure that the correct error message is used.
ApostolFetDec 7, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
In that case we all need the variables seen_var_positional and seen_var_keyword, to check that these parameters have not occurred before even if the parameter order is wrong, right?if kind == top_kind and kind in (_VAR_POSITIONAL, _VAR_KEYWORD) - this check is designed for the correct order of parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hmm, yes, you are right. So you need to simply move the new checks up. I still suggest to usekind.description to format the error message.
You can unify the code for var-positional and var-keyword if use a set instead of two boolean variables.
ifkindin (_VAR_POSITIONAL,_VAR_KEYWORD):ifkindinseen_var_parameters:raise ...seen_var_parameters.add(kind)
Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
…tv_NP.rstCo-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM. 👍
You can use f-string for formatting the error message.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'm ok with this current form and you can change to f-strings as Serhiy said if you want (but forget about myset vs tuple suggestion)
1503fc8 intopython:mainUh oh!
There was an error while loading.Please reload this page.
bedevere-bot commentedDec 8, 2024
|
bedevere-bot commentedDec 8, 2024
|
Eclips4 commentedDec 8, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@serhiy-storchaka what do you think about backports to 3.13 and 3.12? FYI: buildbots failures are unrelated, it seems that there is some kind of network issue. |
bedevere-bot commentedDec 8, 2024
|
…d var-keyword parameters in inspect.Signature (pythonGH-127657)
Uh oh!
There was an error while loading.Please reload this page.
Added flags for var positional and var keyword parameters.
Raise ValueError if arguments are more than one.
Added corresponding tests