gh-155087: Add ternary conditional operator (?:) and $-prefix None-declaration syntax - #155089
gh-155087: Add ternary conditional operator (?:) and $-prefix None-declaration syntax#155089RAJ0793 wants to merge 7 commits into
Conversation
|
The following commit authors need to sign the Contributor License Agreement: |
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Documentation build overview
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4e3560f096
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| | cond=disjunction '?' yes=expression ':' no=expression { | ||
| _PyAST_IfExp(cond, yes, no, EXTRA) |
There was a problem hiding this comment.
Gate the new syntax by feature_version
When callers parse with an older grammar via PyCF_ONLY_AST/ast.parse(..., feature_version=(3, 15)), this rule still accepts a ? b : c because it constructs IfExp directly instead of using CHECK_VERSION. Since this tree is already Python 3.16 (PY_MINOR_VERSION is 16), the new ?: syntax (and the new $ statement rule) should be wrapped in a version check so tools that request an older grammar continue to reject it.
Useful? React with 👍 / 👎.
| - ``":="`` | ||
| * - .. data:: EXCLAMATION | ||
| - ``"!"`` | ||
| .. Auto-generated by Tools/build/generate_token.py |
There was a problem hiding this comment.
Use LF endings for regenerated files
This regenerated file was written with CRLF line endings, and the same happened to many generated C/Python files in the patch; git diff --check e56405d^ e56405d reports these added lines as trailing whitespace. That will make whitespace/patch checks fail even though the semantic token changes are small, so these files should be regenerated or normalized with LF endings before landing.
Useful? React with 👍 / 👎.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Large feature requests must be discussed and require a PEP. Do not suggest such changes in the future please. |
What does this PR do?
Adds two new pieces of syntax to CPython's grammar as a proof-of-concept:
$name— declares a variable pre-initialized toNone:?:(C-style), including support forassignment-aware branches via
NamedExprreuse:Related issue
Related issue
See discussion in gh-155087
Motivation
Python's existing
x if cond else ycovers the same use case as?:, andthis PR is intended primarily as a working reference implementation to
support the discussion in the linked issue, not as a claim that this should
be merged as-is. I wanted to validate the feasibility (grammar, parser,
codegen) before further discussion.
What's implemented
$nameinGrammar/python.gram(dollar_stmt)?:(question_if_expression), integrated intoexpressionandexpression_without_invalidso it works consistentlyacross statements, comprehensions, generator expressions, lambdas, and
keyword arguments
NamedExpr(:=)AST node rather than introducing a new ASDL node, since
:=alreadyprovides "assign and yield that value" semantics
Known limitations
f"{cond ? a : b}") currently fails withoutparentheses — this is because
fstring_replacement_fieldusesannotated_rhsin a context where the f-string's own tokenizer appearsto conflict with the
?/:tokens. This needs further investigationand is not yet fixed in this PR.
the reference implementation available for discussion, per feedback in
the linked issue.
Testing done
Manually tested (no automated test suite added yet):
if/elseternary and other syntax remain unaffectedChecklist
whether one is needed is welcome