Skip to content

gh-78416: Allow "|" after "$" for required keyword-only arguments - #155331

Draft
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:getargs-required-kwonly-bar
Draft

gh-78416: Allow "|" after "$" for required keyword-only arguments#155331
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:getargs-required-kwonly-bar

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Aug 7, 2026

Copy link
Copy Markdown
Member

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"), so f(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 no new syntax at all (taleinat's proposal). | may now follow $, ending the required keyword-only arguments: "O|O$O|O" corresponds to f(a, b=None, *, c, d=None). Without a | after it, $ keeps its current meaning exactly. Every format it gives meaning to is a SystemError today, so nothing that works can change.

Argument Clinic uses the trailing | when $ alone 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.

It is the smallest change to Python/getargs.c of the three, because the "$ before |" error disappears entirely. Against that: whether an argument after $ is required now depends on text further right, so the scanner must look ahead; and a forgotten trailing | silently makes required keyword-only arguments optional, since "O|O$O|" is f(a, b=None, *, c) while "O|O$O" is f(a, b=None, *, c=None) — both legal, neither diagnosable.

The other two alternatives: #155329, #155330.

@read-the-docs-community

read-the-docs-community Bot commented Aug 7, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33962527 | 📁 Comparing 625e7b2 against main (7c653e2)

  🔍 Preview build  

4 files changed
± c-api/arg.html
± library/test.html
± whatsnew/3.16.html
± whatsnew/changelog.html

…eTupleAndKeywords()

"|" can now be specified after "$" to end the required keyword-only
arguments, so that they can be mixed with optional arguments.  The
keyword-only arguments are required until "|", if it follows, and inherit
the state of the positional arguments otherwise.

Argument Clinic uses this if "$" alone 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.
@serhiy-storchaka
serhiy-storchaka force-pushed the getargs-required-kwonly-bar branch from 7805b1c to 625e7b2 Compare August 7, 2026 15:27
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.

1 participant