File tree 3 files changed +20
-10
lines changed
3 files changed +20
-10
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ Unreleased
45
45
filter. :issue: `1624 `
46
46
- Using ``set `` for multiple assignment (``a, b = 1, 2 ``) does not fail when the
47
47
target is a namespace attribute. :issue: `1413 `
48
+ - Using ``set `` in all branches of ``{% if %}{% elif %}{% else %} `` blocks
49
+ does not cause the variable to be considered initially undefined.
50
+ :issue: `1253 `
48
51
49
52
50
53
Version 3.1.4
Original file line number Diff line number Diff line change @@ -121,23 +121,20 @@ def load(self, name: str) -> None:
121
121
self ._define_ref (name , load = (VAR_LOAD_RESOLVE , name ))
122
122
123
123
def branch_update (self , branch_symbols : t .Sequence ["Symbols" ]) -> None :
124
- stores : t .Dict [str , int ] = {}
124
+ stores : t .Set [str ] = set ()
125
+
125
126
for branch in branch_symbols :
126
- for target in branch .stores :
127
- if target in self .stores :
128
- continue
129
- stores [target ] = stores .get (target , 0 ) + 1
127
+ stores .update (branch .stores )
128
+
129
+ stores .difference_update (self .stores )
130
130
131
131
for sym in branch_symbols :
132
132
self .refs .update (sym .refs )
133
133
self .loads .update (sym .loads )
134
134
self .stores .update (sym .stores )
135
135
136
- for name , branch_count in stores .items ():
137
- if branch_count == len (branch_symbols ):
138
- continue
139
-
140
- target = self .find_ref (name ) # type: ignore
136
+ for name in stores :
137
+ target = self .find_ref (name )
141
138
assert target is not None , "should not happen"
142
139
143
140
if self .parent is not None :
Original file line number Diff line number Diff line change @@ -750,6 +750,16 @@ def is_foo(ctx, s):
750
750
assert tmpl .render () == "foo"
751
751
752
752
753
+ def test_load_parameter_when_set_in_all_if_branches (env ):
754
+ tmpl = env .from_string (
755
+ "{% if True %}{{ a.b }}{% set a = 1 %}"
756
+ "{% elif False %}{% set a = 2 %}"
757
+ "{% else %}{% set a = 3 %}{% endif %}"
758
+ "{{ a }}"
759
+ )
760
+ assert tmpl .render (a = {"b" : 0 }) == "01"
761
+
762
+
753
763
@pytest .mark .parametrize ("unicode_char" , ["\N{FORM FEED} " , "\x85 " ])
754
764
def test_unicode_whitespace (env , unicode_char ):
755
765
content = "Lorem ipsum\n " + unicode_char + "\n More text"
You can’t perform that action at this time.
0 commit comments