Skip to content
Myr

More closely match python splatting for calls

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In Python, it's legal to use splats in function calls like this: call_function('123', *b, c=5, **d) Prior to this change, jinja2 did not allow that. It mandated that all splatting had to take place at the end of the call: call_function('123', c=5, *b, **d) This commit allows both orders. Additionally, this commit adds tests for more things that are illegal: namely giving a function a positional argument after using a splat to unpack positional arguments or giving a function a named argument after using a double splat to unpack keyword arguments.

authored by Toshio Kuratomi · committed by Toshio Kuratomi · parent 96cd53d · Browse files · 2 files+11 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+11 added−4 removed

  • tests:+7 added−2 removed
  • other:+4 added−2 removed

2 files+11 added−4 removed

modified:jinja2/parser.py+4 added-2 removed
775 unmodified lines
776 ensure(dyn_kwargs is None)777 next(self.stream)778 dyn_kwargs = self.parse_expression()779 else:780 ensure(dyn_args is None and dyn_kwargs is None)780 if self.stream.current.type == 'name' and \781 self.stream.look().type == 'assign':782 # Parsing a kwarg783 ensure(dyn_kwargs is None)784 key = self.stream.current.value785 self.stream.skip(2)786 value = self.parse_expression()787 kwargs.append(nodes.Keyword(key, value,788 lineno=value.lineno))789 else:789 ensure(not kwargs)790 # Parsing an arg791 ensure(dyn_args is None and dyn_kwargs is None and not kwargs)792 args.append(self.parse_expression())793794 require_comma = True795 self.stream.expect('rparen')
110 unmodified lines
modified:tests/test_lexnparse.py+7 added-2 removed
359 unmodified lines
360 def test_function_calls(self, env):361 tests = [362 (True, '*foo, bar'),363 (True, '*foo, *bar'),364 (True, '*foo, bar=42'),364 (True, '**foo, *bar'),365 (True, '**foo, bar'),366 (True, '**foo, **bar'),367 (True, '**foo, bar=42'),368 (False, 'foo, bar'),369 (False, 'foo, bar=42'),370 (False, 'foo, bar=23, *args'),371 (False, 'foo, *args, bar=23'),372 (False, 'a, b=c, *d, **e'),371 (False, '*foo, **bar')373 (False, '*foo, bar=42'),374 (False, '*foo, **bar'),375 (False, '*foo, bar=42, **baz'),376 (False, 'foo, *args, bar=23, **baz'),377 ]378 for should_fail, sig in tests:379 if should_fail:380 pytest.raises(TemplateSyntaxError,
265 unmodified lines