Skip to content

Python: Unwrap ExpressionStatement around block-level template placeholders#6838

Merged
knutwannheden merged 1 commit intomainfrom
python-replacement-fix
Feb 27, 2026
Merged

Python: Unwrap ExpressionStatement around block-level template placeholders#6838
knutwannheden merged 1 commit intomainfrom
python-replacement-fix

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Summary

  • When a template has a placeholder in statement position (e.g. the body of a with block), the parser wraps it as ExpressionStatement(Identifier). If the replacement is a non-Expression statement (return, if, for, etc.), the visitor must substitute it directly in the block before the default visitor runs, so visit_expression_statement never tries to cast the replacement to Expression.
  • Mirrors the JS template engine's visitBlock logic.
  • Adds test in test_replacement.py verifying the fix.

Test plan

  • Unit test test_replacement.py passes

…olders

When a template has a placeholder in statement position (e.g. the body
of a `with` block), the parser wraps it as ExpressionStatement(Identifier).
If the replacement is a non-Expression statement (return, if, for, etc.),
the visitor must substitute it directly in the block before the default
visitor runs, so visit_expression_statement never tries to cast the
replacement to Expression. This mirrors the JS template engine's
visitBlock logic.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Feb 27, 2026
@knutwannheden knutwannheden changed the title Python: Unwrap ExpressionStatement around block-level template placeholders Python: Unwrap ExpressionStatement around block-level template placeholders Feb 27, 2026
@knutwannheden knutwannheden merged commit f03fa74 into main Feb 27, 2026
1 check passed
@knutwannheden knutwannheden deleted the python-replacement-fix branch February 27, 2026 07:42
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant