gh-78416: Add the "%" format unit for required keyword-only arguments - #155329
Draft
serhiy-storchaka wants to merge 1 commit into
Draft
gh-78416: Add the "%" format unit for required keyword-only arguments#155329serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
…eTupleAndKeywords() The new format unit "%" starts the keyword-only arguments which are required until "|", so that they can be mixed with optional arguments. Unlike "$", it does not depend on whether "|" was specified before it. Argument Clinic uses it if "$" cannot express the signature, so that such functions can now use the limited C API. It also no longer rejects a required keyword-only parameter after an optional positional one.
This was referenced Aug 7, 2026
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PyArg_ParseTupleAndKeywords()supports only three of the four kinds of parameters: keyword-only arguments are either all required ("OO$OO") or all optional ("O|O$O"), sof(a, b=None, *, c, d=None)cannot be expressed.This is one of three alternative implementations, made for comparison as suggested in the issue; this one adds a new format unit.
%replaces$and starts the keyword-only arguments as required,|making the rest optional:"O|O%O|O"corresponds tof(a, b=None, *, c, d=None). Its meaning does not depend on whether|was specified before it, so the meaning of every unit is fixed by the text to its left and no lookahead is needed except in the fast-return path.Argument Clinic uses
%when$cannot express the signature, so such functions can now use the limited C API, and it no longer rejects a required keyword-only parameter after an optional positional one.The cost is a format character, which can then never be used for anything else, spent on a marker that nearly duplicates
$: without a preceding|they are the same thing,"OO%OO"and"OO$OO"both meaningf(a, b, *, c, d). The two diverge only when|precedes the marker, where$makes the keyword-only arguments optional and%keeps them required until the next|.The other two alternatives: #155330, #155331.