parser.py
This file
Path
src/jinja2/parser.py
1,041 lines · 39.2 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 1why, one step back1"""Parse tokens from the lexer into nodes for the compiler."""Merge branch '3.1.x'David Lordbefore adoptionde61312
line 2why, one step back2Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 3–5why, one step back3import typing4import typing as t5update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 6–11why, one step back6from . import nodes7from .exceptions import TemplateAssertionError8from .exceptions import TemplateSyntaxError9from .lexer import describe_token10from .lexer import describe_token_expr11Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 12–13why, one step back12if t.TYPE_CHECKING:13 import typing_extensions as teMerge branch '3.1.x'David Lordbefore adoptionde61312
line 14why, one step back14Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 15–19why, one step back15 from .environment import Environment1617_ImportInclude = t.TypeVar("_ImportInclude", nodes.Import, nodes.Include)18_MacroCall = t.TypeVar("_MacroCall", nodes.Macro, nodes.CallBlock)19update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 20–37why, one step back20_statement_keywords = frozenset(21 [22 "for",23 "if",24 "block",25 "extends",26 "print",27 "macro",28 "include",29 "from",30 "import",31 "set",32 "with",33 "autoescape",34 ]35)36_compare_operators = frozenset(["eq", "ne", "lt", "lteq", "gt", "gteq"])37Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 38why, one step back38_math_nodes: dict[str, type[nodes.Expr]] = {update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 39–52why, one step back39 "add": nodes.Add,40 "sub": nodes.Sub,41 "mul": nodes.Mul,42 "div": nodes.Div,43 "floordiv": nodes.FloorDiv,44 "mod": nodes.Mod,45}464748class Parser:49 """This is the central parsing class Jinja uses. It's passed to50 extensions and can be used to parse expressions or statements.51 """52Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 53–56why, one step back53 def __init__(54 self,55 environment: "Environment",56 source: str,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
lines 57–59why, one step back57 name: str | None = None,58 filename: str | None = None,59 state: str | None = None,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 60why, one step back60 ) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 61–65why, one step back61 self.environment = environment62 self.stream = environment._tokenize(source, name, filename, state)63 self.name = name64 self.filename = filename65 self.closed = FalseUpdated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 66why, one step back66 self.extensions: dict[drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 67why, one step back67 str, t.Callable[[Parser], nodes.Node | list[nodes.Node]]Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 68why, one step back68 ] = {}update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 69–72why, one step back69 for extension in environment.iter_extensions():70 for tag in extension.tags:71 self.extensions[tag] = extension.parse72 self._last_identifier = 0Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 73–74why, one step back73 self._tag_stack: list[str] = []74 self._end_token_stack: list[tuple[str, ...]] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 75why, one step back75Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 76–78why, one step back76 def fail(77 self,78 msg: str,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 79why, one step back79 lineno: int | None = None,Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 80why, one step back80 exc: type[TemplateSyntaxError] = TemplateSyntaxError,Merge pull request #1434 from pallets/update-typingDavid Lordbefore adoptiona2e83d0
line 81why, one step back81 ) -> "te.NoReturn":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 82–89why, one step back82 """Convenience method that raises `exc` with the message, passed83 line number or last line number as well as the current name and84 filename.85 """86 if lineno is None:87 lineno = self.stream.current.lineno88 raise exc(msg, lineno, self.name, self.filename)89Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 90–91why, one step back90 def _fail_ut_eof(91 self,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 92why, one step back92 name: str | None,Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 93why, one step back93 end_token_stack: list[tuple[str, ...]],drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 94why, one step back94 lineno: int | None,Merge pull request #1434 from pallets/update-typingDavid Lordbefore adoptiona2e83d0
line 95why, one step back95 ) -> "te.NoReturn":Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 96why, one step back96 expected: set[str] = set()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 97why, one step back97 for exprs in end_token_stack:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 98why, one step back98 expected.update(map(describe_token_expr, exprs))update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 99why, one step back99 if end_token_stack:drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 100why, one step back100 currently_looking: str | None = " or ".join(update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 101–129why, one step back101 map(repr, map(describe_token_expr, end_token_stack[-1]))102 )103 else:104 currently_looking = None105106 if name is None:107 message = ["Unexpected end of template."]108 else:109 message = [f"Encountered unknown tag {name!r}."]110111 if currently_looking:112 if name is not None and name in expected:113 message.append(114 "You probably made a nesting mistake. Jinja is expecting this tag,"115 f" but currently looking for {currently_looking}."116 )117 else:118 message.append(119 f"Jinja was looking for the following tags: {currently_looking}."120 )121122 if self._tag_stack:123 message.append(124 "The innermost block that needs to be closed is"125 f" {self._tag_stack[-1]!r}."126 )127128 self.fail(" ".join(message), lineno)129drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 130why, one step back130 def fail_unknown_tag(self, name: str, lineno: int | None = None) -> "te.NoReturn":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 131–134why, one step back131 """Called if the parser encounters an unknown tag. Tries to fail132 with a human readable error message that could help to identify133 the problem.134 """Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 135why, one step back135 self._fail_ut_eof(name, self._end_token_stack, lineno)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 136why, one step back136Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 137–138why, one step back137 def fail_eof(138 self,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
lines 139–140why, one step back139 end_tokens: tuple[str, ...] | None = None,140 lineno: int | None = None,Merge pull request #1434 from pallets/update-typingDavid Lordbefore adoptiona2e83d0
line 141why, one step back141 ) -> "te.NoReturn":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 142–145why, one step back142 """Like fail_unknown_tag but for end of template situations."""143 stack = list(self._end_token_stack)144 if end_tokens is not None:145 stack.append(end_tokens)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 146why, one step back146 self._fail_ut_eof(None, stack, lineno)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 147why, one step back147drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 148why, one step back148 def is_tuple_end(self, extra_end_rules: tuple[str, ...] | None = None) -> bool:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 149–152why, one step back149 """Are we at the end of a tuple?"""150 if self.stream.current.type in ("variable_end", "block_end", "rparen"):151 return True152 elif extra_end_rules is not None:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 153why, one step back153 return self.stream.current.test_any(extra_end_rules) # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 154–155why, one step back154 return False155drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 156why, one step back156 def free_identifier(self, lineno: int | None = None) -> nodes.InternalName:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 157–160why, one step back157 """Return a new free identifier as :class:`~jinja2.nodes.InternalName`."""158 self._last_identifier += 1159 rv = object.__new__(nodes.InternalName)160 nodes.Node.__init__(rv, f"fi{self._last_identifier}", lineno=lineno)Merge branch '3.0.x'David Lordbefore adoption896a621
line 161why, one step back161 return rvupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 162why, one step back162drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 163why, one step back163 def parse_statement(self) -> nodes.Node | list[nodes.Node]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 164–171why, one step back164 """Parse a single statement."""165 token = self.stream.current166 if token.type != "name":167 self.fail("tag name expected", token.lineno)168 self._tag_stack.append(token.value)169 pop_tag = True170 try:171 if token.value in _statement_keywords:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 172–173why, one step back172 f = getattr(self, f"parse_{self.stream.current.value}")173 return f() # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 174–191why, one step back174 if token.value == "call":175 return self.parse_call_block()176 if token.value == "filter":177 return self.parse_filter_block()178 ext = self.extensions.get(token.value)179 if ext is not None:180 return ext(self)181182 # did not work out, remove the token we pushed by accident183 # from the stack so that the unknown tag fail function can184 # produce a proper error message.185 self._tag_stack.pop()186 pop_tag = False187 self.fail_unknown_tag(token.value, token.lineno)188 finally:189 if pop_tag:190 self._tag_stack.pop()191Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 192why, one step back192 def parse_statements(Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 193–194why, one step back193 self, end_tokens: tuple[str, ...], drop_needle: bool = False194 ) -> list[nodes.Node]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 195–220why, one step back195 """Parse multiple statements into a list until one of the end tokens196 is reached. This is used to parse the body of statements as it also197 parses template data if appropriate. The parser checks first if the198 current token is a colon and skips it if there is one. Then it checks199 for the block end and parses until if one of the `end_tokens` is200 reached. Per default the active token in the stream at the end of201 the call is the matched end token. If this is not wanted `drop_needle`202 can be set to `True` and the end token is removed.203 """204 # the first token may be a colon for python compatibility205 self.stream.skip_if("colon")206207 # in the future it would be possible to add whole code sections208 # by adding some sort of end of statement token and parsing those here.209 self.stream.expect("block_end")210 result = self.subparse(end_tokens)211212 # we reached the end of the template too early, the subparser213 # does not check for this, so we do that now214 if self.stream.current.type == "eof":215 self.fail_eof(end_tokens)216217 if drop_needle:218 next(self.stream)219 return result220drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 221why, one step back221 def parse_set(self) -> nodes.Assign | nodes.AssignBlock:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 222–231why, one step back222 """Parse an assign statement."""223 lineno = next(self.stream).lineno224 target = self.parse_assign_target(with_namespace=True)225 if self.stream.skip_if("assign"):226 expr = self.parse_tuple()227 return nodes.Assign(target, expr, lineno=lineno)228 filter_node = self.parse_filter(None)229 body = self.parse_statements(("name:endset",), drop_needle=True)230 return nodes.AssignBlock(target, filter_node, body, lineno=lineno)231Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 232why, one step back232 def parse_for(self) -> nodes.For:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 233–250why, one step back233 """Parse a for loop."""234 lineno = self.stream.expect("name:for").lineno235 target = self.parse_assign_target(extra_end_rules=("name:in",))236 self.stream.expect("name:in")237 iter = self.parse_tuple(238 with_condexpr=False, extra_end_rules=("name:recursive",)239 )240 test = None241 if self.stream.skip_if("name:if"):242 test = self.parse_expression()243 recursive = self.stream.skip_if("name:recursive")244 body = self.parse_statements(("name:endfor", "name:else"))245 if next(self.stream).value == "endfor":246 else_ = []247 else:248 else_ = self.parse_statements(("name:endfor",), drop_needle=True)249 return nodes.For(target, iter, body, else_, test, recursive, lineno=lineno)250Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 251why, one step back251 def parse_if(self) -> nodes.If:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 252–253why, one step back252 """Parse an if construct."""253 node = result = nodes.If(lineno=self.stream.expect("name:if").lineno)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 254why, one step back254 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 255–268why, one step back255 node.test = self.parse_tuple(with_condexpr=False)256 node.body = self.parse_statements(("name:elif", "name:else", "name:endif"))257 node.elif_ = []258 node.else_ = []259 token = next(self.stream)260 if token.test("name:elif"):261 node = nodes.If(lineno=self.stream.current.lineno)262 result.elif_.append(node)263 continue264 elif token.test("name:else"):265 result.else_ = self.parse_statements(("name:endif",), drop_needle=True)266 break267 return result268Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 269why, one step back269 def parse_with(self) -> nodes.With:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 270why, one step back270 node = nodes.With(lineno=next(self.stream).lineno)Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 271–272why, one step back271 targets: list[nodes.Expr] = []272 values: list[nodes.Expr] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 273–285why, one step back273 while self.stream.current.type != "block_end":274 if targets:275 self.stream.expect("comma")276 target = self.parse_assign_target()277 target.set_ctx("param")278 targets.append(target)279 self.stream.expect("assign")280 values.append(self.parse_expression())281 node.targets = targets282 node.values = values283 node.body = self.parse_statements(("name:endwith",), drop_needle=True)284 return node285Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 286why, one step back286 def parse_autoescape(self) -> nodes.Scope:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 287–291why, one step back287 node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)288 node.options = [nodes.Keyword("autoescape", self.parse_expression())]289 node.body = self.parse_statements(("name:endautoescape",), drop_needle=True)290 return nodes.Scope([node])291Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 292why, one step back292 def parse_block(self) -> nodes.Block:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 293–311why, one step back293 node = nodes.Block(lineno=next(self.stream).lineno)294 node.name = self.stream.expect("name").value295 node.scoped = self.stream.skip_if("name:scoped")296 node.required = self.stream.skip_if("name:required")297298 # common problem people encounter when switching from django299 # to jinja. we do not support hyphens in block names, so let's300 # raise a nicer error message in that case.301 if self.stream.current.type == "sub":302 self.fail(303 "Block names in Jinja have to be valid Python identifiers and may not"304 " contain hyphens, use an underscore instead."305 )306307 node.body = self.parse_statements(("name:endblock",), drop_needle=True)308309 # enforce that required blocks only contain whitespace or comments310 # by asserting that the body, if not empty, is just TemplateData nodes311 # with whitespace dataMerge branch '3.1.x'David Lordbefore adoption81a2384
lines 312–319why, one step back312 if node.required:313 for body_node in node.body:314 if not isinstance(body_node, nodes.Output) or any(315 not isinstance(output_node, nodes.TemplateData)316 or not output_node.data.isspace()317 for output_node in body_node.nodes318 ):319 self.fail("Required blocks can only contain comments or whitespace")update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 320–323why, one step back320321 self.stream.skip_if("name:" + node.name)322 return node323Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 324why, one step back324 def parse_extends(self) -> nodes.Extends:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 325–328why, one step back325 node = nodes.Extends(lineno=next(self.stream).lineno)326 node.template = self.parse_expression()327 return node328Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 329–331why, one step back329 def parse_import_context(330 self, node: _ImportInclude, default: bool331 ) -> _ImportInclude:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 332–340why, one step back332 if self.stream.current.test_any(333 "name:with", "name:without"334 ) and self.stream.look().test("name:context"):335 node.with_context = next(self.stream).value == "with"336 self.stream.skip()337 else:338 node.with_context = default339 return node340Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 341why, one step back341 def parse_include(self) -> nodes.Include:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 342–352why, one step back342 node = nodes.Include(lineno=next(self.stream).lineno)343 node.template = self.parse_expression()344 if self.stream.current.test("name:ignore") and self.stream.look().test(345 "name:missing"346 ):347 node.ignore_missing = True348 self.stream.skip(2)349 else:350 node.ignore_missing = False351 return self.parse_import_context(node, True)352Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 353why, one step back353 def parse_import(self) -> nodes.Import:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 354–359why, one step back354 node = nodes.Import(lineno=next(self.stream).lineno)355 node.template = self.parse_expression()356 self.stream.expect("name:as")357 node.target = self.parse_assign_target(name_only=True).name358 return self.parse_import_context(node, False)359Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 360why, one step back360 def parse_from(self) -> nodes.FromImport:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 361–365why, one step back361 node = nodes.FromImport(lineno=next(self.stream).lineno)362 node.template = self.parse_expression()363 self.stream.expect("name:import")364 node.names = []365Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 366why, one step back366 def parse_context() -> bool:Merge branch '3.0.x'David Lordbefore adoption80e6c4b
lines 367–370why, one step back367 if self.stream.current.value in {368 "with",369 "without",370 } and self.stream.look().test("name:context"):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 371–375why, one step back371 node.with_context = next(self.stream).value == "with"372 self.stream.skip()373 return True374 return False375Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 376why, one step back376 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 377–401why, one step back377 if node.names:378 self.stream.expect("comma")379 if self.stream.current.type == "name":380 if parse_context():381 break382 target = self.parse_assign_target(name_only=True)383 if target.name.startswith("_"):384 self.fail(385 "names starting with an underline can not be imported",386 target.lineno,387 exc=TemplateAssertionError,388 )389 if self.stream.skip_if("name:as"):390 alias = self.parse_assign_target(name_only=True)391 node.names.append((target.name, alias.name))392 else:393 node.names.append(target.name)394 if parse_context() or self.stream.current.type != "comma":395 break396 else:397 self.stream.expect("name")398 if not hasattr(node, "with_context"):399 node.with_context = False400 return node401Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 402–404why, one step back402 def parse_signature(self, node: _MacroCall) -> None:403 args = node.args = []404 defaults = node.defaults = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 405–417why, one step back405 self.stream.expect("lparen")406 while self.stream.current.type != "rparen":407 if args:408 self.stream.expect("comma")409 arg = self.parse_assign_target(name_only=True)410 arg.set_ctx("param")411 if self.stream.skip_if("assign"):412 defaults.append(self.parse_expression())413 elif defaults:414 self.fail("non-default argument follows default argument")415 args.append(arg)416 self.stream.expect("rparen")417Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 418why, one step back418 def parse_call_block(self) -> nodes.CallBlock:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 419–425why, one step back419 node = nodes.CallBlock(lineno=next(self.stream).lineno)420 if self.stream.current.type == "lparen":421 self.parse_signature(node)422 else:423 node.args = []424 node.defaults = []425Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 426–427why, one step back426 call_node = self.parse_expression()427 if not isinstance(call_node, nodes.Call):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 428why, one step back428 self.fail("expected call", node.lineno)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 429why, one step back429 node.call = call_nodeupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 430–432why, one step back430 node.body = self.parse_statements(("name:endcall",), drop_needle=True)431 return node432Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 433why, one step back433 def parse_filter_block(self) -> nodes.FilterBlock:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 434why, one step back434 node = nodes.FilterBlock(lineno=next(self.stream).lineno)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 435why, one step back435 node.filter = self.parse_filter(None, start_inline=True) # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 436–438why, one step back436 node.body = self.parse_statements(("name:endfilter",), drop_needle=True)437 return node438Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 439why, one step back439 def parse_macro(self) -> nodes.Macro:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 440–445why, one step back440 node = nodes.Macro(lineno=next(self.stream).lineno)441 node.name = self.parse_assign_target(name_only=True).name442 self.parse_signature(node)443 node.body = self.parse_statements(("name:endmacro",), drop_needle=True)444 return node445Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 446why, one step back446 def parse_print(self) -> nodes.Output:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 447–454why, one step back447 node = nodes.Output(lineno=next(self.stream).lineno)448 node.nodes = []449 while self.stream.current.type != "block_end":450 if node.nodes:451 self.stream.expect("comma")452 node.nodes.append(self.parse_expression())453 return node454Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 455why, one step back455 @typing.overloadupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 456why, one step back456 def parse_assign_target(Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 457why, one step back457 self, with_tuple: bool = ..., name_only: "te.Literal[True]" = ...Merge branch '3.1.x'David Lordbefore adoptionde61312
line 458why, one step back458 ) -> nodes.Name: ...Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 459–461why, one step back459460 @typing.overload461 def parse_assign_target(update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 462why, one step back462 self,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 463–464why, one step back463 with_tuple: bool = True,464 name_only: bool = False,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 465why, one step back465 extra_end_rules: tuple[str, ...] | None = None,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 466why, one step back466 with_namespace: bool = False,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 467why, one step back467 ) -> nodes.NSRef | nodes.Name | nodes.Tuple: ...Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 468–472why, one step back468469 def parse_assign_target(470 self,471 with_tuple: bool = True,472 name_only: bool = False,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 473why, one step back473 extra_end_rules: tuple[str, ...] | None = None,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 474why, one step back474 with_namespace: bool = False,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 475why, one step back475 ) -> nodes.NSRef | nodes.Name | nodes.Tuple:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 476–483why, one step back476 """Parse an assignment target. As Jinja allows assignments to477 tuples, this function can parse all allowed assignment targets. Per478 default assignments to tuples are parsed, that can be disable however479 by setting `with_tuple` to `False`. If only assignments to names are480 wanted `name_only` can be set to `True`. The `extra_end_rules`481 parameter is forwarded to the tuple parsing function. If482 `with_namespace` is enabled, a namespace assignment may be parsed.483 """Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 484–485why, one step back484 target: nodes.Expr485Merge branch 'stable'David Lordbefore adoption6aeab5d
line 486why, one step back486 if name_only:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 487–491why, one step back487 token = self.stream.expect("name")488 target = nodes.Name(token.value, "store", lineno=token.lineno)489 else:490 if with_tuple:491 target = self.parse_tuple(Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 492–494why, one step back492 simplified=True,493 extra_end_rules=extra_end_rules,494 with_namespace=with_namespace,update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 495–496why, one step back495 )496 else:Merge branch 'stable'David Lordbefore adoption6aeab5d
line 497why, one step back497 target = self.parse_primary(with_namespace=with_namespace)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 498why, one step back498update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 499why, one step back499 target.set_ctx("store")Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 500why, one step back500update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 501–502why, one step back501 if not target.can_assign():502 self.fail(Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 503why, one step back503 f"can't assign to {type(target).__name__.lower()!r}", target.linenoupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 504–505why, one step back504 )505Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 506–508why, one step back506 return target # type: ignore507508 def parse_expression(self, with_condexpr: bool = True) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 509–516why, one step back509 """Parse an expression. Per default all expressions are parsed, if510 the optional `with_condexpr` parameter is set to `False` conditional511 expressions are not parsed.512 """513 if with_condexpr:514 return self.parse_condexpr()515 return self.parse_or()516Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 517why, one step back517 def parse_condexpr(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 518–519why, one step back518 lineno = self.stream.current.lineno519 expr1 = self.parse_or()drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 520why, one step back520 expr3: nodes.Expr | NoneMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 521why, one step back521update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 522–531why, one step back522 while self.stream.skip_if("name:if"):523 expr2 = self.parse_or()524 if self.stream.skip_if("name:else"):525 expr3 = self.parse_condexpr()526 else:527 expr3 = None528 expr1 = nodes.CondExpr(expr2, expr1, expr3, lineno=lineno)529 lineno = self.stream.current.lineno530 return expr1531Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 532why, one step back532 def parse_or(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 533–540why, one step back533 lineno = self.stream.current.lineno534 left = self.parse_and()535 while self.stream.skip_if("name:or"):536 right = self.parse_and()537 left = nodes.Or(left, right, lineno=lineno)538 lineno = self.stream.current.lineno539 return left540Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 541why, one step back541 def parse_and(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 542–549why, one step back542 lineno = self.stream.current.lineno543 left = self.parse_not()544 while self.stream.skip_if("name:and"):545 right = self.parse_not()546 left = nodes.And(left, right, lineno=lineno)547 lineno = self.stream.current.lineno548 return left549Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 550why, one step back550 def parse_not(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 551–555why, one step back551 if self.stream.current.test("name:not"):552 lineno = next(self.stream).lineno553 return nodes.Not(self.parse_not(), lineno=lineno)554 return self.parse_compare()555Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 556why, one step back556 def parse_compare(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 557–559why, one step back557 lineno = self.stream.current.lineno558 expr = self.parse_math1()559 ops = []Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 560why, one step back560 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 561–578why, one step back561 token_type = self.stream.current.type562 if token_type in _compare_operators:563 next(self.stream)564 ops.append(nodes.Operand(token_type, self.parse_math1()))565 elif self.stream.skip_if("name:in"):566 ops.append(nodes.Operand("in", self.parse_math1()))567 elif self.stream.current.test("name:not") and self.stream.look().test(568 "name:in"569 ):570 self.stream.skip(2)571 ops.append(nodes.Operand("notin", self.parse_math1()))572 else:573 break574 lineno = self.stream.current.lineno575 if not ops:576 return expr577 return nodes.Compare(expr, ops, lineno=lineno)578Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 579why, one step back579 def parse_math1(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 580–589why, one step back580 lineno = self.stream.current.lineno581 left = self.parse_concat()582 while self.stream.current.type in ("add", "sub"):583 cls = _math_nodes[self.stream.current.type]584 next(self.stream)585 right = self.parse_concat()586 left = cls(left, right, lineno=lineno)587 lineno = self.stream.current.lineno588 return left589Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 590why, one step back590 def parse_concat(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 591–599why, one step back591 lineno = self.stream.current.lineno592 args = [self.parse_math2()]593 while self.stream.current.type == "tilde":594 next(self.stream)595 args.append(self.parse_math2())596 if len(args) == 1:597 return args[0]598 return nodes.Concat(args, lineno=lineno)599Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 600why, one step back600 def parse_math2(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 601–610why, one step back601 lineno = self.stream.current.lineno602 left = self.parse_pow()603 while self.stream.current.type in ("mul", "div", "floordiv", "mod"):604 cls = _math_nodes[self.stream.current.type]605 next(self.stream)606 right = self.parse_pow()607 left = cls(left, right, lineno=lineno)608 lineno = self.stream.current.lineno609 return left610Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 611why, one step back611 def parse_pow(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 612–620why, one step back612 lineno = self.stream.current.lineno613 left = self.parse_unary()614 while self.stream.current.type == "pow":615 next(self.stream)616 right = self.parse_unary()617 left = nodes.Pow(left, right, lineno=lineno)618 lineno = self.stream.current.lineno619 return left620Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 621why, one step back621 def parse_unary(self, with_filter: bool = True) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 622–623why, one step back622 token_type = self.stream.current.type623 lineno = self.stream.current.linenoMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 624–625why, one step back624 node: nodes.Expr625update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 626–638why, one step back626 if token_type == "sub":627 next(self.stream)628 node = nodes.Neg(self.parse_unary(False), lineno=lineno)629 elif token_type == "add":630 next(self.stream)631 node = nodes.Pos(self.parse_unary(False), lineno=lineno)632 else:633 node = self.parse_primary()634 node = self.parse_postfix(node)635 if with_filter:636 node = self.parse_filter_expr(node)637 return node638Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 639–641why, one step back639 def parse_primary(self, with_namespace: bool = False) -> nodes.Expr:640 """Parse a name or literal value. If ``with_namespace`` is enabled, also641 parse namespace attr refs, for use in assignments."""update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 642why, one step back642 token = self.stream.currentMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 643why, one step back643 node: nodes.Exprupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 644why, one step back644 if token.type == "name":Merge branch 'stable'David Lordbefore adoption6aeab5d
line 645why, one step back645 next(self.stream)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 646–649why, one step back646 if token.value in ("true", "false", "True", "False"):647 node = nodes.Const(token.value in ("true", "True"), lineno=token.lineno)648 elif token.value in ("none", "None"):649 node = nodes.Const(None, lineno=token.lineno)Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 650–655why, one step back650 elif with_namespace and self.stream.current.type == "dot":651 # If namespace attributes are allowed at this point, and the next652 # token is a dot, produce a namespace reference.653 next(self.stream)654 attr = self.stream.expect("name")655 node = nodes.NSRef(token.value, attr.value, lineno=token.lineno)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 656–682why, one step back656 else:657 node = nodes.Name(token.value, "load", lineno=token.lineno)658 elif token.type == "string":659 next(self.stream)660 buf = [token.value]661 lineno = token.lineno662 while self.stream.current.type == "string":663 buf.append(self.stream.current.value)664 next(self.stream)665 node = nodes.Const("".join(buf), lineno=lineno)666 elif token.type in ("integer", "float"):667 next(self.stream)668 node = nodes.Const(token.value, lineno=token.lineno)669 elif token.type == "lparen":670 next(self.stream)671 node = self.parse_tuple(explicit_parentheses=True)672 self.stream.expect("rparen")673 elif token.type == "lbracket":674 node = self.parse_list()675 elif token.type == "lbrace":676 node = self.parse_dict()677 else:678 self.fail(f"unexpected {describe_token(token)!r}", token.lineno)679 return node680681 def parse_tuple(682 self,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 683–684why, one step back683 simplified: bool = False,684 with_condexpr: bool = True,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 685why, one step back685 extra_end_rules: tuple[str, ...] | None = None,Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 686why, one step back686 explicit_parentheses: bool = False,Merge branch 'stable'David Lordbefore adoption6aeab5d
line 687why, one step back687 with_namespace: bool = False,drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 688why, one step back688 ) -> nodes.Tuple | nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 689–694why, one step back689 """Works like `parse_expression` but if multiple expressions are690 delimited by a comma a :class:`~jinja2.nodes.Tuple` node is created.691 This method could also return a regular expression instead of a tuple692 if no commas where found.693694 The default parsing mode is a full tuple. If `simplified` is `True`Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 695–697why, one step back695 only names and literals are parsed; ``with_namespace`` allows namespace696 attr refs as well. The `no_condexpr` parameter is forwarded to697 :meth:`parse_expression`.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 698–709why, one step back698699 Because tuples do not require delimiters and may end in a bogus comma700 an extra hint is needed that marks the end of a tuple. For example701 for loops support tuples between `for` and `in`. In that case the702 `extra_end_rules` is set to ``['name:in']``.703704 `explicit_parentheses` is true if the parsing was triggered by an705 expression in parentheses. This is used to figure out if an empty706 tuple is a valid expression or not.707 """708 lineno = self.stream.current.lineno709 if simplified:Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 710–713why, one step back710711 def parse() -> nodes.Expr:712 return self.parse_primary(with_namespace=with_namespace)713update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 714–715why, one step back714 else:715Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 716why, one step back716 def parse() -> nodes.Expr:Merge branch 'stable'David Lordbefore adoption6aeab5d
line 717why, one step back717 return self.parse_expression(with_condexpr=with_condexpr)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 718why, one step back718Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 719why, one step back719 args: list[nodes.Expr] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 720why, one step back720 is_tuple = FalseMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 721–722why, one step back721722 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 723–749why, one step back723 if args:724 self.stream.expect("comma")725 if self.is_tuple_end(extra_end_rules):726 break727 args.append(parse())728 if self.stream.current.type == "comma":729 is_tuple = True730 else:731 break732 lineno = self.stream.current.lineno733734 if not is_tuple:735 if args:736 return args[0]737738 # if we don't have explicit parentheses, an empty tuple is739 # not a valid expression. This would mean nothing (literally740 # nothing) in the spot of an expression would be an empty741 # tuple.742 if not explicit_parentheses:743 self.fail(744 "Expected an expression,"745 f" got {describe_token(self.stream.current)!r}"746 )747748 return nodes.Tuple(args, "load", lineno=lineno)749Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 750why, one step back750 def parse_list(self) -> nodes.List:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 751why, one step back751 token = self.stream.expect("lbracket")Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 752why, one step back752 items: list[nodes.Expr] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 753–761why, one step back753 while self.stream.current.type != "rbracket":754 if items:755 self.stream.expect("comma")756 if self.stream.current.type == "rbracket":757 break758 items.append(self.parse_expression())759 self.stream.expect("rbracket")760 return nodes.List(items, lineno=token.lineno)761Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 762why, one step back762 def parse_dict(self) -> nodes.Dict:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 763why, one step back763 token = self.stream.expect("lbrace")Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 764why, one step back764 items: list[nodes.Pair] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 765–776why, one step back765 while self.stream.current.type != "rbrace":766 if items:767 self.stream.expect("comma")768 if self.stream.current.type == "rbrace":769 break770 key = self.parse_expression()771 self.stream.expect("colon")772 value = self.parse_expression()773 items.append(nodes.Pair(key, value, lineno=key.lineno))774 self.stream.expect("rbrace")775 return nodes.Dict(items, lineno=token.lineno)776Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 777–778why, one step back777 def parse_postfix(self, node: nodes.Expr) -> nodes.Expr:778 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 779–789why, one step back779 token_type = self.stream.current.type780 if token_type == "dot" or token_type == "lbracket":781 node = self.parse_subscript(node)782 # calls are valid both after postfix expressions (getattr783 # and getitem) as well as filters and tests784 elif token_type == "lparen":785 node = self.parse_call(node)786 else:787 break788 return node789Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 790–791why, one step back790 def parse_filter_expr(self, node: nodes.Expr) -> nodes.Expr:791 while True:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 792–793why, one step back792 token_type = self.stream.current.type793 if token_type == "pipe":Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 794why, one step back794 node = self.parse_filter(node) # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 795–804why, one step back795 elif token_type == "name" and self.stream.current.value == "is":796 node = self.parse_test(node)797 # calls are valid both after postfix expressions (getattr798 # and getitem) as well as filters and tests799 elif token_type == "lparen":800 node = self.parse_call(node)801 else:802 break803 return node804drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 805why, one step back805 def parse_subscript(self, node: nodes.Expr) -> nodes.Getattr | nodes.Getitem:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 806why, one step back806 token = next(self.stream)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 807–808why, one step back807 arg: nodes.Expr808update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 809–820why, one step back809 if token.type == "dot":810 attr_token = self.stream.current811 next(self.stream)812 if attr_token.type == "name":813 return nodes.Getattr(814 node, attr_token.value, "load", lineno=token.lineno815 )816 elif attr_token.type != "integer":817 self.fail("expected name or number", attr_token.lineno)818 arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)819 return nodes.Getitem(node, arg, "load", lineno=token.lineno)820 if token.type == "lbracket":Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 821why, one step back821 args: list[nodes.Expr] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 822–833why, one step back822 while self.stream.current.type != "rbracket":823 if args:824 self.stream.expect("comma")825 args.append(self.parse_subscribed())826 self.stream.expect("rbracket")827 if len(args) == 1:828 arg = args[0]829 else:830 arg = nodes.Tuple(args, "load", lineno=token.lineno)831 return nodes.Getitem(node, arg, "load", lineno=token.lineno)832 self.fail("expected subscript expression", token.lineno)833Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 834why, one step back834 def parse_subscribed(self) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 835why, one step back835 lineno = self.stream.current.linenodrop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 836why, one step back836 args: list[nodes.Expr | None]update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 837–863why, one step back837838 if self.stream.current.type == "colon":839 next(self.stream)840 args = [None]841 else:842 node = self.parse_expression()843 if self.stream.current.type != "colon":844 return node845 next(self.stream)846 args = [node]847848 if self.stream.current.type == "colon":849 args.append(None)850 elif self.stream.current.type not in ("rbracket", "comma"):851 args.append(self.parse_expression())852 else:853 args.append(None)854855 if self.stream.current.type == "colon":856 next(self.stream)857 if self.stream.current.type not in ("rbracket", "comma"):858 args.append(self.parse_expression())859 else:860 args.append(None)861 else:862 args.append(None)863Merge branch '3.1.x'David Lordbefore adoption3fd91e4
line 864why, one step back864 return nodes.Slice(lineno=lineno, *args) # noqa: B026update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 865why, one step back865switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81
lines 866–867why, one step back866 def parse_call_args(867 self,Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 868–870why, one step back868 ) -> tuple[869 list[nodes.Expr],870 list[nodes.Keyword],drop end of life python versions (#2096)David Lordbefore adoption49c48a0
lines 871–872why, one step back871 nodes.Expr | None,872 nodes.Expr | None,switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81
line 873why, one step back873 ]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 874–876why, one step back874 token = self.stream.expect("lparen")875 args = []876 kwargs = []Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 877–878why, one step back877 dyn_args = None878 dyn_kwargs = Noneupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 879–880why, one step back879 require_comma = False880Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 881why, one step back881 def ensure(expr: bool) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 882–887why, one step back882 if not expr:883 self.fail("invalid syntax for function call expression", token.lineno)884885 while self.stream.current.type != "rparen":886 if require_comma:887 self.stream.expect("comma")Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 888why, one step back888update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 889–891why, one step back889 # support for trailing comma890 if self.stream.current.type == "rparen":891 breakMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 892why, one step back892update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 893–917why, one step back893 if self.stream.current.type == "mul":894 ensure(dyn_args is None and dyn_kwargs is None)895 next(self.stream)896 dyn_args = self.parse_expression()897 elif self.stream.current.type == "pow":898 ensure(dyn_kwargs is None)899 next(self.stream)900 dyn_kwargs = self.parse_expression()901 else:902 if (903 self.stream.current.type == "name"904 and self.stream.look().type == "assign"905 ):906 # Parsing a kwarg907 ensure(dyn_kwargs is None)908 key = self.stream.current.value909 self.stream.skip(2)910 value = self.parse_expression()911 kwargs.append(nodes.Keyword(key, value, lineno=value.lineno))912 else:913 # Parsing an arg914 ensure(dyn_args is None and dyn_kwargs is None and not kwargs)915 args.append(self.parse_expression())916917 require_comma = TrueMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 918why, one step back918update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 919why, one step back919 self.stream.expect("rparen")Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 920why, one step back920 return args, kwargs, dyn_args, dyn_kwargsupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 921why, one step back921Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 922–926why, one step back922 def parse_call(self, node: nodes.Expr) -> nodes.Call:923 # The lparen will be expected in parse_call_args, but the lineno924 # needs to be recorded before the stream is advanced.925 token = self.stream.current926 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 927–928why, one step back927 return nodes.Call(node, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno)928Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 929why, one step back929 def parse_filter(drop end of life python versions (#2096)David Lordbefore adoption49c48a0
lines 930–931why, one step back930 self, node: nodes.Expr | None, start_inline: bool = False931 ) -> nodes.Expr | None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 932–940why, one step back932 while self.stream.current.type == "pipe" or start_inline:933 if not start_inline:934 next(self.stream)935 token = self.stream.expect("name")936 name = token.value937 while self.stream.current.type == "dot":938 next(self.stream)939 name += "." + self.stream.expect("name").value940 if self.stream.current.type == "lparen":Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 941why, one step back941 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 942–951why, one step back942 else:943 args = []944 kwargs = []945 dyn_args = dyn_kwargs = None946 node = nodes.Filter(947 node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno948 )949 start_inline = False950 return node951Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 952why, one step back952 def parse_test(self, node: nodes.Expr) -> nodes.Expr:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 953–963why, one step back953 token = next(self.stream)954 if self.stream.current.test("name:not"):955 next(self.stream)956 negated = True957 else:958 negated = False959 name = self.stream.expect("name").value960 while self.stream.current.type == "dot":961 next(self.stream)962 name += "." + self.stream.expect("name").value963 dyn_args = dyn_kwargs = NoneUpdated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 964why, one step back964 kwargs: list[nodes.Keyword] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 965why, one step back965 if self.stream.current.type == "lparen":Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 966why, one step back966 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()Merge branch '3.0.x'David Lordbefore adoption80e6c4b
lines 967–975why, one step back967 elif self.stream.current.type in {968 "name",969 "string",970 "integer",971 "float",972 "lparen",973 "lbracket",974 "lbrace",975 } and not self.stream.current.test_any("name:else", "name:or", "name:and"):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 976–989why, one step back976 if self.stream.current.test("name:is"):977 self.fail("You cannot chain multiple tests with is")978 arg_node = self.parse_primary()979 arg_node = self.parse_postfix(arg_node)980 args = [arg_node]981 else:982 args = []983 node = nodes.Test(984 node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno985 )986 if negated:987 node = nodes.Not(node, lineno=token.lineno)988 return node989drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 990why, one step back990 def subparse(self, end_tokens: tuple[str, ...] | None = None) -> list[nodes.Node]:Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 991–992why, one step back991 body: list[nodes.Node] = []992 data_buffer: list[nodes.Node] = []update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 993–997why, one step back993 add_data = data_buffer.append994995 if end_tokens is not None:996 self._end_token_stack.append(end_tokens)997Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 998why, one step back998 def flush_data() -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 999–1036why, one step back999 if data_buffer:1000 lineno = data_buffer[0].lineno1001 body.append(nodes.Output(data_buffer[:], lineno=lineno))1002 del data_buffer[:]10031004 try:1005 while self.stream:1006 token = self.stream.current1007 if token.type == "data":1008 if token.value:1009 add_data(nodes.TemplateData(token.value, lineno=token.lineno))1010 next(self.stream)1011 elif token.type == "variable_begin":1012 next(self.stream)1013 add_data(self.parse_tuple(with_condexpr=True))1014 self.stream.expect("variable_end")1015 elif token.type == "block_begin":1016 flush_data()1017 next(self.stream)1018 if end_tokens is not None and self.stream.current.test_any(1019 *end_tokens1020 ):1021 return body1022 rv = self.parse_statement()1023 if isinstance(rv, list):1024 body.extend(rv)1025 else:1026 body.append(rv)1027 self.stream.expect("block_end")1028 else:1029 raise AssertionError("internal parsing error")10301031 flush_data()1032 finally:1033 if end_tokens is not None:1034 self._end_token_stack.pop()1035 return body1036Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 1037why, one step back1037 def parse(self) -> nodes.Template:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 1038–1041why, one step back1038 """Parse the whole template into a `Template` node."""1039 result = nodes.Template(self.subparse(), lineno=1)1040 result.set_environment(self.environment)1041 return result