Do not report names assigned by every if branch as undeclared - #2237
Closed
rulfox wants to merge 1 commit into
Closed
Do not report names assigned by every if branch as undeclared#2237rulfox wants to merge 1 commit into
rulfox wants to merge 1 commit into
Conversation
`meta.find_undeclared_variables` reported a name that is `{% set %}` in
every branch of an `{% if %}` chain ending in `{% else %}`, even though
such a name is always defined afterwards and never read from the context.
The optimisation that recognised this was removed in pallets#1665 because it
also skipped the load when a branch read the name before assigning it,
which broke pallets#1253. Restore it with two guards: the name must be a fresh
local in every branch, and it must not be read in a nested scope, which
the frame's symbols do not see.
Track each `{% elif %}` body as its own branch as well, so `{% if %}` /
`{% else %}` without an `{% elif %}` is recognised too.
fixes pallets#2069
Co-authored-by: Claude <noreply@anthropic.com>
Member
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.
meta.find_undeclared_variablesreports a name that is{% set %}in every branch of an{% if %}chain ending in{% else %}, even though such a name is always defined afterwards and is never read from the context:Why it regressed
Symbols.branch_updateused to skip the context load for a name stored by every branch. #1665 removed that optimisation to fix #1253, because it also skipped the load when a branch read the name before assigning it.This change
Restores the optimisation behind two guards:
undefinedrather thanresolve/alias. This is the case What's wrong with this Jinja template when using {% elif %} #1253 needed.FrameSymbolVisitordoes not enter. Those reads are invisible to the frame's symbols but still read the frame's variables, soFrameSymbolVisitornow records them andvisit_Ifconsults them. Every field the compiler visits with a child frame is tracked:For.body/else_/test,Macro.body/defaults,CallBlock.body/defaults,FilterBlock.body/filter,With.body,AssignBlock.body/filter,Scope.body,Block.body,OverlayScope.context/body.visit_Ifalso tracks each{% elif %}body as its own branch, so plain{% if %}/{% else %}is recognised too. That case was reported in #2069 as failing on 3.1.4 as well.The scan of nested scopes is lazy, so a frame with no exhaustive
{% if %}chain does no extra work.Verification
elif, withoutelif, and the no-elsecontrol case) all behave as the reporter expected.for,for recursive,macro,call,filter,with,set/endset,block,extends,include,import, namespaces, tuple targets and nested chains: no rendered output changes, only the intended reduction infind_undeclared_variablesresults.Tests
tests/test_api.py— parametrizedfind_undeclared_variablescases, including the negative ones: noelse, a branch that does not assign, read-before-assign, read in aneliftest, read in a nested scope.tests/test_regression.py— rendering tests beside the existing What's wrong with this Jinja template when using {% elif %} #1253 test, covering a read in each child-frame field.tests/test_idtracking.py— symbol-level assertions for an exhaustive chain and forOverlayScope.context.Full suite: 936 passed.
ruffclean;mypyreports the same pre-existing errors asmain.fixes #2069