Python: Fix pattern matching false positives and add template auto-parenthesization#6814
Merged
knutwannheden merged 1 commit intomainfrom Feb 25, 2026
Conversation
…renthesization
Two fixes in the Python template system:
1. PatternMatchingComparator: Change default fallthrough from `return True`
to `return False` so unhandled node types reject matches instead of
silently accepting. Add explicit handlers for Empty (sentinel) and
ArrayAccess nodes.
2. PlaceholderReplacementVisitor: Auto-wrap substituted expressions in
parentheses when they have lower operator precedence than the
surrounding context. For example, `template("{a} and {b}")` with
`b = x or y` now correctly produces `a and (x or y)` instead of
`a and x or y`. Handles Binary, Python Binary, and Unary (not).
macsux
pushed a commit
that referenced
this pull request
Feb 27, 2026
…renthesization (#6814) Two fixes in the Python template system: 1. PatternMatchingComparator: Change default fallthrough from `return True` to `return False` so unhandled node types reject matches instead of silently accepting. Add explicit handlers for Empty (sentinel) and ArrayAccess nodes. 2. PlaceholderReplacementVisitor: Auto-wrap substituted expressions in parentheses when they have lower operator precedence than the surrounding context. For example, `template("{a} and {b}")` with `b = x or y` now correctly produces `a and (x or y)` instead of `a and x or y`. Handles Binary, Python Binary, and Unary (not).
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
Two fixes in the Python template system discovered while running cleanup recipes across 11 open-source Python projects:
PatternMatchingComparator: The default fallthrough for unhandled node types returned
True, causing false positive matches. For example,pattern("{x} == None")would matchx == b"hello"becauseArrayAccessnodes weren't handled and any two nodes of an unrecognized type silently matched. Changed toreturn Falseand added explicit handlers forEmpty(sentinel) andArrayAccessnodes.PlaceholderReplacementVisitor: Template substitution didn't account for operator precedence, producing semantically incorrect output. For example,
template("{a} and {b}")withbcaptured asx or ywould producea and x or yinstead ofa and (x or y). Added auto-parenthesization invisit_binary,visit_python_binary, andvisit_unarythat wraps substituted expressions when they have lower precedence than the surrounding context.Test plan
test_comparator.pyandtest_replacement.pycovering:orinand,andinand(no parens), arithmetic precedence, Pythoninoperator,notwithand/oroperands,notwith identifiers/comparisons (no parens)