Skip to content

Commit dcd0cb7

Browse files
committed
Changed lookup logic for make_attrgetter to support integers like the regular syntax
1 parent 03bea56 commit dcd0cb7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

jinja2/filters.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ def environmentfilter(f):
5454
def make_attrgetter(environment, attribute):
5555
"""Returns a callable that looks up the given attribute from a
5656
passed object with the rules of the environment. Dots are allowed
57-
to access attributes of attributes.
57+
to access attributes of attributes. Integer parts in paths are
58+
looked up as integers.
5859
"""
59-
if not isinstance(attribute, string_types) or '.' not in attribute:
60+
if not isinstance(attribute, string_types) \
61+
or ('.' not in attribute and not attribute.isdigit()):
6062
return lambda x: environment.getitem(x, attribute)
6163
attribute = attribute.split('.')
6264
def attrgetter(item):
6365
for part in attribute:
66+
if part.isdigit():
67+
part = int(part)
6468
item = environment.getitem(item, part)
6569
return item
6670
return attrgetter

jinja2/testsuite/filters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ def test_sum_attributes_nested(self):
256256
{'real': {'value': 18}},
257257
]) == '42'
258258

259+
def test_sum_attributes_tuple(self):
260+
tmpl = env.from_string('''{{ values.items()|sum('1') }}''')
261+
assert tmpl.render(values={
262+
'foo': 23,
263+
'bar': 1,
264+
'baz': 18,
265+
}) == '42'
266+
259267
def test_abs(self):
260268
tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
261269
assert tmpl.render() == '1|1', tmpl.render()

0 commit comments

Comments
 (0)