gh-153568: Share expression parsing with assignment targets - #157450
Merged
pablogsal merged 2 commits intoSep 14, 2026
Conversation
pablogsal
requested review from
Eclips4,
lysnikolaou and
tomasr8
as code owners
September 13, 2026 19:03
pablogsal
force-pushed
the
gh-153568-simplify-target-grammar
branch
from
September 14, 2026 15:45
8e86eef to
d79c9b3
Compare
pablogsal
enabled auto-merge (squash)
September 14, 2026 15:45
pablogsal
force-pushed
the
gh-153568-simplify-target-grammar
branch
from
September 14, 2026 16:10
d79c9b3 to
59e69f0
Compare
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.
The grammar has two versions of almost the same parser:
primaryfor expressions andt_primaryfor assignment and deletion targets. Forfactory().field[index], both walk through the call, attribute and subscript. When we try an assignment and then fall back to an expression, that means doing the same work through a different set of rules.This lets targets use
primarytoo. Parse the expression once, check that its shape is allowed as a target, then copy it into Store or Del context. Forfactory().field[index] = value, only the outer subscript changes context. The call, attribute and index stay Load, as does the cached expression.That removes
t_primary,star_atom,del_t_atomand the separate target list and tuple rules. It also folds the comparison forwarding rules into their parent, shares the keyword-argument prefix, and gives display items shorter names next to the list, tuple and set rules. There are 15 fewer grammar rules in total.On my machine, this makes parsing the full standard library and twenty repositories about 9.1% faster overall.
The table compares this change against
mainatfb46c67d56, using release builds without PGO or LTO. Each result is the median of five paired runs. The timings include tokenization, building the AST and cleanup, without compiling bytecode or running the files.Part of #153568.