Skip to content
Myr

Merge pull request #618 from jackwilsdon/stricter-from-parsing

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

Add stricter checking to "from ... import ..."

authored by David Lord · committed by GitHub · parents 2c61df1, 32afe63 · Browse files · 3 files+23 added−3 removed
This commit

State

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

Diff stats

3 files+23 added−3 removed

  • tests:+20 added−2 removed
  • other:+3 added−1 removed

3 files+23 added−3 removed

modified:jinja2/parser.py+1 added-1 removed
332 unmodified lines
333 node.names.append(target.name)334 if parse_context() or self.stream.current.type != 'comma':335 break336 else:337 break337 self.stream.expect('name')338 if not hasattr(node, 'with_context'):339 node.with_context = False340 self.stream.skip_if('comma')341 return node
563 unmodified lines
modified:tests/test_imports.py+20 added-2 removed
9 unmodified lines
10"""11import pytest1213from jinja2 import Environment, DictLoader14from jinja2.exceptions import TemplateNotFound, TemplatesNotFound14from jinja2.exceptions import TemplateNotFound, TemplatesNotFound, \15 TemplateSyntaxError161718@pytest.fixture19def test_env():
30 unmodified lines
50 '{% from "module" import test with context %}{{ test() }}'51 )52 assert t.render(foo=42) == '[42|23]'5353 def test_trailing_comma(self, test_env):54 def test_import_needs_name(self, test_env):55 test_env.from_string('{% from "foo" import bar %}')56 test_env.from_string('{% from "foo" import bar, baz %}')5758 with pytest.raises(TemplateSyntaxError):59 test_env.from_string('{% from "foo" import %}')6061 def test_no_trailing_comma(self, test_env):62 with pytest.raises(TemplateSyntaxError):63 test_env.from_string('{% from "foo" import bar, %}')6465 with pytest.raises(TemplateSyntaxError):66 test_env.from_string('{% from "foo" import bar,, %}')6768 with pytest.raises(TemplateSyntaxError):69 test_env.from_string('{% from "foo" import, %}')7071 def test_trailing_comma_with_context(self, test_env):72 test_env.from_string('{% from "foo" import bar, baz with context %}')73 test_env.from_string('{% from "foo" import bar, baz, with context %}')74 test_env.from_string('{% from "foo" import bar, with context %}')75 test_env.from_string('{% from "foo" import bar, with, context %}')
99 unmodified lines
modified:CHANGES+2 added
26 unmodified lines
27- Add a ``unique`` filter. (`#469`_)28- Add ``min`` and ``max`` filters. (`#475`_)29- Add tests for all comparison operators: ``eq``, ``ne``, ``lt``, ``le``,30 ``gt``, ``ge``. (`#665`_)31- ``import`` statement cannot end with a trailing comma. (`#618`_)3233.. _#469: https://github.com/pallets/jinja/pull/46934.. _#475: https://github.com/pallets/jinja/pull/47535.. _#478: https://github.com/pallets/jinja/pull/47836.. _#618: https://github.com/pallets/jinja/pull/61837.. _#665: https://github.com/pallets/jinja/pull/6653839Version 2.9.640-------------
505 unmodified lines