jinja2.nodes

AST nodes generated by the parser for the compiler. Also provides some node tree helper functions used by the parser and compiler in order to normalize nodes.

   1"""AST nodes generated by the parser for the compiler. Also provides
   2some node tree helper functions used by the parser and compiler in order
   3to normalize nodes.
   4"""
   5
   6import inspect
   7import operator
   8import typing as t
   9from collections import deque
  10
  11from markupsafe import Markup
  12
  13from .utils import _PassArg
  14
  15if t.TYPE_CHECKING:
  16    import typing_extensions as te
  17
  18    from .environment import Environment
  19
  20_NodeBound = t.TypeVar("_NodeBound", bound="Node")
  21
  22_binop_to_func: t.Dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
  23    "*": operator.mul,
  24    "/": operator.truediv,
  25    "//": operator.floordiv,
  26    "**": operator.pow,
  27    "%": operator.mod,
  28    "+": operator.add,
  29    "-": operator.sub,
  30}
  31
  32_uaop_to_func: t.Dict[str, t.Callable[[t.Any], t.Any]] = {
  33    "not": operator.not_,
  34    "+": operator.pos,
  35    "-": operator.neg,
  36}
  37
  38_cmpop_to_func: t.Dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
  39    "eq": operator.eq,
  40    "ne": operator.ne,
  41    "gt": operator.gt,
  42    "gteq": operator.ge,
  43    "lt": operator.lt,
  44    "lteq": operator.le,
  45    "in": lambda a, b: a in b,
  46    "notin": lambda a, b: a not in b,
  47}
  48
  49
  50class Impossible(Exception):
  51    """Raised if the node could not perform a requested action."""
  52
  53
  54class NodeType(type):
  55    """A metaclass for nodes that handles the field and attribute
  56    inheritance.  fields and attributes from the parent class are
  57    automatically forwarded to the child."""
  58
  59    def __new__(mcs, name, bases, d):  # type: ignore
  60        for attr in "fields", "attributes":
  61            storage: t.List[t.Tuple[str, ...]] = []
  62            storage.extend(getattr(bases[0] if bases else object, attr, ()))
  63            storage.extend(d.get(attr, ()))
  64            assert len(bases) <= 1, "multiple inheritance not allowed"
  65            assert len(storage) == len(set(storage)), "layout conflict"
  66            d[attr] = tuple(storage)
  67        d.setdefault("abstract", False)
  68        return type.__new__(mcs, name, bases, d)
  69
  70
  71class EvalContext:
  72    """Holds evaluation time information.  Custom attributes can be attached
  73    to it in extensions.
  74    """
  75
  76    def __init__(
  77        self, environment: "Environment", template_name: t.Optional[str] = None
  78    ) -> None:
  79        self.environment = environment
  80        if callable(environment.autoescape):
  81            self.autoescape = environment.autoescape(template_name)
  82        else:
  83            self.autoescape = environment.autoescape
  84        self.volatile = False
  85
  86    def save(self) -> t.Mapping[str, t.Any]:
  87        return self.__dict__.copy()
  88
  89    def revert(self, old: t.Mapping[str, t.Any]) -> None:
  90        self.__dict__.clear()
  91        self.__dict__.update(old)
  92
  93
  94def get_eval_context(node: "Node", ctx: t.Optional[EvalContext]) -> EvalContext:
  95    if ctx is None:
  96        if node.environment is None:
  97            raise RuntimeError(
  98                "if no eval context is passed, the node must have an"
  99                " attached environment."
 100            )
 101        return EvalContext(node.environment)
 102    return ctx
 103
 104
 105class Node(metaclass=NodeType):
 106    """Baseclass for all Jinja nodes.  There are a number of nodes available
 107    of different types.  There are four major types:
 108
 109    -   :class:`Stmt`: statements
 110    -   :class:`Expr`: expressions
 111    -   :class:`Helper`: helper nodes
 112    -   :class:`Template`: the outermost wrapper node
 113
 114    All nodes have fields and attributes.  Fields may be other nodes, lists,
 115    or arbitrary values.  Fields are passed to the constructor as regular
 116    positional arguments, attributes as keyword arguments.  Each node has
 117    two attributes: `lineno` (the line number of the node) and `environment`.
 118    The `environment` attribute is set at the end of the parsing process for
 119    all nodes automatically.
 120    """
 121
 122    fields: t.Tuple[str, ...] = ()
 123    attributes: t.Tuple[str, ...] = ("lineno", "environment")
 124    abstract = True
 125
 126    lineno: int
 127    environment: t.Optional["Environment"]
 128
 129    def __init__(self, *fields: t.Any, **attributes: t.Any) -> None:
 130        if self.abstract:
 131            raise TypeError("abstract nodes are not instantiable")
 132        if fields:
 133            if len(fields) != len(self.fields):
 134                if not self.fields:
 135                    raise TypeError(f"{type(self).__name__!r} takes 0 arguments")
 136                raise TypeError(
 137                    f"{type(self).__name__!r} takes 0 or {len(self.fields)}"
 138                    f" argument{'s' if len(self.fields) != 1 else ''}"
 139                )
 140            for name, arg in zip(self.fields, fields):
 141                setattr(self, name, arg)
 142        for attr in self.attributes:
 143            setattr(self, attr, attributes.pop(attr, None))
 144        if attributes:
 145            raise TypeError(f"unknown attribute {next(iter(attributes))!r}")
 146
 147    def iter_fields(
 148        self,
 149        exclude: t.Optional[t.Container[str]] = None,
 150        only: t.Optional[t.Container[str]] = None,
 151    ) -> t.Iterator[t.Tuple[str, t.Any]]:
 152        """This method iterates over all fields that are defined and yields
 153        ``(key, value)`` tuples.  Per default all fields are returned, but
 154        it's possible to limit that to some fields by providing the `only`
 155        parameter or to exclude some using the `exclude` parameter.  Both
 156        should be sets or tuples of field names.
 157        """
 158        for name in self.fields:
 159            if (
 160                (exclude is None and only is None)
 161                or (exclude is not None and name not in exclude)
 162                or (only is not None and name in only)
 163            ):
 164                try:
 165                    yield name, getattr(self, name)
 166                except AttributeError:
 167                    pass
 168
 169    def iter_child_nodes(
 170        self,
 171        exclude: t.Optional[t.Container[str]] = None,
 172        only: t.Optional[t.Container[str]] = None,
 173    ) -> t.Iterator["Node"]:
 174        """Iterates over all direct child nodes of the node.  This iterates
 175        over all fields and yields the values of they are nodes.  If the value
 176        of a field is a list all the nodes in that list are returned.
 177        """
 178        for _, item in self.iter_fields(exclude, only):
 179            if isinstance(item, list):
 180                for n in item:
 181                    if isinstance(n, Node):
 182                        yield n
 183            elif isinstance(item, Node):
 184                yield item
 185
 186    def find(self, node_type: t.Type[_NodeBound]) -> t.Optional[_NodeBound]:
 187        """Find the first node of a given type.  If no such node exists the
 188        return value is `None`.
 189        """
 190        for result in self.find_all(node_type):
 191            return result
 192
 193        return None
 194
 195    def find_all(
 196        self, node_type: t.Union[t.Type[_NodeBound], t.Tuple[t.Type[_NodeBound], ...]]
 197    ) -> t.Iterator[_NodeBound]:
 198        """Find all the nodes of a given type.  If the type is a tuple,
 199        the check is performed for any of the tuple items.
 200        """
 201        for child in self.iter_child_nodes():
 202            if isinstance(child, node_type):
 203                yield child  # type: ignore
 204            yield from child.find_all(node_type)
 205
 206    def set_ctx(self, ctx: str) -> "Node":
 207        """Reset the context of a node and all child nodes.  Per default the
 208        parser will all generate nodes that have a 'load' context as it's the
 209        most common one.  This method is used in the parser to set assignment
 210        targets and other nodes to a store context.
 211        """
 212        todo = deque([self])
 213        while todo:
 214            node = todo.popleft()
 215            if "ctx" in node.fields:
 216                node.ctx = ctx  # type: ignore
 217            todo.extend(node.iter_child_nodes())
 218        return self
 219
 220    def set_lineno(self, lineno: int, override: bool = False) -> "Node":
 221        """Set the line numbers of the node and children."""
 222        todo = deque([self])
 223        while todo:
 224            node = todo.popleft()
 225            if "lineno" in node.attributes:
 226                if node.lineno is None or override:
 227                    node.lineno = lineno
 228            todo.extend(node.iter_child_nodes())
 229        return self
 230
 231    def set_environment(self, environment: "Environment") -> "Node":
 232        """Set the environment for all nodes."""
 233        todo = deque([self])
 234        while todo:
 235            node = todo.popleft()
 236            node.environment = environment
 237            todo.extend(node.iter_child_nodes())
 238        return self
 239
 240    def __eq__(self, other: t.Any) -> bool:
 241        if type(self) is not type(other):
 242            return NotImplemented
 243
 244        return tuple(self.iter_fields()) == tuple(other.iter_fields())
 245
 246    __hash__ = object.__hash__
 247
 248    def __repr__(self) -> str:
 249        args_str = ", ".join(f"{a}={getattr(self, a, None)!r}" for a in self.fields)
 250        return f"{type(self).__name__}({args_str})"
 251
 252    def dump(self) -> str:
 253        def _dump(node: t.Union[Node, t.Any]) -> None:
 254            if not isinstance(node, Node):
 255                buf.append(repr(node))
 256                return
 257
 258            buf.append(f"nodes.{type(node).__name__}(")
 259            if not node.fields:
 260                buf.append(")")
 261                return
 262            for idx, field in enumerate(node.fields):
 263                if idx:
 264                    buf.append(", ")
 265                value = getattr(node, field)
 266                if isinstance(value, list):
 267                    buf.append("[")
 268                    for idx, item in enumerate(value):
 269                        if idx:
 270                            buf.append(", ")
 271                        _dump(item)
 272                    buf.append("]")
 273                else:
 274                    _dump(value)
 275            buf.append(")")
 276
 277        buf: t.List[str] = []
 278        _dump(self)
 279        return "".join(buf)
 280
 281
 282class Stmt(Node):
 283    """Base node for all statements."""
 284
 285    abstract = True
 286
 287
 288class Helper(Node):
 289    """Nodes that exist in a specific context only."""
 290
 291    abstract = True
 292
 293
 294class Template(Node):
 295    """Node that represents a template.  This must be the outermost node that
 296    is passed to the compiler.
 297    """
 298
 299    fields = ("body",)
 300    body: t.List[Node]
 301
 302
 303class Output(Stmt):
 304    """A node that holds multiple expressions which are then printed out.
 305    This is used both for the `print` statement and the regular template data.
 306    """
 307
 308    fields = ("nodes",)
 309    nodes: t.List["Expr"]
 310
 311
 312class Extends(Stmt):
 313    """Represents an extends statement."""
 314
 315    fields = ("template",)
 316    template: "Expr"
 317
 318
 319class For(Stmt):
 320    """The for loop.  `target` is the target for the iteration (usually a
 321    :class:`Name` or :class:`Tuple`), `iter` the iterable.  `body` is a list
 322    of nodes that are used as loop-body, and `else_` a list of nodes for the
 323    `else` block.  If no else node exists it has to be an empty list.
 324
 325    For filtered nodes an expression can be stored as `test`, otherwise `None`.
 326    """
 327
 328    fields = ("target", "iter", "body", "else_", "test", "recursive")
 329    target: Node
 330    iter: Node
 331    body: t.List[Node]
 332    else_: t.List[Node]
 333    test: t.Optional[Node]
 334    recursive: bool
 335
 336
 337class If(Stmt):
 338    """If `test` is true, `body` is rendered, else `else_`."""
 339
 340    fields = ("test", "body", "elif_", "else_")
 341    test: Node
 342    body: t.List[Node]
 343    elif_: t.List["If"]
 344    else_: t.List[Node]
 345
 346
 347class Macro(Stmt):
 348    """A macro definition.  `name` is the name of the macro, `args` a list of
 349    arguments and `defaults` a list of defaults if there are any.  `body` is
 350    a list of nodes for the macro body.
 351    """
 352
 353    fields = ("name", "args", "defaults", "body")
 354    name: str
 355    args: t.List["Name"]
 356    defaults: t.List["Expr"]
 357    body: t.List[Node]
 358
 359
 360class CallBlock(Stmt):
 361    """Like a macro without a name but a call instead.  `call` is called with
 362    the unnamed macro as `caller` argument this node holds.
 363    """
 364
 365    fields = ("call", "args", "defaults", "body")
 366    call: "Call"
 367    args: t.List["Name"]
 368    defaults: t.List["Expr"]
 369    body: t.List[Node]
 370
 371
 372class FilterBlock(Stmt):
 373    """Node for filter sections."""
 374
 375    fields = ("body", "filter")
 376    body: t.List[Node]
 377    filter: "Filter"
 378
 379
 380class With(Stmt):
 381    """Specific node for with statements.  In older versions of Jinja the
 382    with statement was implemented on the base of the `Scope` node instead.
 383
 384    .. versionadded:: 2.9.3
 385    """
 386
 387    fields = ("targets", "values", "body")
 388    targets: t.List["Expr"]
 389    values: t.List["Expr"]
 390    body: t.List[Node]
 391
 392
 393class Block(Stmt):
 394    """A node that represents a block.
 395
 396    .. versionchanged:: 3.0.0
 397        the `required` field was added.
 398    """
 399
 400    fields = ("name", "body", "scoped", "required")
 401    name: str
 402    body: t.List[Node]
 403    scoped: bool
 404    required: bool
 405
 406
 407class Include(Stmt):
 408    """A node that represents the include tag."""
 409
 410    fields = ("template", "with_context", "ignore_missing")
 411    template: "Expr"
 412    with_context: bool
 413    ignore_missing: bool
 414
 415
 416class Import(Stmt):
 417    """A node that represents the import tag."""
 418
 419    fields = ("template", "target", "with_context")
 420    template: "Expr"
 421    target: str
 422    with_context: bool
 423
 424
 425class FromImport(Stmt):
 426    """A node that represents the from import tag.  It's important to not
 427    pass unsafe names to the name attribute.  The compiler translates the
 428    attribute lookups directly into getattr calls and does *not* use the
 429    subscript callback of the interface.  As exported variables may not
 430    start with double underscores (which the parser asserts) this is not a
 431    problem for regular Jinja code, but if this node is used in an extension
 432    extra care must be taken.
 433
 434    The list of names may contain tuples if aliases are wanted.
 435    """
 436
 437    fields = ("template", "names", "with_context")
 438    template: "Expr"
 439    names: t.List[t.Union[str, t.Tuple[str, str]]]
 440    with_context: bool
 441
 442
 443class ExprStmt(Stmt):
 444    """A statement that evaluates an expression and discards the result."""
 445
 446    fields = ("node",)
 447    node: Node
 448
 449
 450class Assign(Stmt):
 451    """Assigns an expression to a target."""
 452
 453    fields = ("target", "node")
 454    target: "Expr"
 455    node: Node
 456
 457
 458class AssignBlock(Stmt):
 459    """Assigns a block to a target."""
 460
 461    fields = ("target", "filter", "body")
 462    target: "Expr"
 463    filter: t.Optional["Filter"]
 464    body: t.List[Node]
 465
 466
 467class Expr(Node):
 468    """Baseclass for all expressions."""
 469
 470    abstract = True
 471
 472    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 473        """Return the value of the expression as constant or raise
 474        :exc:`Impossible` if this was not possible.
 475
 476        An :class:`EvalContext` can be provided, if none is given
 477        a default context is created which requires the nodes to have
 478        an attached environment.
 479
 480        .. versionchanged:: 2.4
 481           the `eval_ctx` parameter was added.
 482        """
 483        raise Impossible()
 484
 485    def can_assign(self) -> bool:
 486        """Check if it's possible to assign something to this node."""
 487        return False
 488
 489
 490class BinExpr(Expr):
 491    """Baseclass for all binary expressions."""
 492
 493    fields = ("left", "right")
 494    left: Expr
 495    right: Expr
 496    operator: str
 497    abstract = True
 498
 499    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 500        eval_ctx = get_eval_context(self, eval_ctx)
 501
 502        # intercepted operators cannot be folded at compile time
 503        if (
 504            eval_ctx.environment.sandboxed
 505            and self.operator in eval_ctx.environment.intercepted_binops  # type: ignore
 506        ):
 507            raise Impossible()
 508        f = _binop_to_func[self.operator]
 509        try:
 510            return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
 511        except Exception as e:
 512            raise Impossible() from e
 513
 514
 515class UnaryExpr(Expr):
 516    """Baseclass for all unary expressions."""
 517
 518    fields = ("node",)
 519    node: Expr
 520    operator: str
 521    abstract = True
 522
 523    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 524        eval_ctx = get_eval_context(self, eval_ctx)
 525
 526        # intercepted operators cannot be folded at compile time
 527        if (
 528            eval_ctx.environment.sandboxed
 529            and self.operator in eval_ctx.environment.intercepted_unops  # type: ignore
 530        ):
 531            raise Impossible()
 532        f = _uaop_to_func[self.operator]
 533        try:
 534            return f(self.node.as_const(eval_ctx))
 535        except Exception as e:
 536            raise Impossible() from e
 537
 538
 539class Name(Expr):
 540    """Looks up a name or stores a value in a name.
 541    The `ctx` of the node can be one of the following values:
 542
 543    -   `store`: store a value in the name
 544    -   `load`: load that name
 545    -   `param`: like `store` but if the name was defined as function parameter.
 546    """
 547
 548    fields = ("name", "ctx")
 549    name: str
 550    ctx: str
 551
 552    def can_assign(self) -> bool:
 553        return self.name not in {"true", "false", "none", "True", "False", "None"}
 554
 555
 556class NSRef(Expr):
 557    """Reference to a namespace value assignment"""
 558
 559    fields = ("name", "attr")
 560    name: str
 561    attr: str
 562
 563    def can_assign(self) -> bool:
 564        # We don't need any special checks here; NSRef assignments have a
 565        # runtime check to ensure the target is a namespace object which will
 566        # have been checked already as it is created using a normal assignment
 567        # which goes through a `Name` node.
 568        return True
 569
 570
 571class Literal(Expr):
 572    """Baseclass for literals."""
 573
 574    abstract = True
 575
 576
 577class Const(Literal):
 578    """All constant values.  The parser will return this node for simple
 579    constants such as ``42`` or ``"foo"`` but it can be used to store more
 580    complex values such as lists too.  Only constants with a safe
 581    representation (objects where ``eval(repr(x)) == x`` is true).
 582    """
 583
 584    fields = ("value",)
 585    value: t.Any
 586
 587    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 588        return self.value
 589
 590    @classmethod
 591    def from_untrusted(
 592        cls,
 593        value: t.Any,
 594        lineno: t.Optional[int] = None,
 595        environment: "t.Optional[Environment]" = None,
 596    ) -> "Const":
 597        """Return a const object if the value is representable as
 598        constant value in the generated code, otherwise it will raise
 599        an `Impossible` exception.
 600        """
 601        from .compiler import has_safe_repr
 602
 603        if not has_safe_repr(value):
 604            raise Impossible()
 605        return cls(value, lineno=lineno, environment=environment)
 606
 607
 608class TemplateData(Literal):
 609    """A constant template string."""
 610
 611    fields = ("data",)
 612    data: str
 613
 614    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
 615        eval_ctx = get_eval_context(self, eval_ctx)
 616        if eval_ctx.volatile:
 617            raise Impossible()
 618        if eval_ctx.autoescape:
 619            return Markup(self.data)
 620        return self.data
 621
 622
 623class Tuple(Literal):
 624    """For loop unpacking and some other things like multiple arguments
 625    for subscripts.  Like for :class:`Name` `ctx` specifies if the tuple
 626    is used for loading the names or storing.
 627    """
 628
 629    fields = ("items", "ctx")
 630    items: t.List[Expr]
 631    ctx: str
 632
 633    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[t.Any, ...]:
 634        eval_ctx = get_eval_context(self, eval_ctx)
 635        return tuple(x.as_const(eval_ctx) for x in self.items)
 636
 637    def can_assign(self) -> bool:
 638        for item in self.items:
 639            if not item.can_assign():
 640                return False
 641        return True
 642
 643
 644class List(Literal):
 645    """Any list literal such as ``[1, 2, 3]``"""
 646
 647    fields = ("items",)
 648    items: t.List[Expr]
 649
 650    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.List[t.Any]:
 651        eval_ctx = get_eval_context(self, eval_ctx)
 652        return [x.as_const(eval_ctx) for x in self.items]
 653
 654
 655class Dict(Literal):
 656    """Any dict literal such as ``{1: 2, 3: 4}``.  The items must be a list of
 657    :class:`Pair` nodes.
 658    """
 659
 660    fields = ("items",)
 661    items: t.List["Pair"]
 662
 663    def as_const(
 664        self, eval_ctx: t.Optional[EvalContext] = None
 665    ) -> t.Dict[t.Any, t.Any]:
 666        eval_ctx = get_eval_context(self, eval_ctx)
 667        return dict(x.as_const(eval_ctx) for x in self.items)
 668
 669
 670class Pair(Helper):
 671    """A key, value pair for dicts."""
 672
 673    fields = ("key", "value")
 674    key: Expr
 675    value: Expr
 676
 677    def as_const(
 678        self, eval_ctx: t.Optional[EvalContext] = None
 679    ) -> t.Tuple[t.Any, t.Any]:
 680        eval_ctx = get_eval_context(self, eval_ctx)
 681        return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)
 682
 683
 684class Keyword(Helper):
 685    """A key, value pair for keyword arguments where key is a string."""
 686
 687    fields = ("key", "value")
 688    key: str
 689    value: Expr
 690
 691    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[str, t.Any]:
 692        eval_ctx = get_eval_context(self, eval_ctx)
 693        return self.key, self.value.as_const(eval_ctx)
 694
 695
 696class CondExpr(Expr):
 697    """A conditional expression (inline if expression).  (``{{
 698    foo if bar else baz }}``)
 699    """
 700
 701    fields = ("test", "expr1", "expr2")
 702    test: Expr
 703    expr1: Expr
 704    expr2: t.Optional[Expr]
 705
 706    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 707        eval_ctx = get_eval_context(self, eval_ctx)
 708        if self.test.as_const(eval_ctx):
 709            return self.expr1.as_const(eval_ctx)
 710
 711        # if we evaluate to an undefined object, we better do that at runtime
 712        if self.expr2 is None:
 713            raise Impossible()
 714
 715        return self.expr2.as_const(eval_ctx)
 716
 717
 718def args_as_const(
 719    node: t.Union["_FilterTestCommon", "Call"], eval_ctx: t.Optional[EvalContext]
 720) -> t.Tuple[t.List[t.Any], t.Dict[t.Any, t.Any]]:
 721    args = [x.as_const(eval_ctx) for x in node.args]
 722    kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)
 723
 724    if node.dyn_args is not None:
 725        try:
 726            args.extend(node.dyn_args.as_const(eval_ctx))
 727        except Exception as e:
 728            raise Impossible() from e
 729
 730    if node.dyn_kwargs is not None:
 731        try:
 732            kwargs.update(node.dyn_kwargs.as_const(eval_ctx))
 733        except Exception as e:
 734            raise Impossible() from e
 735
 736    return args, kwargs
 737
 738
 739class _FilterTestCommon(Expr):
 740    fields = ("node", "name", "args", "kwargs", "dyn_args", "dyn_kwargs")
 741    node: Expr
 742    name: str
 743    args: t.List[Expr]
 744    kwargs: t.List[Pair]
 745    dyn_args: t.Optional[Expr]
 746    dyn_kwargs: t.Optional[Expr]
 747    abstract = True
 748    _is_filter = True
 749
 750    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 751        eval_ctx = get_eval_context(self, eval_ctx)
 752
 753        if eval_ctx.volatile:
 754            raise Impossible()
 755
 756        if self._is_filter:
 757            env_map = eval_ctx.environment.filters
 758        else:
 759            env_map = eval_ctx.environment.tests
 760
 761        func = env_map.get(self.name)
 762        pass_arg = _PassArg.from_obj(func)  # type: ignore
 763
 764        if func is None or pass_arg is _PassArg.context:
 765            raise Impossible()
 766
 767        if eval_ctx.environment.is_async and (
 768            getattr(func, "jinja_async_variant", False) is True
 769            or inspect.iscoroutinefunction(func)
 770        ):
 771            raise Impossible()
 772
 773        args, kwargs = args_as_const(self, eval_ctx)
 774        args.insert(0, self.node.as_const(eval_ctx))
 775
 776        if pass_arg is _PassArg.eval_context:
 777            args.insert(0, eval_ctx)
 778        elif pass_arg is _PassArg.environment:
 779            args.insert(0, eval_ctx.environment)
 780
 781        try:
 782            return func(*args, **kwargs)
 783        except Exception as e:
 784            raise Impossible() from e
 785
 786
 787class Filter(_FilterTestCommon):
 788    """Apply a filter to an expression. ``name`` is the name of the
 789    filter, the other fields are the same as :class:`Call`.
 790
 791    If ``node`` is ``None``, the filter is being used in a filter block
 792    and is applied to the content of the block.
 793    """
 794
 795    node: t.Optional[Expr]  # type: ignore
 796
 797    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 798        if self.node is None:
 799            raise Impossible()
 800
 801        return super().as_const(eval_ctx=eval_ctx)
 802
 803
 804class Test(_FilterTestCommon):
 805    """Apply a test to an expression. ``name`` is the name of the test,
 806    the other field are the same as :class:`Call`.
 807
 808    .. versionchanged:: 3.0
 809        ``as_const`` shares the same logic for filters and tests. Tests
 810        check for volatile, async, and ``@pass_context`` etc.
 811        decorators.
 812    """
 813
 814    _is_filter = False
 815
 816
 817class Call(Expr):
 818    """Calls an expression.  `args` is a list of arguments, `kwargs` a list
 819    of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`
 820    and `dyn_kwargs` has to be either `None` or a node that is used as
 821    node for dynamic positional (``*args``) or keyword (``**kwargs``)
 822    arguments.
 823    """
 824
 825    fields = ("node", "args", "kwargs", "dyn_args", "dyn_kwargs")
 826    node: Expr
 827    args: t.List[Expr]
 828    kwargs: t.List[Keyword]
 829    dyn_args: t.Optional[Expr]
 830    dyn_kwargs: t.Optional[Expr]
 831
 832
 833class Getitem(Expr):
 834    """Get an attribute or item from an expression and prefer the item."""
 835
 836    fields = ("node", "arg", "ctx")
 837    node: Expr
 838    arg: Expr
 839    ctx: str
 840
 841    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 842        if self.ctx != "load":
 843            raise Impossible()
 844
 845        eval_ctx = get_eval_context(self, eval_ctx)
 846
 847        try:
 848            return eval_ctx.environment.getitem(
 849                self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)
 850            )
 851        except Exception as e:
 852            raise Impossible() from e
 853
 854
 855class Getattr(Expr):
 856    """Get an attribute or item from an expression that is a ascii-only
 857    bytestring and prefer the attribute.
 858    """
 859
 860    fields = ("node", "attr", "ctx")
 861    node: Expr
 862    attr: str
 863    ctx: str
 864
 865    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 866        if self.ctx != "load":
 867            raise Impossible()
 868
 869        eval_ctx = get_eval_context(self, eval_ctx)
 870
 871        try:
 872            return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)
 873        except Exception as e:
 874            raise Impossible() from e
 875
 876
 877class Slice(Expr):
 878    """Represents a slice object.  This must only be used as argument for
 879    :class:`Subscript`.
 880    """
 881
 882    fields = ("start", "stop", "step")
 883    start: t.Optional[Expr]
 884    stop: t.Optional[Expr]
 885    step: t.Optional[Expr]
 886
 887    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> slice:
 888        eval_ctx = get_eval_context(self, eval_ctx)
 889
 890        def const(obj: t.Optional[Expr]) -> t.Optional[t.Any]:
 891            if obj is None:
 892                return None
 893            return obj.as_const(eval_ctx)
 894
 895        return slice(const(self.start), const(self.stop), const(self.step))
 896
 897
 898class Concat(Expr):
 899    """Concatenates the list of expressions provided after converting
 900    them to strings.
 901    """
 902
 903    fields = ("nodes",)
 904    nodes: t.List[Expr]
 905
 906    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
 907        eval_ctx = get_eval_context(self, eval_ctx)
 908        return "".join(str(x.as_const(eval_ctx)) for x in self.nodes)
 909
 910
 911class Compare(Expr):
 912    """Compares an expression with some other expressions.  `ops` must be a
 913    list of :class:`Operand`\\s.
 914    """
 915
 916    fields = ("expr", "ops")
 917    expr: Expr
 918    ops: t.List["Operand"]
 919
 920    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 921        eval_ctx = get_eval_context(self, eval_ctx)
 922        result = value = self.expr.as_const(eval_ctx)
 923
 924        try:
 925            for op in self.ops:
 926                new_value = op.expr.as_const(eval_ctx)
 927                result = _cmpop_to_func[op.op](value, new_value)
 928
 929                if not result:
 930                    return False
 931
 932                value = new_value
 933        except Exception as e:
 934            raise Impossible() from e
 935
 936        return result
 937
 938
 939class Operand(Helper):
 940    """Holds an operator and an expression."""
 941
 942    fields = ("op", "expr")
 943    op: str
 944    expr: Expr
 945
 946
 947class Mul(BinExpr):
 948    """Multiplies the left with the right node."""
 949
 950    operator = "*"
 951
 952
 953class Div(BinExpr):
 954    """Divides the left by the right node."""
 955
 956    operator = "/"
 957
 958
 959class FloorDiv(BinExpr):
 960    """Divides the left by the right node and converts the
 961    result into an integer by truncating.
 962    """
 963
 964    operator = "//"
 965
 966
 967class Add(BinExpr):
 968    """Add the left to the right node."""
 969
 970    operator = "+"
 971
 972
 973class Sub(BinExpr):
 974    """Subtract the right from the left node."""
 975
 976    operator = "-"
 977
 978
 979class Mod(BinExpr):
 980    """Left modulo right."""
 981
 982    operator = "%"
 983
 984
 985class Pow(BinExpr):
 986    """Left to the power of right."""
 987
 988    operator = "**"
 989
 990
 991class And(BinExpr):
 992    """Short circuited AND."""
 993
 994    operator = "and"
 995
 996    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
 997        eval_ctx = get_eval_context(self, eval_ctx)
 998        return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)
 999
