visitor.py
This file
Path
src/jinja2/visitor.py
92 lines · 3.5 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 1–3why, one step back1"""API for traversing the AST nodes. Implemented by the compiler and2meta introspection.3"""Merge branch '3.1.x'David Lordbefore adoptionde61312
line 4why, one step back4Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 5–6why, one step back5import typing as t6update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 7–8why, one step back7from .nodes import Node8Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 9–10why, one step back9if t.TYPE_CHECKING:10 import typing_extensions as teupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 11why, one step back11Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 12why, one step back12 class VisitCallable(te.Protocol):Merge branch '3.1.x'David Lordbefore adoptionde61312
line 13why, one step back13 def __call__(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any: ...Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 14–15why, one step back1415update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 16–27why, one step back16class NodeVisitor:17 """Walks the abstract syntax tree and call visitor functions for every18 node found. The visitor functions may return values which will be19 forwarded by the `visit` method.2021 Per default the visitor functions for the nodes are ``'visit_'`` +22 class name of the node. So a `TryFinally` node visit function would23 be `visit_TryFinally`. This behavior can be changed by overriding24 the `get_visitor` function. If no visitor function exists for a node25 (return value `None`) the `generic_visit` visitor is used instead.26 """27drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 28why, one step back28 def get_visitor(self, node: Node) -> "VisitCallable | None":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 29–32why, one step back29 """Return the visitor function for this node or `None` if no visitor30 exists for this node. In that case the generic visit function is31 used instead.32 """Merge branch '3.0.x'David Lordbefore adoption896a621
line 33why, one step back33 return getattr(self, f"visit_{type(node).__name__}", None)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 34why, one step back34Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 35why, one step back35 def visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 36–37why, one step back36 """Visit a node."""37 f = self.get_visitor(node)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 38why, one step back38update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 39–40why, one step back39 if f is not None:40 return f(node, *args, **kwargs)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 41why, one step back41update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 42–43why, one step back42 return self.generic_visit(node, *args, **kwargs)43Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 44why, one step back44 def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.Any:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 45why, one step back45 """Called if no explicit visitor function exists for a node."""Merge branch '3.1.x'David Lordbefore adoptiona0dd775
lines 46–47why, one step back46 for child_node in node.iter_child_nodes():47 self.visit(child_node, *args, **kwargs)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 48–60why, one step back484950class NodeTransformer(NodeVisitor):51 """Walks the abstract syntax tree and allows modifications of nodes.5253 The `NodeTransformer` will walk the AST and use the return value of the54 visitor functions to replace or remove the old node. If the return55 value of the visitor function is `None` the node will be removed56 from the previous location otherwise it's replaced with the return57 value. The return value may be the original node in which case no58 replacement takes place.59 """60Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 61why, one step back61 def generic_visit(self, node: Node, *args: t.Any, **kwargs: t.Any) -> Node:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 62–82why, one step back62 for field, old_value in node.iter_fields():63 if isinstance(old_value, list):64 new_values = []65 for value in old_value:66 if isinstance(value, Node):67 value = self.visit(value, *args, **kwargs)68 if value is None:69 continue70 elif not isinstance(value, Node):71 new_values.extend(value)72 continue73 new_values.append(value)74 old_value[:] = new_values75 elif isinstance(old_value, Node):76 new_node = self.visit(old_value, *args, **kwargs)77 if new_node is None:78 delattr(node, field)79 else:80 setattr(node, field, new_node)81 return node82Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 83why, one step back83 def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> list[Node]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 84–87why, one step back84 """As transformers may return lists in some places this method85 can be used to enforce a list as return value.86 """87 rv = self.visit(node, *args, **kwargs)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 88why, one step back88update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 89why, one step back89 if not isinstance(rv, list):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 90–91why, one step back90 return [rv]91update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 92why, one step back92 return rv