fix: suppress PrecedenceWarning when nullish fallback is parenthesized#96
Merged
fix: suppress PrecedenceWarning when nullish fallback is parenthesized#96
Conversation
`x ?? (y | filter)` already disambiguates intent via explicit parens, but the compiler warned anyway because the AST didn't track grouping. Add a `parenthesized` flag to `Filter` nodes, set it in the parser when returning from `(expr)`, and skip the warning when the flag is set. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a false-positive PrecedenceWarning emitted for null-coalescing (??) expressions where the fallback side already disambiguates precedence via parentheses (e.g., x ?? (y | upper)).
Changes:
- Adds a
parenthesizedflag toFilterAST nodes and sets it when a filter expression is returned from a grouped/parenthesized expression. - Updates null-coalesce compilation to skip the filter/
??precedence warning when the RHSFilteris explicitly parenthesized. - Adds a regression test ensuring parenthesized fallbacks don’t warn while unparenthesized cases still do.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
tests/test_kida_modern_syntax.py |
Adds a regression test for suppressing PrecedenceWarning when the RHS filter is parenthesized. |
src/kida/parser/expressions.py |
Marks Filter nodes as parenthesized=True when parsed from grouped expressions. |
src/kida/nodes/expressions.py |
Extends the Filter AST node with a parenthesized boolean flag. |
src/kida/compiler/expressions.py |
Suppresses the ?? + ` |
| from kida.nodes.expressions import Filter | ||
|
|
||
| if isinstance(node.right, Filter): | ||
| if isinstance(node.right, Filter) and not node.right.parenthesized: |
| env.from_string("{{ x ?? fallback | upper }}") | ||
| prec_warnings = [x for x in w if issubclass(x.category, PrecedenceWarning)] | ||
| assert len(prec_warnings) == 1 | ||
|
|
4 tasks
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
PrecedenceWarningfor??+|fired even when the fallback expression was already parenthesized (e.g.,x ?? (y | upper)), producing false positivesparenthesizedflag toFilterAST nodes, set by the parser when returning from grouped expressionsTrue, while still warning on ambiguousx ?? y | filterTest plan
test_no_precedence_warning_when_fallback_parenthesizedverifies parenthesized fallback suppresses warning and unparenthesized still warns🤖 Generated with Claude Code