Python: Parenthesize template results based on surrounding context#6827
Merged
knutwannheden merged 4 commits intomainfrom Feb 26, 2026
Merged
Python: Parenthesize template results based on surrounding context#6827knutwannheden merged 4 commits intomainfrom
knutwannheden merged 4 commits intomainfrom
Conversation
Template.apply() now checks whether the returned expression needs parentheses when placed in the surrounding AST context, mirroring JavaTemplate.doApply()'s call to ParenthesizeVisitor.maybeParenthesize. Restructures Template.apply() into three phases: 1. Placeholder substitution 2. Precedence-based parenthesization (new) 3. Coordinate application (prefix, statement wrapping, formatting)
Remove dead `coordinates` and `cursor` params from `apply_substitutions`, extend `_get_precedence` to handle `not` unary (precedence 3), fix test calling renamed `apply_coordinates`, and add clarifying docstring for the outer/inner parenthesization boundary.
…ture Remove stale `cursor=None` kwarg from test calls after the parameter was removed from TemplateEngine.apply_substitutions in 7ca8044.
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.
Summary
maybe_parenthesize()to wrap template results inJ.Parentheseswhen their operator precedence is lower than the surrounding AST context (e.g.,ortemplate result placed inside anotparent)Template.apply()into three explicit phases: placeholder substitution → precedence-based parenthesization → coordinate applicationParenthesizeVisitor.maybeParenthesizecalled fromJavaTemplate.doApply()Test plan
TestMaybeParenthesizecovering binary-in-binary, unary-under-not, no-parens-needed, non-expression passthrough, and fullTemplate.apply()integration