summaryrefslogtreecommitdiff
path: root/src/jinja2/nodes.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/jinja2/nodes.py')
-rw-r--r--src/jinja2/nodes.py46
1 files changed, 16 insertions, 30 deletions
diff --git a/src/jinja2/nodes.py b/src/jinja2/nodes.py
index e2351bd..b91f6bf 100644
--- a/src/jinja2/nodes.py
+++ b/src/jinja2/nodes.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
"""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.
@@ -18,7 +17,11 @@ _binop_to_func = {
"-": operator.sub,
}
-_uaop_to_func = {"not": operator.not_, "+": operator.pos, "-": operator.neg}
+_uaop_to_func = {
+ "not": operator.not_,
+ "+": operator.pos,
+ "-": operator.neg,
+}
_cmpop_to_func = {
"eq": operator.eq,
@@ -53,7 +56,7 @@ class NodeType(type):
return type.__new__(mcs, name, bases, d)
-class EvalContext(object):
+class EvalContext:
"""Holds evaluation time information. Custom attributes can be attached
to it in extensions.
"""
@@ -78,9 +81,8 @@ def get_eval_context(node, ctx):
if ctx is None:
if node.environment is None:
raise RuntimeError(
- "if no eval context is passed, the "
- "node must have an attached "
- "environment."
+ "if no eval context is passed, the node must have an"
+ " attached environment."
)
return EvalContext(node.environment)
return ctx
@@ -113,21 +115,17 @@ class Node(metaclass=NodeType):
if fields:
if len(fields) != len(self.fields):
if not self.fields:
- raise TypeError("%r takes 0 arguments" % self.__class__.__name__)
+ raise TypeError(f"{self.__class__.__name__!r} takes 0 arguments")
raise TypeError(
- "%r takes 0 or %d argument%s"
- % (
- self.__class__.__name__,
- len(self.fields),
- len(self.fields) != 1 and "s" or "",
- )
+ f"{self.__class__.__name__!r} takes 0 or {len(self.fields)}"
+ f" argument{'s' if len(self.fields) != 1 else ''}"
)
for name, arg in zip(self.fields, fields):
setattr(self, name, arg)
for attr in self.attributes:
setattr(self, attr, attributes.pop(attr, None))
if attributes:
- raise TypeError("unknown attribute %r" % next(iter(attributes)))
+ raise TypeError(f"unknown attribute {next(iter(attributes))!r}")
def iter_fields(self, exclude=None, only=None):
"""This method iterates over all fields that are defined and yields
@@ -174,8 +172,7 @@ class Node(metaclass=NodeType):
for child in self.iter_child_nodes():
if isinstance(child, node_type):
yield child
- for result in child.find_all(node_type):
- yield result
+ yield from child.find_all(node_type)
def set_ctx(self, ctx):
"""Reset the context of a node and all child nodes. Per default the
@@ -221,10 +218,8 @@ class Node(metaclass=NodeType):
return hash(tuple(self.iter_fields()))
def __repr__(self):
- return "%s(%s)" % (
- self.__class__.__name__,
- ", ".join("%s=%r" % (arg, getattr(self, arg, None)) for arg in self.fields),
- )
+ args_str = ", ".join(f"{a}={getattr(self, a, None)!r}" for a in self.fields)
+ return f"{self.__class__.__name__}({args_str})"
def dump(self):
def _dump(node):
@@ -232,7 +227,7 @@ class Node(metaclass=NodeType):
buf.append(repr(node))
return
- buf.append("nodes.%s(" % node.__class__.__name__)
+ buf.append(f"nodes.{node.__class__.__name__}(")
if not node.fields:
buf.append(")")
return
@@ -809,15 +804,6 @@ class Operand(Helper):
fields = ("op", "expr")
-if __debug__:
- Operand.__doc__ += "\nThe following operators are available: " + ", ".join(
- sorted(
- "``%s``" % x
- for x in set(_binop_to_func) | set(_uaop_to_func) | set(_cmpop_to_func)
- )
- )
-
-
class Mul(BinExpr):
"""Multiplies the left with the right node."""