1000
1001class Or(BinExpr):
1002    """Short circuited OR."""
1003
1004    operator = "or"
1005
1006    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
1007        eval_ctx = get_eval_context(self, eval_ctx)
1008        return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)
1009
1010
1011class Not(UnaryExpr):
1012    """Negate the expression."""
1013
1014    operator = "not"
1015
1016
1017class Neg(UnaryExpr):
1018    """Make the expression negative."""
1019
1020    operator = "-"
1021
1022
1023class Pos(UnaryExpr):
1024    """Make the expression positive (noop for most expressions)"""
1025
1026    operator = "+"
1027
1028
1029# Helpers for extensions
1030
1031
1032class EnvironmentAttribute(Expr):
1033    """Loads an attribute from the environment object.  This is useful for
1034    extensions that want to call a callback stored on the environment.
1035    """
1036
1037    fields = ("name",)
1038    name: str
1039
1040
1041class ExtensionAttribute(Expr):
1042    """Returns the attribute of an extension bound to the environment.
1043    The identifier is the identifier of the :class:`Extension`.
1044
1045    This node is usually constructed by calling the
1046    :meth:`~jinja2.ext.Extension.attr` method on an extension.
1047    """
1048
1049    fields = ("identifier", "name")
1050    identifier: str
1051    name: str
1052
1053
1054class ImportedName(Expr):
1055    """If created with an import name the import name is returned on node
1056    access.  For example ``ImportedName('cgi.escape')`` returns the `escape`
1057    function from the cgi module on evaluation.  Imports are optimized by the
1058    compiler so there is no need to assign them to local variables.
1059    """
1060
1061    fields = ("importname",)
1062    importname: str
1063
1064
1065class InternalName(Expr):
1066    """An internal name in the compiler.  You cannot create these nodes
1067    yourself but the parser provides a
1068    :meth:`~jinja2.parser.Parser.free_identifier` method that creates
1069    a new identifier for you.  This identifier is not available from the
1070    template and is not treated specially by the compiler.
1071    """
1072
1073    fields = ("name",)
1074    name: str
1075
1076    def __init__(self) -> None:
1077        raise TypeError(
1078            "Can't create internal names.  Use the "
1079            "`free_identifier` method on a parser."
1080        )
1081
1082
1083class MarkSafe(Expr):
1084    """Mark the wrapped expression as safe (wrap it as `Markup`)."""
1085
1086    fields = ("expr",)
1087    expr: Expr
1088
1089    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> Markup:
1090        eval_ctx = get_eval_context(self, eval_ctx)
1091        return Markup(self.expr.as_const(eval_ctx))
1092
1093
1094class MarkSafeIfAutoescape(Expr):
1095    """Mark the wrapped expression as safe (wrap it as `Markup`) but
1096    only if autoescaping is active.
1097
1098    .. versionadded:: 2.5
1099    """
1100
1101    fields = ("expr",)
1102    expr: Expr
1103
1104    def as_const(
1105        self, eval_ctx: t.Optional[EvalContext] = None
1106    ) -> t.Union[Markup, t.Any]:
1107        eval_ctx = get_eval_context(self, eval_ctx)
1108        if eval_ctx.volatile:
1109            raise Impossible()
1110        expr = self.expr.as_const(eval_ctx)
1111        if eval_ctx.autoescape:
1112            return Markup(expr)
1113        return expr
1114
1115
1116class ContextReference(Expr):
1117    """Returns the current template context.  It can be used like a
1118    :class:`Name` node, with a ``'load'`` ctx and will return the
1119    current :class:`~jinja2.runtime.Context` object.
1120
1121    Here an example that assigns the current template name to a
1122    variable named `foo`::
1123
1124        Assign(Name('foo', ctx='store'),
1125               Getattr(ContextReference(), 'name'))
1126
1127    This is basically equivalent to using the
1128    :func:`~jinja2.pass_context` decorator when using the high-level
1129    API, which causes a reference to the context to be passed as the
1130    first argument to a function.
1131    """
1132
1133
1134class DerivedContextReference(Expr):
1135    """Return the current template context including locals. Behaves
1136    exactly like :class:`ContextReference`, but includes local
1137    variables, such as from a ``for`` loop.
1138
1139    .. versionadded:: 2.11
1140    """
1141
1142
1143class Continue(Stmt):
1144    """Continue a loop."""
1145
1146
1147class Break(Stmt):
1148    """Break a loop."""
1149
1150
1151class Scope(Stmt):
1152    """An artificial scope."""
1153
1154    fields = ("body",)
1155    body: t.List[Node]
1156
1157
1158class OverlayScope(Stmt):
1159    """An overlay scope for extensions.  This is a largely unoptimized scope
1160    that however can be used to introduce completely arbitrary variables into
1161    a sub scope from a dictionary or dictionary like object.  The `context`
1162    field has to evaluate to a dictionary object.
1163
1164    Example usage::
1165
1166        OverlayScope(context=self.call_method('get_context'),
1167                     body=[...])
1168
1169    .. versionadded:: 2.10
1170    """
1171
1172    fields = ("context", "body")
1173    context: Expr
1174    body: t.List[Node]
1175
1176
1177class EvalContextModifier(Stmt):
1178    """Modifies the eval context.  For each option that should be modified,
1179    a :class:`Keyword` has to be added to the :attr:`options` list.
1180
1181    Example to change the `autoescape` setting::
1182
1183        EvalContextModifier(options=[Keyword('autoescape', Const(True))])
1184    """
1185
1186    fields = ("options",)
1187    options: t.List[Keyword]
1188
1189
1190class ScopedEvalContextModifier(EvalContextModifier):
1191    """Modifies the eval context and reverts it later.  Works exactly like
1192    :class:`EvalContextModifier` but will only modify the
1193    :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.
1194    """
1195
1196    fields = ("body",)
1197    body: t.List[Node]
1198
1199
1200# make sure nobody creates custom nodes
1201def _failing_new(*args: t.Any, **kwargs: t.Any) -> "te.NoReturn":
1202    raise TypeError("can't create custom node types")
1203
1204
1205NodeType.__new__ = staticmethod(_failing_new)  # type: ignore
1206del _failing_new
class Impossible(builtins.Exception):
51class Impossible(Exception):
52    """Raised if the node could not perform a requested action."""

Raised if the node could not perform a requested action.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
add_note
args
class NodeType(builtins.type):
55class NodeType(type):
56    """A metaclass for nodes that handles the field and attribute
57    inheritance.  fields and attributes from the parent class are
58    automatically forwarded to the child."""
59
60    def __new__(mcs, name, bases, d):  # type: ignore
61        for attr in "fields", "attributes":
62            storage: t.List[t.Tuple[str, ...]] = []
63            storage.extend(getattr(bases[0] if bases else object, attr, ()))
64            storage.extend(d.get(attr, ()))
65            assert len(bases) <= 1, "multiple inheritance not allowed"
66            assert len(storage) == len(set(storage)), "layout conflict"
67            d[attr] = tuple(storage)
68        d.setdefault("abstract", False)
69        return type.__new__(mcs, name, bases, d)

A metaclass for nodes that handles the field and attribute inheritance. fields and attributes from the parent class are automatically forwarded to the child.

Inherited Members
builtins.type
type
mro
class EvalContext:
72class EvalContext:
73    """Holds evaluation time information.  Custom attributes can be attached
74    to it in extensions.
75    """
76
77    def __init__(
78        self, environment: "Environment", template_name: t.Optional[str] = None
79    ) -> None:
80        self.environment = environment
81        if callable(environment.autoescape):
82            self.autoescape = environment.autoescape(template_name)
83        else:
84            self.autoescape = environment.autoescape
85        self.volatile = False
86
87    def save(self) -> t.Mapping[str, t.Any]:
88        return self.__dict__.copy()
89
90    def revert(self, old: t.Mapping[str, t.Any]) -> None:
91        self.__dict__.clear()
92        self.__dict__.update(old)

Holds evaluation time information. Custom attributes can be attached to it in extensions.

EvalContext( environment: jinja2.environment.Environment, template_name: Optional[str] = None)
77    def __init__(
78        self, environment: "Environment", template_name: t.Optional[str] = None
79    ) -> None:
80        self.environment = environment
81        if callable(environment.autoescape):
82            self.autoescape = environment.autoescape(template_name)
83        else:
84            self.autoescape = environment.autoescape
85        self.volatile = False
environment
volatile
def save(self) -> Mapping[str, Any]:
87    def save(self) -> t.Mapping[str, t.Any]:
88        return self.__dict__.copy()
def revert(self, old: Mapping[str, Any]) -> None:
90    def revert(self, old: t.Mapping[str, t.Any]) -> None:
91        self.__dict__.clear()
92        self.__dict__.update(old)
def get_eval_context( node: Node, ctx: Optional[EvalContext]) -> EvalContext:
 95def get_eval_context(node: "Node", ctx: t.Optional[EvalContext]) -> EvalContext:
 96    if ctx is None:
 97        if node.environment is None:
 98            raise RuntimeError(
 99                "if no eval context is passed, the node must have an"
100                " attached environment."
101            )
102        return EvalContext(node.environment)
103    return ctx
class Node:
106class Node(metaclass=NodeType):
107    """Baseclass for all Jinja nodes.  There are a number of nodes available
108    of different types.  There are four major types:
109
110    -   :class:`Stmt`: statements
111    -   :class:`Expr`: expressions
112    -   :class:`Helper`: helper nodes
113    -   :class:`Template`: the outermost wrapper node
114
115    All nodes have fields and attributes.  Fields may be other nodes, lists,
116    or arbitrary values.  Fields are passed to the constructor as regular
117    positional arguments, attributes as keyword arguments.  Each node has
118    two attributes: `lineno` (the line number of the node) and `environment`.
119    The `environment` attribute is set at the end of the parsing process for
120    all nodes automatically.
121    """
122
123    fields: t.Tuple[str, ...] = ()
124    attributes: t.Tuple[str, ...] = ("lineno", "environment")
125    abstract = True
126
127    lineno: int
128    environment: t.Optional["Environment"]
129
130    def __init__(self, *fields: t.Any, **attributes: t.Any) -> None:
131        if self.abstract:
132            raise TypeError("abstract nodes are not instantiable")
133        if fields:
134            if len(fields) != len(self.fields):
135                if not self.fields:
136                    raise TypeError(f"{type(self).__name__!r} takes 0 arguments")
137                raise TypeError(
138                    f"{type(self).__name__!r} takes 0 or {len(self.fields)}"
139                    f" argument{'s' if len(self.fields) != 1 else ''}"
140                )
141            for name, arg in zip(self.fields, fields):
142                setattr(self, name, arg)
143        for attr in self.attributes:
144            setattr(self, attr, attributes.pop(attr, None))
145        if attributes:
146            raise TypeError(f"unknown attribute {next(iter(attributes))!r}")
147
148    def iter_fields(
149        self,
150        exclude: t.Optional[t.Container[str]] = None,
151        only: t.Optional[t.Container[str]] = None,
152    ) -> t.Iterator[t.Tuple[str, t.Any]]:
153        """This method iterates over all fields that are defined and yields
154        ``(key, value)`` tuples.  Per default all fields are returned, but
155        it's possible to limit that to some fields by providing the `only`
156        parameter or to exclude some using the `exclude` parameter.  Both
157        should be sets or tuples of field names.
158        """
159        for name in self.fields:
160            if (
161                (exclude is None and only is None)
162                or (exclude is not None and name not in exclude)
163                or (only is not None and name in only)
164            ):
165                try:
166                    yield name, getattr(self, name)
167                except AttributeError:
168                    pass
169
170    def iter_child_nodes(
171        self,
172        exclude: t.Optional[t.Container[str]] = None,
173        only: t.Optional[t.Container[str]] = None,
174    ) -> t.Iterator["Node"]:
175        """Iterates over all direct child nodes of the node.  This iterates
176        over all fields and yields the values of they are nodes.  If the value
177        of a field is a list all the nodes in that list are returned.
178        """
179        for _, item in self.iter_fields(exclude, only):
180            if isinstance(item, list):
181                for n in item:
182                    if isinstance(n, Node):
183                        yield n
184            elif isinstance(item, Node):
185                yield item
186
187    def find(self, node_type: t.Type[_NodeBound]) -> t.Optional[_NodeBound]:
188        """Find the first node of a given type.  If no such node exists the
189        return value is `None`.
190        """
191        for result in self.find_all(node_type):
192            return result
193
194        return None
195
196    def find_all(
197        self, node_type: t.Union[t.Type[_NodeBound], t.Tuple[t.Type[_NodeBound], ...]]
198    ) -> t.Iterator[_NodeBound]:
199        """Find all the nodes of a given type.  If the type is a tuple,
200        the check is performed for any of the tuple items.
201        """
202        for child in self.iter_child_nodes():
203            if isinstance(child, node_type):
204                yield child  # type: ignore
205            yield from child.find_all(node_type)
206
207    def set_ctx(self, ctx: str) -> "Node":
208        """Reset the context of a node and all child nodes.  Per default the
209        parser will all generate nodes that have a 'load' context as it's the
210        most common one.  This method is used in the parser to set assignment
211        targets and other nodes to a store context.
212        """
213        todo = deque([self])
214        while todo:
215            node = todo.popleft()
216            if "ctx" in node.fields:
217                node.ctx = ctx  # type: ignore
218            todo.extend(node.iter_child_nodes())
219        return self
220
221    def set_lineno(self, lineno: int, override: bool = False) -> "Node":
222        """Set the line numbers of the node and children."""
223        todo = deque([self])
224        while todo:
225            node = todo.popleft()
226            if "lineno" in node.attributes:
227                if node.lineno is None or override:
228                    node.lineno = lineno
229            todo.extend(node.iter_child_nodes())
230        return self
231
232    def set_environment(self, environment: "Environment") -> "Node":
233        """Set the environment for all nodes."""
234        todo = deque([self])
235        while todo:
236            node = todo.popleft()
237            node.environment = environment
238            todo.extend(node.iter_child_nodes())
239        return self
240
241    def __eq__(self, other: t.Any) -> bool:
242        if type(self) is not type(other):
243            return NotImplemented
244
245        return tuple(self.iter_fields()) == tuple(other.iter_fields())
246
247    __hash__ = object.__hash__
248
249    def __repr__(self) -> str:
250        args_str = ", ".join(f"{a}={getattr(self, a, None)!r}" for a in self.fields)
251        return f"{type(self).__name__}({args_str})"
252
253    def dump(self) -> str:
254        def _dump(node: t.Union[Node, t.Any]) -> None:
255            if not isinstance(node, Node):
256                buf.append(repr(node))
257                return
258
259            buf.append(f"nodes.{type(node).__name__}(")
260            if not node.fields:
261                buf.append(")")
262                return
263            for idx, field in enumerate(node.fields):
264                if idx:
265                    buf.append(", ")
266                value = getattr(node, field)
267                if isinstance(value, list):
268                    buf.append("[")
269                    for idx, item in enumerate(value):
270                        if idx:
271                            buf.append(", ")
272                        _dump(item)
273                    buf.append("]")
274                else:
275                    _dump(value)
276            buf.append(")")
277
278        buf: t.List[str] = []
279        _dump(self)
280        return "".join(buf)

Baseclass for all Jinja nodes. There are a number of nodes available of different types. There are four major types:

All nodes have fields and attributes. Fields may be other nodes, lists, or arbitrary values. Fields are passed to the constructor as regular positional arguments, attributes as keyword arguments. Each node has two attributes: lineno (the line number of the node) and environment. The environment attribute is set at the end of the parsing process for all nodes automatically.

Node(*fields: Any, **attributes: Any)
130    def __init__(self, *fields: t.Any, **attributes: t.Any) -> None:
131        if self.abstract:
132            raise TypeError("abstract nodes are not instantiable")
133        if fields:
134            if len(fields) != len(self.fields):
135                if not self.fields:
136                    raise TypeError(f"{type(self).__name__!r} takes 0 arguments")
137                raise TypeError(
138                    f"{type(self).__name__!r} takes 0 or {len(self.fields)}"
139                    f" argument{'s' if len(self.fields) != 1 else ''}"
140                )
141            for name, arg in zip(self.fields, fields):
142                setattr(self, name, arg)
143        for attr in self.attributes:
144            setattr(self, attr, attributes.pop(attr, None))
145        if attributes:
146            raise TypeError(f"unknown attribute {next(iter(attributes))!r}")
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
abstract = True
lineno: int
environment: Optional[jinja2.environment.Environment]
def iter_fields( self, exclude: Optional[Container[str]] = None, only: Optional[Container[str]] = None) -> Iterator[Tuple[str, Any]]:
148    def iter_fields(
149        self,
150        exclude: t.Optional[t.Container[str]] = None,
151        only: t.Optional[t.Container[str]] = None,
152    ) -> t.Iterator[t.Tuple[str, t.Any]]:
153        """This method iterates over all fields that are defined and yields
154        ``(key, value)`` tuples.  Per default all fields are returned, but
155        it's possible to limit that to some fields by providing the `only`
156        parameter or to exclude some using the `exclude` parameter.  Both
157        should be sets or tuples of field names.
158        """
159        for name in self.fields:
160            if (
161                (exclude is None and only is None)
162                or (exclude is not None and name not in exclude)
163                or (only is not None and name in only)
164            ):
165                try:
166                    yield name, getattr(self, name)
167                except AttributeError:
168                    pass

This method iterates over all fields that are defined and yields (key, value) tuples. Per default all fields are returned, but it's possible to limit that to some fields by providing the only parameter or to exclude some using the exclude parameter. Both should be sets or tuples of field names.

def iter_child_nodes( self, exclude: Optional[Container[str]] = None, only: Optional[Container[str]] = None) -> Iterator[Node]:
170    def iter_child_nodes(
171        self,
172        exclude: t.Optional[t.Container[str]] = None,
173        only: t.Optional[t.Container[str]] = None,
174    ) -> t.Iterator["Node"]:
175        """Iterates over all direct child nodes of the node.  This iterates
176        over all fields and yields the values of they are nodes.  If the value
177        of a field is a list all the nodes in that list are returned.
178        """
179        for _, item in self.iter_fields(exclude, only):
180            if isinstance(item, list):
181                for n in item:
182                    if isinstance(n, Node):
183                        yield n
184            elif isinstance(item, Node):
185                yield item

Iterates over all direct child nodes of the node. This iterates over all fields and yields the values of they are nodes. If the value of a field is a list all the nodes in that list are returned.

def find(self, node_type: Type[~_NodeBound]) -> Optional[~_NodeBound]:
187    def find(self, node_type: t.Type[_NodeBound]) -> t.Optional[_NodeBound]:
188        """Find the first node of a given type.  If no such node exists the
189        return value is `None`.
190        """
191        for result in self.find_all(node_type):
192            return result
193
194        return None

Find the first node of a given type. If no such node exists the return value is None.

def find_all( self, node_type: Union[Type[~_NodeBound], Tuple[Type[~_NodeBound], ...]]) -> Iterator[~_NodeBound]:
196    def find_all(
197        self, node_type: t.Union[t.Type[_NodeBound], t.Tuple[t.Type[_NodeBound], ...]]
198    ) -> t.Iterator[_NodeBound]:
199        """Find all the nodes of a given type.  If the type is a tuple,
200        the check is performed for any of the tuple items.
201        """
202        for child in self.iter_child_nodes():
203            if isinstance(child, node_type):
204                yield child  # type: ignore
205            yield from child.find_all(node_type)

Find all the nodes of a given type. If the type is a tuple, the check is performed for any of the tuple items.

def set_ctx(self, ctx: str) -> Node:
207    def set_ctx(self, ctx: str) -> "Node":
208        """Reset the context of a node and all child nodes.  Per default the
209        parser will all generate nodes that have a 'load' context as it's the
210        most common one.  This method is used in the parser to set assignment
211        targets and other nodes to a store context.
212        """
213        todo = deque([self])
214        while todo:
215            node = todo.popleft()
216            if "ctx" in node.fields:
217                node.ctx = ctx  # type: ignore
218            todo.extend(node.iter_child_nodes())
219        return self

Reset the context of a node and all child nodes. Per default the parser will all generate nodes that have a 'load' context as it's the most common one. This method is used in the parser to set assignment targets and other nodes to a store context.

def set_lineno(self, lineno: int, override: bool = False) -> Node:
221    def set_lineno(self, lineno: int, override: bool = False) -> "Node":
222        """Set the line numbers of the node and children."""
223        todo = deque([self])
224        while todo:
225            node = todo.popleft()
226            if "lineno" in node.attributes:
227                if node.lineno is None or override:
228                    node.lineno = lineno
229            todo.extend(node.iter_child_nodes())
230        return self

Set the line numbers of the node and children.

def set_environment(self, environment: jinja2.environment.Environment) -> Node:
232    def set_environment(self, environment: "Environment") -> "Node":
233        """Set the environment for all nodes."""
234        todo = deque([self])
235        while todo:
236            node = todo.popleft()
237            node.environment = environment
238            todo.extend(node.iter_child_nodes())
239        return self

Set the environment for all nodes.

def dump(self) -> str:
253    def dump(self) -> str:
254        def _dump(node: t.Union[Node, t.Any]) -> None:
255            if not isinstance(node, Node):
256                buf.append(repr(node))
257                return
258
259            buf.append(f"nodes.{type(node).__name__}(")
260            if not node.fields:
261                buf.append(")")
262                return
263            for idx, field in enumerate(node.fields):
264                if idx:
265                    buf.append(", ")
266                value = getattr(node, field)
267                if isinstance(value, list):
268                    buf.append("[")
269                    for idx, item in enumerate(value):
270                        if idx:
271                            buf.append(", ")
272                        _dump(item)
273                    buf.append("]")
274                else:
275                    _dump(value)
276            buf.append(")")
277
278        buf: t.List[str] = []
279        _dump(self)
280        return "".join(buf)
class Stmt(Node):
283class Stmt(Node):
284    """Base node for all statements."""
285
286    abstract = True

Base node for all statements.

abstract = True
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Helper(Node):
289class Helper(Node):
290    """Nodes that exist in a specific context only."""
291
292    abstract = True

Nodes that exist in a specific context only.

abstract = True
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Template(Node):
295class Template(Node):
296    """Node that represents a template.  This must be the outermost node that
297    is passed to the compiler.
298    """
299
300    fields = ("body",)
301    body: t.List[Node]

Node that represents a template. This must be the outermost node that is passed to the compiler.

fields = ('body',)
body: List[Node]
attributes: Tuple[str, ...] = ('lineno', 'environment')
abstract = False
class Output(Stmt):
304class Output(Stmt):
305    """A node that holds multiple expressions which are then printed out.
306    This is used both for the `print` statement and the regular template data.
307    """
308
309    fields = ("nodes",)
310    nodes: t.List["Expr"]

A node that holds multiple expressions which are then printed out. This is used both for the print statement and the regular template data.

fields = ('nodes',)
nodes: List[Expr]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Extends(Stmt):
313class Extends(Stmt):
314    """Represents an extends statement."""
315
316    fields = ("template",)
317    template: "Expr"

Represents an extends statement.

fields = ('template',)
template: Expr
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class For(Stmt):
320class For(Stmt):
321    """The for loop.  `target` is the target for the iteration (usually a
322    :class:`Name` or :class:`Tuple`), `iter` the iterable.  `body` is a list
323    of nodes that are used as loop-body, and `else_` a list of nodes for the
324    `else` block.  If no else node exists it has to be an empty list.
325
326    For filtered nodes an expression can be stored as `test`, otherwise `None`.
327    """
328
329    fields = ("target", "iter", "body", "else_", "test", "recursive")
330    target: Node
331    iter: Node
332    body: t.List[Node]
333    else_: t.List[Node]
334    test: t.Optional[Node]
335    recursive: bool

The for loop. target is the target for the iteration (usually a Name or Tuple), iter the iterable. body is a list of nodes that are used as loop-body, and else_ a list of nodes for the else block. If no else node exists it has to be an empty list.

For filtered nodes an expression can be stored as test, otherwise None.

fields = ('target', 'iter', 'body', 'else_', 'test', 'recursive')
target: Node
iter: Node
body: List[Node]
else_: List[Node]
test: Optional[Node]
recursive: bool
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class If(Stmt):
338class If(Stmt):
339    """If `test` is true, `body` is rendered, else `else_`."""
340
341    fields = ("test", "body", "elif_", "else_")
342    test: Node
343    body: t.List[Node]
344    elif_: t.List["If"]
345    else_: t.List[Node]

If test is true, body is rendered, else else_.

fields = ('test', 'body', 'elif_', 'else_')
test: Node
body: List[Node]
elif_: List[If]
else_: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Macro(Stmt):
348class Macro(Stmt):
349    """A macro definition.  `name` is the name of the macro, `args` a list of
350    arguments and `defaults` a list of defaults if there are any.  `body` is
351    a list of nodes for the macro body.
352    """
353
354    fields = ("name", "args", "defaults", "body")
355    name: str
356    args: t.List["Name"]
357    defaults: t.List["Expr"]
358    body: t.List[Node]

A macro definition. name is the name of the macro, args a list of arguments and defaults a list of defaults if there are any. body is a list of nodes for the macro body.

fields = ('name', 'args', 'defaults', 'body')
name: str
args: List[Name]
defaults: List[Expr]
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class CallBlock(Stmt):
361class CallBlock(Stmt):
362    """Like a macro without a name but a call instead.  `call` is called with
363    the unnamed macro as `caller` argument this node holds.
364    """
365
366    fields = ("call", "args", "defaults", "body")
367    call: "Call"
368    args: t.List["Name"]
369    defaults: t.List["Expr"]
370    body: t.List[Node]

Like a macro without a name but a call instead. call is called with the unnamed macro as caller argument this node holds.

fields = ('call', 'args', 'defaults', 'body')
call: Call
args: List[Name]
defaults: List[Expr]
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class FilterBlock(Stmt):
373class FilterBlock(Stmt):
374    """Node for filter sections."""
375
376    fields = ("body", "filter")
377    body: t.List[Node]
378    filter: "Filter"

Node for filter sections.

fields = ('body', 'filter')
body: List[Node]
filter: Filter
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class With(Stmt):
381class With(Stmt):
382    """Specific node for with statements.  In older versions of Jinja the
383    with statement was implemented on the base of the `Scope` node instead.
384
385    .. versionadded:: 2.9.3
386    """
387
388    fields = ("targets", "values", "body")
389    targets: t.List["Expr"]
390    values: t.List["Expr"]
391    body: t.List[Node]

Specific node for with statements. In older versions of Jinja the with statement was implemented on the base of the Scope node instead.

New in version 2.9.3.

fields = ('targets', 'values', 'body')
targets: List[Expr]
values: List[Expr]
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Block(Stmt):
394class Block(Stmt):
395    """A node that represents a block.
396
397    .. versionchanged:: 3.0.0
398        the `required` field was added.
399    """
400
401    fields = ("name", "body", "scoped", "required")
402    name: str
403    body: t.List[Node]
404    scoped: bool
405    required: bool

A node that represents a block.

Changed in version 3.0.0: the required field was added.

fields = ('name', 'body', 'scoped', 'required')
name: str
body: List[Node]
scoped: bool
required: bool
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Include(Stmt):
408class Include(Stmt):
409    """A node that represents the include tag."""
410
411    fields = ("template", "with_context", "ignore_missing")
412    template: "Expr"
413    with_context: bool
414    ignore_missing: bool

A node that represents the include tag.

fields = ('template', 'with_context', 'ignore_missing')
template: Expr
with_context: bool
ignore_missing: bool
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Import(Stmt):
417class Import(Stmt):
418    """A node that represents the import tag."""
419
420    fields = ("template", "target", "with_context")
421    template: "Expr"
422    target: str
423    with_context: bool

A node that represents the import tag.

fields = ('template', 'target', 'with_context')
template: Expr
target: str
with_context: bool
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class FromImport(Stmt):
426class FromImport(Stmt):
427    """A node that represents the from import tag.  It's important to not
428    pass unsafe names to the name attribute.  The compiler translates the
429    attribute lookups directly into getattr calls and does *not* use the
430    subscript callback of the interface.  As exported variables may not
431    start with double underscores (which the parser asserts) this is not a
432    problem for regular Jinja code, but if this node is used in an extension
433    extra care must be taken.
434
435    The list of names may contain tuples if aliases are wanted.
436    """
437
438    fields = ("template", "names", "with_context")
439    template: "Expr"
440    names: t.List[t.Union[str, t.Tuple[str, str]]]
441    with_context: bool

A node that represents the from import tag. It's important to not pass unsafe names to the name attribute. The compiler translates the attribute lookups directly into getattr calls and does not use the subscript callback of the interface. As exported variables may not start with double underscores (which the parser asserts) this is not a problem for regular Jinja code, but if this node is used in an extension extra care must be taken.

The list of names may contain tuples if aliases are wanted.

fields = ('template', 'names', 'with_context')
template: Expr
names: List[Union[str, Tuple[str, str]]]
with_context: bool
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class ExprStmt(Stmt):
444class ExprStmt(Stmt):
445    """A statement that evaluates an expression and discards the result."""
446
447    fields = ("node",)
448    node: Node

A statement that evaluates an expression and discards the result.

fields = ('node',)
node: Node
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Assign(Stmt):
451class Assign(Stmt):
452    """Assigns an expression to a target."""
453
454    fields = ("target", "node")
455    target: "Expr"
456    node: Node

Assigns an expression to a target.

fields = ('target', 'node')
target: Expr
node: Node
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class AssignBlock(Stmt):
459class AssignBlock(Stmt):
460    """Assigns a block to a target."""
461
462    fields = ("target", "filter", "body")
463    target: "Expr"
464    filter: t.Optional["Filter"]
465    body: t.List[Node]

Assigns a block to a target.

fields = ('target', 'filter', 'body')
target: Expr
filter: Optional[Filter]
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Expr(Node):
468class Expr(Node):
469    """Baseclass for all expressions."""
470
471    abstract = True
472
473    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
474        """Return the value of the expression as constant or raise
475        :exc:`Impossible` if this was not possible.
476
477        An :class:`EvalContext` can be provided, if none is given
478        a default context is created which requires the nodes to have
479        an attached environment.
480
481        .. versionchanged:: 2.4
482           the `eval_ctx` parameter was added.
483        """
484        raise Impossible()
485
486    def can_assign(self) -> bool:
487        """Check if it's possible to assign something to this node."""
488        return False

Baseclass for all expressions.

abstract = True
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
473    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
474        """Return the value of the expression as constant or raise
475        :exc:`Impossible` if this was not possible.
476
477        An :class:`EvalContext` can be provided, if none is given
478        a default context is created which requires the nodes to have
479        an attached environment.
480
481        .. versionchanged:: 2.4
482           the `eval_ctx` parameter was added.
483        """
484        raise Impossible()

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

def can_assign(self) -> bool:
486    def can_assign(self) -> bool:
487        """Check if it's possible to assign something to this node."""
488        return False

Check if it's possible to assign something to this node.

fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class BinExpr(Expr):
491class BinExpr(Expr):
492    """Baseclass for all binary expressions."""
493
494    fields = ("left", "right")
495    left: Expr
496    right: Expr
497    operator: str
498    abstract = True
499
500    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
501        eval_ctx = get_eval_context(self, eval_ctx)
502
503        # intercepted operators cannot be folded at compile time
504        if (
505            eval_ctx.environment.sandboxed
506            and self.operator in eval_ctx.environment.intercepted_binops  # type: ignore
507        ):
508            raise Impossible()
509        f = _binop_to_func[self.operator]
510        try:
511            return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
512        except Exception as e:
513            raise Impossible() from e

Baseclass for all binary expressions.

fields = ('left', 'right')
left: Expr
right: Expr
operator: str
abstract = True
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
500    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
501        eval_ctx = get_eval_context(self, eval_ctx)
502
503        # intercepted operators cannot be folded at compile time
504        if (
505            eval_ctx.environment.sandboxed
506            and self.operator in eval_ctx.environment.intercepted_binops  # type: ignore
507        ):
508            raise Impossible()
509        f = _binop_to_func[self.operator]
510        try:
511            return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
512        except Exception as e:
513            raise Impossible() from e

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

attributes: Tuple[str, ...] = ('lineno', 'environment')
class UnaryExpr(Expr):
516class UnaryExpr(Expr):
517    """Baseclass for all unary expressions."""
518
519    fields = ("node",)
520    node: Expr
521    operator: str
522    abstract = True
523
524    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
525        eval_ctx = get_eval_context(self, eval_ctx)
526
527        # intercepted operators cannot be folded at compile time
528        if (
529            eval_ctx.environment.sandboxed
530            and self.operator in eval_ctx.environment.intercepted_unops  # type: ignore
531        ):
532            raise Impossible()
533        f = _uaop_to_func[self.operator]
534        try:
535            return f(self.node.as_const(eval_ctx))
536        except Exception as e:
537            raise Impossible() from e

Baseclass for all unary expressions.

fields = ('node',)
node: Expr
operator: str
abstract = True
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
524    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
525        eval_ctx = get_eval_context(self, eval_ctx)
526
527        # intercepted operators cannot be folded at compile time
528        if (
529            eval_ctx.environment.sandboxed
530            and self.operator in eval_ctx.environment.intercepted_unops  # type: ignore
531        ):
532            raise Impossible()
533        f = _uaop_to_func[self.operator]
534        try:
535            return f(self.node.as_const(eval_ctx))
536        except Exception as e:
537            raise Impossible() from e

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

attributes: Tuple[str, ...] = ('lineno', 'environment')
class Name(Expr):
540class Name(Expr):
541    """Looks up a name or stores a value in a name.
542    The `ctx` of the node can be one of the following values:
543
544    -   `store`: store a value in the name
545    -   `load`: load that name
546    -   `param`: like `store` but if the name was defined as function parameter.
547    """
548
549    fields = ("name", "ctx")
550    name: str
551    ctx: str
552
553    def can_assign(self) -> bool:
554        return self.name not in {"true", "false", "none", "True", "False", "None"}

Looks up a name or stores a value in a name. The ctx of the node can be one of the following values:

  • store: store a value in the name
  • load: load that name
  • param: like store but if the name was defined as function parameter.
fields = ('name', 'ctx')
name: str
ctx: str
def can_assign(self) -> bool:
553    def can_assign(self) -> bool:
554        return self.name not in {"true", "false", "none", "True", "False", "None"}

Check if it's possible to assign something to this node.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class NSRef(Expr):
557class NSRef(Expr):
558    """Reference to a namespace value assignment"""
559
560    fields = ("name", "attr")
561    name: str
562    attr: str
563
564    def can_assign(self) -> bool:
565        # We don't need any special checks here; NSRef assignments have a
566        # runtime check to ensure the target is a namespace object which will
567        # have been checked already as it is created using a normal assignment
568        # which goes through a `Name` node.
569        return True

Reference to a namespace value assignment

fields = ('name', 'attr')
name: str
attr: str
def can_assign(self) -> bool:
564    def can_assign(self) -> bool:
565        # We don't need any special checks here; NSRef assignments have a
566        # runtime check to ensure the target is a namespace object which will
567        # have been checked already as it is created using a normal assignment
568        # which goes through a `Name` node.
569        return True

Check if it's possible to assign something to this node.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Literal(Expr):
572class Literal(Expr):
573    """Baseclass for literals."""
574
575    abstract = True

Baseclass for literals.

abstract = True
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Const(Literal):
578class Const(Literal):
579    """All constant values.  The parser will return this node for simple
580    constants such as ``42`` or ``"foo"`` but it can be used to store more
581    complex values such as lists too.  Only constants with a safe
582    representation (objects where ``eval(repr(x)) == x`` is true).
583    """
584
585    fields = ("value",)
586    value: t.Any
587
588    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
589        return self.value
590
591    @classmethod
592    def from_untrusted(
593        cls,
594        value: t.Any,
595        lineno: t.Optional[int] = None,
596        environment: "t.Optional[Environment]" = None,
597    ) -> "Const":
598        """Return a const object if the value is representable as
599        constant value in the generated code, otherwise it will raise
600        an `Impossible` exception.
601        """
602        from .compiler import has_safe_repr
603
604        if not has_safe_repr(value):
605            raise Impossible()
606        return cls(value, lineno=lineno, environment=environment)

All constant values. The parser will return this node for simple constants such as 42 or "foo" but it can be used to store more complex values such as lists too. Only constants with a safe representation (objects where eval(repr(x)) == x is true).

fields = ('value',)
value: Any
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
588    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
589        return self.value

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

@classmethod
def from_untrusted( cls, value: Any, lineno: Optional[int] = None, environment: Optional[jinja2.environment.Environment] = None) -> Const:
591    @classmethod
592    def from_untrusted(
593        cls,
594        value: t.Any,
595        lineno: t.Optional[int] = None,
596        environment: "t.Optional[Environment]" = None,
597    ) -> "Const":
598        """Return a const object if the value is representable as
599        constant value in the generated code, otherwise it will raise
600        an `Impossible` exception.
601        """
602        from .compiler import has_safe_repr
603
604        if not has_safe_repr(value):
605            raise Impossible()
606        return cls(value, lineno=lineno, environment=environment)

Return a const object if the value is representable as constant value in the generated code, otherwise it will raise an Impossible exception.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class TemplateData(Literal):
609class TemplateData(Literal):
610    """A constant template string."""
611
612    fields = ("data",)
613    data: str
614
615    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
616        eval_ctx = get_eval_context(self, eval_ctx)
617        if eval_ctx.volatile:
618            raise Impossible()
619        if eval_ctx.autoescape:
620            return Markup(self.data)
621        return self.data

A constant template string.

fields = ('data',)
data: str
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> str:
615    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
616        eval_ctx = get_eval_context(self, eval_ctx)
617        if eval_ctx.volatile:
618            raise Impossible()
619        if eval_ctx.autoescape:
620            return Markup(self.data)
621        return self.data

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Tuple(Literal):
624class Tuple(Literal):
625    """For loop unpacking and some other things like multiple arguments
626    for subscripts.  Like for :class:`Name` `ctx` specifies if the tuple
627    is used for loading the names or storing.
628    """
629
630    fields = ("items", "ctx")
631    items: t.List[Expr]
632    ctx: str
633
634    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[t.Any, ...]:
635        eval_ctx = get_eval_context(self, eval_ctx)
636        return tuple(x.as_const(eval_ctx) for x in self.items)
637
638    def can_assign(self) -> bool:
639        for item in self.items:
640            if not item.can_assign():
641                return False
642        return True

For loop unpacking and some other things like multiple arguments for subscripts. Like for Name ctx specifies if the tuple is used for loading the names or storing.

fields = ('items', 'ctx')
items: List[Expr]
ctx: str
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> Tuple[Any, ...]:
634    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[t.Any, ...]:
635        eval_ctx = get_eval_context(self, eval_ctx)
636        return tuple(x.as_const(eval_ctx) for x in self.items)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

def can_assign(self) -> bool:
638    def can_assign(self) -> bool:
639        for item in self.items:
640            if not item.can_assign():
641                return False
642        return True

Check if it's possible to assign something to this node.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class List(Literal):
645class List(Literal):
646    """Any list literal such as ``[1, 2, 3]``"""
647
648    fields = ("items",)
649    items: t.List[Expr]
650
651    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.List[t.Any]:
652        eval_ctx = get_eval_context(self, eval_ctx)
653        return [x.as_const(eval_ctx) for x in self.items]

Any list literal such as [1, 2, 3]

fields = ('items',)
items: List[Expr]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> List[Any]:
651    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.List[t.Any]:
652        eval_ctx = get_eval_context(self, eval_ctx)
653        return [x.as_const(eval_ctx) for x in self.items]

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Dict(Literal):
656class Dict(Literal):
657    """Any dict literal such as ``{1: 2, 3: 4}``.  The items must be a list of
658    :class:`Pair` nodes.
659    """
660
661    fields = ("items",)
662    items: t.List["Pair"]
663
664    def as_const(
665        self, eval_ctx: t.Optional[EvalContext] = None
666    ) -> t.Dict[t.Any, t.Any]:
667        eval_ctx = get_eval_context(self, eval_ctx)
668        return dict(x.as_const(eval_ctx) for x in self.items)

Any dict literal such as {1: 2, 3: 4}. The items must be a list of Pair nodes.

fields = ('items',)
items: List[Pair]
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> Dict[Any, Any]:
664    def as_const(
665        self, eval_ctx: t.Optional[EvalContext] = None
666    ) -> t.Dict[t.Any, t.Any]:
667        eval_ctx = get_eval_context(self, eval_ctx)
668        return dict(x.as_const(eval_ctx) for x in self.items)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Pair(Helper):
671class Pair(Helper):
672    """A key, value pair for dicts."""
673
674    fields = ("key", "value")
675    key: Expr
676    value: Expr
677
678    def as_const(
679        self, eval_ctx: t.Optional[EvalContext] = None
680    ) -> t.Tuple[t.Any, t.Any]:
681        eval_ctx = get_eval_context(self, eval_ctx)
682        return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)

A key, value pair for dicts.

fields = ('key', 'value')
key: Expr
value: Expr
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> Tuple[Any, Any]:
678    def as_const(
679        self, eval_ctx: t.Optional[EvalContext] = None
680    ) -> t.Tuple[t.Any, t.Any]:
681        eval_ctx = get_eval_context(self, eval_ctx)
682        return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Keyword(Helper):
685class Keyword(Helper):
686    """A key, value pair for keyword arguments where key is a string."""
687
688    fields = ("key", "value")
689    key: str
690    value: Expr
691
692    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[str, t.Any]:
693        eval_ctx = get_eval_context(self, eval_ctx)
694        return self.key, self.value.as_const(eval_ctx)

A key, value pair for keyword arguments where key is a string.

fields = ('key', 'value')
key: str
value: Expr
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> Tuple[str, Any]:
692    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[str, t.Any]:
693        eval_ctx = get_eval_context(self, eval_ctx)
694        return self.key, self.value.as_const(eval_ctx)
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class CondExpr(Expr):
697class CondExpr(Expr):
698    """A conditional expression (inline if expression).  (``{{
699    foo if bar else baz }}``)
700    """
701
702    fields = ("test", "expr1", "expr2")
703    test: Expr
704    expr1: Expr
705    expr2: t.Optional[Expr]
706
707    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
708        eval_ctx = get_eval_context(self, eval_ctx)
709        if self.test.as_const(eval_ctx):
710            return self.expr1.as_const(eval_ctx)
711
712        # if we evaluate to an undefined object, we better do that at runtime
713        if self.expr2 is None:
714            raise Impossible()
715
716        return self.expr2.as_const(eval_ctx)

A conditional expression (inline if expression). ({{ foo if bar else baz }})

fields = ('test', 'expr1', 'expr2')
test: Expr
expr1: Expr
expr2: Optional[Expr]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
707    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
708        eval_ctx = get_eval_context(self, eval_ctx)
709        if self.test.as_const(eval_ctx):
710            return self.expr1.as_const(eval_ctx)
711
712        # if we evaluate to an undefined object, we better do that at runtime
713        if self.expr2 is None:
714            raise Impossible()
715
716        return self.expr2.as_const(eval_ctx)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
def args_as_const( node: Union[jinja2.nodes._FilterTestCommon, Call], eval_ctx: Optional[EvalContext]) -> Tuple[List[Any], Dict[Any, Any]]:
719def args_as_const(
720    node: t.Union["_FilterTestCommon", "Call"], eval_ctx: t.Optional[EvalContext]
721) -> t.Tuple[t.List[t.Any], t.Dict[t.Any, t.Any]]:
722    args = [x.as_const(eval_ctx) for x in node.args]
723    kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)
724
725    if node.dyn_args is not None:
726        try:
727            args.extend(node.dyn_args.as_const(eval_ctx))
728        except Exception as e:
729            raise Impossible() from e
730
731    if node.dyn_kwargs is not None:
732        try:
733            kwargs.update(node.dyn_kwargs.as_const(eval_ctx))
734        except Exception as e:
735            raise Impossible() from e
736
737    return args, kwargs
class Filter(_FilterTestCommon):
788class Filter(_FilterTestCommon):
789    """Apply a filter to an expression. ``name`` is the name of the
790    filter, the other fields are the same as :class:`Call`.
791
792    If ``node`` is ``None``, the filter is being used in a filter block
793    and is applied to the content of the block.
794    """
795
796    node: t.Optional[Expr]  # type: ignore
797
798    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
799        if self.node is None:
800            raise Impossible()
801
802        return super().as_const(eval_ctx=eval_ctx)

Apply a filter to an expression. name is the name of the filter, the other fields are the same as Call.

If node is None, the filter is being used in a filter block and is applied to the content of the block.

node: Optional[Expr]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
798    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
799        if self.node is None:
800            raise Impossible()
801
802        return super().as_const(eval_ctx=eval_ctx)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Test(_FilterTestCommon):
805class Test(_FilterTestCommon):
806    """Apply a test to an expression. ``name`` is the name of the test,
807    the other field are the same as :class:`Call`.
808
809    .. versionchanged:: 3.0
810        ``as_const`` shares the same logic for filters and tests. Tests
811        check for volatile, async, and ``@pass_context`` etc.
812        decorators.
813    """
814
815    _is_filter = False

Apply a test to an expression. name is the name of the test, the other field are the same as Call.

Changed in version 3.0: as_const shares the same logic for filters and tests. Tests check for volatile, async, and @pass_context etc. decorators.

fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Call(Expr):
818class Call(Expr):
819    """Calls an expression.  `args` is a list of arguments, `kwargs` a list
820    of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`
821    and `dyn_kwargs` has to be either `None` or a node that is used as
822    node for dynamic positional (``*args``) or keyword (``**kwargs``)
823    arguments.
824    """
825
826    fields = ("node", "args", "kwargs", "dyn_args", "dyn_kwargs")
827    node: Expr
828    args: t.List[Expr]
829    kwargs: t.List[Keyword]
830    dyn_args: t.Optional[Expr]
831    dyn_kwargs: t.Optional[Expr]

Calls an expression. args is a list of arguments, kwargs a list of keyword arguments (list of Keyword nodes), and dyn_args and dyn_kwargs has to be either None or a node that is used as node for dynamic positional (*args) or keyword (**kwargs) arguments.

fields = ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
node: Expr
args: List[Expr]
kwargs: List[Keyword]
dyn_args: Optional[Expr]
dyn_kwargs: Optional[Expr]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Getitem(Expr):
834class Getitem(Expr):
835    """Get an attribute or item from an expression and prefer the item."""
836
837    fields = ("node", "arg", "ctx")
838    node: Expr
839    arg: Expr
840    ctx: str
841
842    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
843        if self.ctx != "load":
844            raise Impossible()
845
846        eval_ctx = get_eval_context(self, eval_ctx)
847
848        try:
849            return eval_ctx.environment.getitem(
850                self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)
851            )
852        except Exception as e:
853            raise Impossible() from e

Get an attribute or item from an expression and prefer the item.

fields = ('node', 'arg', 'ctx')
node: Expr
arg: Expr
ctx: str
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
842    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
843        if self.ctx != "load":
844            raise Impossible()
845
846        eval_ctx = get_eval_context(self, eval_ctx)
847
848        try:
849            return eval_ctx.environment.getitem(
850                self.node.as_const(eval_ctx), self.arg.as_const(eval_ctx)
851            )
852        except Exception as e:
853            raise Impossible() from e

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Getattr(Expr):
856class Getattr(Expr):
857    """Get an attribute or item from an expression that is a ascii-only
858    bytestring and prefer the attribute.
859    """
860
861    fields = ("node", "attr", "ctx")
862    node: Expr
863    attr: str
864    ctx: str
865
866    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
867        if self.ctx != "load":
868            raise Impossible()
869
870        eval_ctx = get_eval_context(self, eval_ctx)
871
872        try:
873            return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)
874        except Exception as e:
875            raise Impossible() from e

Get an attribute or item from an expression that is a ascii-only bytestring and prefer the attribute.

fields = ('node', 'attr', 'ctx')
node: Expr
attr: str
ctx: str
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
866    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
867        if self.ctx != "load":
868            raise Impossible()
869
870        eval_ctx = get_eval_context(self, eval_ctx)
871
872        try:
873            return eval_ctx.environment.getattr(self.node.as_const(eval_ctx), self.attr)
874        except Exception as e:
875            raise Impossible() from e

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Slice(Expr):
878class Slice(Expr):
879    """Represents a slice object.  This must only be used as argument for
880    :class:`Subscript`.
881    """
882
883    fields = ("start", "stop", "step")
884    start: t.Optional[Expr]
885    stop: t.Optional[Expr]
886    step: t.Optional[Expr]
887
888    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> slice:
889        eval_ctx = get_eval_context(self, eval_ctx)
890
891        def const(obj: t.Optional[Expr]) -> t.Optional[t.Any]:
892            if obj is None:
893                return None
894            return obj.as_const(eval_ctx)
895
896        return slice(const(self.start), const(self.stop), const(self.step))

Represents a slice object. This must only be used as argument for Subscript.

fields = ('start', 'stop', 'step')
start: Optional[Expr]
stop: Optional[Expr]
step: Optional[Expr]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> slice:
888    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> slice:
889        eval_ctx = get_eval_context(self, eval_ctx)
890
891        def const(obj: t.Optional[Expr]) -> t.Optional[t.Any]:
892            if obj is None:
893                return None
894            return obj.as_const(eval_ctx)
895
896        return slice(const(self.start), const(self.stop), const(self.step))

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Concat(Expr):
899class Concat(Expr):
900    """Concatenates the list of expressions provided after converting
901    them to strings.
902    """
903
904    fields = ("nodes",)
905    nodes: t.List[Expr]
906
907    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
908        eval_ctx = get_eval_context(self, eval_ctx)
909        return "".join(str(x.as_const(eval_ctx)) for x in self.nodes)

Concatenates the list of expressions provided after converting them to strings.

fields = ('nodes',)
nodes: List[Expr]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> str:
907    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
908        eval_ctx = get_eval_context(self, eval_ctx)
909        return "".join(str(x.as_const(eval_ctx)) for x in self.nodes)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Compare(Expr):
912class Compare(Expr):
913    """Compares an expression with some other expressions.  `ops` must be a
914    list of :class:`Operand`\\s.
915    """
916
917    fields = ("expr", "ops")
918    expr: Expr
919    ops: t.List["Operand"]
920
921    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
922        eval_ctx = get_eval_context(self, eval_ctx)
923        result = value = self.expr.as_const(eval_ctx)
924
925        try:
926            for op in self.ops:
927                new_value = op.expr.as_const(eval_ctx)
928                result = _cmpop_to_func[op.op](value, new_value)
929
930                if not result:
931                    return False
932
933                value = new_value
934        except Exception as e:
935            raise Impossible() from e
936
937        return result

Compares an expression with some other expressions. ops must be a list of Operand\s.

fields = ('expr', 'ops')
expr: Expr
ops: List[Operand]
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
921    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
922        eval_ctx = get_eval_context(self, eval_ctx)
923        result = value = self.expr.as_const(eval_ctx)
924
925        try:
926            for op in self.ops:
927                new_value = op.expr.as_const(eval_ctx)
928                result = _cmpop_to_func[op.op](value, new_value)
929
930                if not result:
931                    return False
932
933                value = new_value
934        except Exception as e:
935            raise Impossible() from e
936
937        return result

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Operand(Helper):
940class Operand(Helper):
941    """Holds an operator and an expression."""
942
943    fields = ("op", "expr")
944    op: str
945    expr: Expr

Holds an operator and an expression.

fields = ('op', 'expr')
op: str
expr: Expr
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Mul(BinExpr):
948class Mul(BinExpr):
949    """Multiplies the left with the right node."""
950
951    operator = "*"

Multiplies the left with the right node.

operator: str = '*'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Div(BinExpr):
954class Div(BinExpr):
955    """Divides the left by the right node."""
956
957    operator = "/"

Divides the left by the right node.

operator: str = '/'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class FloorDiv(BinExpr):
960class FloorDiv(BinExpr):
961    """Divides the left by the right node and converts the
962    result into an integer by truncating.
963    """
964
965    operator = "//"

Divides the left by the right node and converts the result into an integer by truncating.

operator: str = '//'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Add(BinExpr):
968class Add(BinExpr):
969    """Add the left to the right node."""
970
971    operator = "+"

Add the left to the right node.

operator: str = '+'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Sub(BinExpr):
974class Sub(BinExpr):
975    """Subtract the right from the left node."""
976
977    operator = "-"

Subtract the right from the left node.

operator: str = '-'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Mod(BinExpr):
980class Mod(BinExpr):
981    """Left modulo right."""
982
983    operator = "%"

Left modulo right.

operator: str = '%'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Pow(BinExpr):
986class Pow(BinExpr):
987    """Left to the power of right."""
988
989    operator = "**"

Left to the power of right.

operator: str = '**'
fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class And(BinExpr):
992class And(BinExpr):
993    """Short circuited AND."""
994
995    operator = "and"
996
997    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
998        eval_ctx = get_eval_context(self, eval_ctx)
999        return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)

Short circuited AND.

operator: str = 'and'
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
997    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
998        eval_ctx = get_eval_context(self, eval_ctx)
999        return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Or(BinExpr):
1002class Or(BinExpr):
1003    """Short circuited OR."""
1004
1005    operator = "or"
1006
1007    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
1008        eval_ctx = get_eval_context(self, eval_ctx)
1009        return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)

Short circuited OR.

operator: str = 'or'
def as_const(self, eval_ctx: Optional[EvalContext] = None) -> Any:
1007    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
1008        eval_ctx = get_eval_context(self, eval_ctx)
1009        return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

fields = ('left', 'right')
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Not(UnaryExpr):
1012class Not(UnaryExpr):
1013    """Negate the expression."""
1014
1015    operator = "not"

Negate the expression.

operator: str = 'not'
fields = ('node',)
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Neg(UnaryExpr):
1018class Neg(UnaryExpr):
1019    """Make the expression negative."""
1020
1021    operator = "-"

Make the expression negative.

operator: str = '-'
fields = ('node',)
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Pos(UnaryExpr):
1024class Pos(UnaryExpr):
1025    """Make the expression positive (noop for most expressions)"""
1026
1027    operator = "+"

Make the expression positive (noop for most expressions)

operator: str = '+'
fields = ('node',)
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class EnvironmentAttribute(Expr):
1033class EnvironmentAttribute(Expr):
1034    """Loads an attribute from the environment object.  This is useful for
1035    extensions that want to call a callback stored on the environment.
1036    """
1037
1038    fields = ("name",)
1039    name: str

Loads an attribute from the environment object. This is useful for extensions that want to call a callback stored on the environment.

fields = ('name',)
name: str
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class ExtensionAttribute(Expr):
1042class ExtensionAttribute(Expr):
1043    """Returns the attribute of an extension bound to the environment.
1044    The identifier is the identifier of the :class:`Extension`.
1045
1046    This node is usually constructed by calling the
1047    :meth:`~jinja2.ext.Extension.attr` method on an extension.
1048    """
1049
1050    fields = ("identifier", "name")
1051    identifier: str
1052    name: str

Returns the attribute of an extension bound to the environment. The identifier is the identifier of the Extension.

This node is usually constructed by calling the ~jinja2.ext.Extension.attr() method on an extension.

fields = ('identifier', 'name')
identifier: str
name: str
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class ImportedName(Expr):
1055class ImportedName(Expr):
1056    """If created with an import name the import name is returned on node
1057    access.  For example ``ImportedName('cgi.escape')`` returns the `escape`
1058    function from the cgi module on evaluation.  Imports are optimized by the
1059    compiler so there is no need to assign them to local variables.
1060    """
1061
1062    fields = ("importname",)
1063    importname: str

If created with an import name the import name is returned on node access. For example ImportedName('cgi.escape') returns the escape function from the cgi module on evaluation. Imports are optimized by the compiler so there is no need to assign them to local variables.

fields = ('importname',)
importname: str
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class InternalName(Expr):
1066class InternalName(Expr):
1067    """An internal name in the compiler.  You cannot create these nodes
1068    yourself but the parser provides a
1069    :meth:`~jinja2.parser.Parser.free_identifier` method that creates
1070    a new identifier for you.  This identifier is not available from the
1071    template and is not treated specially by the compiler.
1072    """
1073
1074    fields = ("name",)
1075    name: str
1076
1077    def __init__(self) -> None:
1078        raise TypeError(
1079            "Can't create internal names.  Use the "
1080            "`free_identifier` method on a parser."
1081        )

An internal name in the compiler. You cannot create these nodes yourself but the parser provides a ~jinja2.parser.Parser.free_identifier() method that creates a new identifier for you. This identifier is not available from the template and is not treated specially by the compiler.

fields = ('name',)
name: str
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class MarkSafe(Expr):
1084class MarkSafe(Expr):
1085    """Mark the wrapped expression as safe (wrap it as `Markup`)."""
1086
1087    fields = ("expr",)
1088    expr: Expr
1089
1090    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> Markup:
1091        eval_ctx = get_eval_context(self, eval_ctx)
1092        return Markup(self.expr.as_const(eval_ctx))

Mark the wrapped expression as safe (wrap it as Markup).

fields = ('expr',)
expr: Expr
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> markupsafe.Markup:
1090    def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> Markup:
1091        eval_ctx = get_eval_context(self, eval_ctx)
1092        return Markup(self.expr.as_const(eval_ctx))

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class MarkSafeIfAutoescape(Expr):
1095class MarkSafeIfAutoescape(Expr):
1096    """Mark the wrapped expression as safe (wrap it as `Markup`) but
1097    only if autoescaping is active.
1098
1099    .. versionadded:: 2.5
1100    """
1101
1102    fields = ("expr",)
1103    expr: Expr
1104
1105    def as_const(
1106        self, eval_ctx: t.Optional[EvalContext] = None
1107    ) -> t.Union[Markup, t.Any]:
1108        eval_ctx = get_eval_context(self, eval_ctx)
1109        if eval_ctx.volatile:
1110            raise Impossible()
1111        expr = self.expr.as_const(eval_ctx)
1112        if eval_ctx.autoescape:
1113            return Markup(expr)
1114        return expr

Mark the wrapped expression as safe (wrap it as Markup) but only if autoescaping is active.

New in version 2.5.

fields = ('expr',)
expr: Expr
def as_const( self, eval_ctx: Optional[EvalContext] = None) -> Union[markupsafe.Markup, Any]:
1105    def as_const(
1106        self, eval_ctx: t.Optional[EvalContext] = None
1107    ) -> t.Union[Markup, t.Any]:
1108        eval_ctx = get_eval_context(self, eval_ctx)
1109        if eval_ctx.volatile:
1110            raise Impossible()
1111        expr = self.expr.as_const(eval_ctx)
1112        if eval_ctx.autoescape:
1113            return Markup(expr)
1114        return expr

Return the value of the expression as constant or raise Impossible if this was not possible.

An EvalContext can be provided, if none is given a default context is created which requires the nodes to have an attached environment.

Changed in version 2.4: the eval_ctx parameter was added.

abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class ContextReference(Expr):
1117class ContextReference(Expr):
1118    """Returns the current template context.  It can be used like a
1119    :class:`Name` node, with a ``'load'`` ctx and will return the
1120    current :class:`~jinja2.runtime.Context` object.
1121
1122    Here an example that assigns the current template name to a
1123    variable named `foo`::
1124
1125        Assign(Name('foo', ctx='store'),
1126               Getattr(ContextReference(), 'name'))
1127
1128    This is basically equivalent to using the
1129    :func:`~jinja2.pass_context` decorator when using the high-level
1130    API, which causes a reference to the context to be passed as the
1131    first argument to a function.
1132    """

Returns the current template context. It can be used like a Name node, with a 'load' ctx and will return the current ~jinja2.runtime.Context object.

Here an example that assigns the current template name to a variable named foo::

Assign(Name('foo', ctx='store'),
       Getattr(ContextReference(), 'name'))

This is basically equivalent to using the ~jinja2.pass_context() decorator when using the high-level API, which causes a reference to the context to be passed as the first argument to a function.

abstract = False
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class DerivedContextReference(Expr):
1135class DerivedContextReference(Expr):
1136    """Return the current template context including locals. Behaves
1137    exactly like :class:`ContextReference`, but includes local
1138    variables, such as from a ``for`` loop.
1139
1140    .. versionadded:: 2.11
1141    """

Return the current template context including locals. Behaves exactly like ContextReference, but includes local variables, such as from a for loop.

New in version 2.11.

abstract = False
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Continue(Stmt):
1144class Continue(Stmt):
1145    """Continue a loop."""

Continue a loop.

abstract = False
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Break(Stmt):
1148class Break(Stmt):
1149    """Break a loop."""

Break a loop.

abstract = False
fields: Tuple[str, ...] = ()
attributes: Tuple[str, ...] = ('lineno', 'environment')
class Scope(Stmt):
1152class Scope(Stmt):
1153    """An artificial scope."""
1154
1155    fields = ("body",)
1156    body: t.List[Node]

An artificial scope.

fields = ('body',)
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class OverlayScope(Stmt):
1159class OverlayScope(Stmt):
1160    """An overlay scope for extensions.  This is a largely unoptimized scope
1161    that however can be used to introduce completely arbitrary variables into
1162    a sub scope from a dictionary or dictionary like object.  The `context`
1163    field has to evaluate to a dictionary object.
1164
1165    Example usage::
1166
1167        OverlayScope(context=self.call_method('get_context'),
1168                     body=[...])
1169
1170    .. versionadded:: 2.10
1171    """
1172
1173    fields = ("context", "body")
1174    context: Expr
1175    body: t.List[Node]

An overlay scope for extensions. This is a largely unoptimized scope that however can be used to introduce completely arbitrary variables into a sub scope from a dictionary or dictionary like object. The context field has to evaluate to a dictionary object.

Example usage::

OverlayScope(context=self.call_method('get_context'),
             body=[...])

New in version 2.10.

fields = ('context', 'body')
context: Expr
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class EvalContextModifier(Stmt):
1178class EvalContextModifier(Stmt):
1179    """Modifies the eval context.  For each option that should be modified,
1180    a :class:`Keyword` has to be added to the :attr:`options` list.
1181
1182    Example to change the `autoescape` setting::
1183
1184        EvalContextModifier(options=[Keyword('autoescape', Const(True))])
1185    """
1186
1187    fields = ("options",)
1188    options: t.List[Keyword]

Modifies the eval context. For each option that should be modified, a Keyword has to be added to the options list.

Example to change the autoescape setting::

EvalContextModifier(options=[Keyword('autoescape', Const(True))])
fields = ('options',)
options: List[Keyword]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')
class ScopedEvalContextModifier(EvalContextModifier):
1191class ScopedEvalContextModifier(EvalContextModifier):
1192    """Modifies the eval context and reverts it later.  Works exactly like
1193    :class:`EvalContextModifier` but will only modify the
1194    :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.
1195    """
1196
1197    fields = ("body",)
1198    body: t.List[Node]

Modifies the eval context and reverts it later. Works exactly like EvalContextModifier but will only modify the ~EvalContext for nodes in the body.

fields = ('options', 'body')
body: List[Node]
abstract = False
attributes: Tuple[str, ...] = ('lineno', 'environment')