-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Add support for partial keyword arguments in extension functions #70470
Comments
Currently extension functions either accept only positional-only arguments (PyArg_ParseTuple), or keyword arguments (PyArg_ParseTupleAndKeywords). While adding support passing by keywords looks good for some arguments, for other arguments it doesn't make much sense. For example "encoding" and "errors" arguments for str or "base" argument for int are examples of good keyword arguments, but it is hard to choose good name for the first argument. I suggest to allow to add the support of keyword arguments only for the part of arguments, while left other arguments positional-only. This issue consists from two stages:
|
For the examples you gave, they already seem to support keywords for the first parameter: int(x=0), str(object=""), bytes(source=b""). Maybe it is a bit unfortunate that they are inconsistent, but these names seem reasonable when considered separately. But I agree that in those cases having the option of a keyword would rarely be useful. This proposal sounds like it could give a small benefit, and I guess it wouldn’t be costly to implement. |
+1 for this suggestion. There are a number of places where we've been trapped between having no keyword arguments or having to use keywords for all arguments even when it doesn't make sense to have all arguments be keywords. |
Proposed patch adds support for positional-only parameters in PyArg_ParseTupleAndKeywords(). It is simple, the most complex part is generating detailed error message for the case of calling with insufficient number of positional arguments. |
Removed development code from tests (thanks Martin for catching this) and adds support in Argument Clinic. Now the patch is complete and ready for final review. |
For example here is a patch that partially reverts bpo-26243 by making "data" positional parameter, but keeping "level" positional-or-keyword parameter. |
If there are no objections I'm inclined to commit pyarg_parse_positional_only_and_keywords_2.patch in short time. |
I made some suggestions in the documentation. I only skimmed over the C and arg clinic code, but there is nothing obviously wrong. |
On new patch applied Martin's suggestions, added entities in What's New and NEWS, added a reference to the glossary, and improved error message. |
New changeset 69c0aa8a8185 by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: