idtracking.py
This file
Path
src/jinja2/idtracking.py
316 lines · 10.2 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 1–3why, one step back1import typing as t23from . import nodesupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 4–5why, one step back4from .visitor import NodeVisitor5Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 6–8why, one step back6if t.TYPE_CHECKING:7 import typing_extensions as te8update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 9–14why, one step back9VAR_LOAD_PARAMETER = "param"10VAR_LOAD_RESOLVE = "resolve"11VAR_LOAD_ALIAS = "alias"12VAR_LOAD_UNDEFINED = "undefined"1314Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 15–17why, one step back15def find_symbols(16 nodes: t.Iterable[nodes.Node], parent_symbols: t.Optional["Symbols"] = None17) -> "Symbols":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 18–24why, one step back18 sym = Symbols(parent=parent_symbols)19 visitor = FrameSymbolVisitor(sym)20 for node in nodes:21 visitor.visit(node)22 return sym2324Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 25–27why, one step back25def symbols_for_node(26 node: nodes.Node, parent_symbols: t.Optional["Symbols"] = None27) -> "Symbols":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 28–33why, one step back28 sym = Symbols(parent=parent_symbols)29 sym.analyze_node(node)30 return sym313233class Symbols:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 34why, one step back34 def __init__(drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 35why, one step back35 self, parent: t.Optional["Symbols"] = None, level: int | None = NoneMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 36why, one step back36 ) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 37–41why, one step back37 if level is None:38 if parent is None:39 level = 040 else:41 level = parent.level + 1Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 42–43why, one step back4243 self.level: int = levelupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 44why, one step back44 self.parent = parentUpdated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 45–47why, one step back45 self.refs: dict[str, str] = {}46 self.loads: dict[str, t.Any] = {}47 self.stores: set[str] = set()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 48why, one step back48Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 49why, one step back49 def analyze_node(self, node: nodes.Node, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 50–52why, one step back50 visitor = RootVisitor(self)51 visitor.visit(node, **kwargs)52drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 53why, one step back53 def _define_ref(self, name: str, load: tuple[str, str | None] | None = None) -> str:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 54–59why, one step back54 ident = f"l_{self.level}_{name}"55 self.refs[name] = ident56 if load is not None:57 self.loads[ident] = load58 return ident59drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 60why, one step back60 def find_load(self, target: str) -> t.Any | None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 61–62why, one step back61 if target in self.loads:62 return self.loads[target]Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 63why, one step back63update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 64–66why, one step back64 if self.parent is not None:65 return self.parent.find_load(target)66Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 67–68why, one step back67 return None68drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 69why, one step back69 def find_ref(self, name: str) -> str | None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 70–71why, one step back70 if name in self.refs:71 return self.refs[name]Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 72why, one step back72update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 73–75why, one step back73 if self.parent is not None:74 return self.parent.find_ref(name)75Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 76–78why, one step back76 return None7778 def ref(self, name: str) -> str:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 79–86why, one step back79 rv = self.find_ref(name)80 if rv is None:81 raise AssertionError(82 "Tried to resolve a name to a reference that was"83 f" unknown to the frame ({name!r})"84 )85 return rv86Merge branch 'stable'David Lordbefore adoption6aeab5d
line 87why, one step back87 def copy(self) -> "te.Self":Merge branch '3.0.x'David Lordbefore adoption896a621
line 88why, one step back88 rv = object.__new__(self.__class__)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 89–94why, one step back89 rv.__dict__.update(self.__dict__)90 rv.refs = self.refs.copy()91 rv.loads = self.loads.copy()92 rv.stores = self.stores.copy()93 return rv94Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 95why, one step back95 def store(self, name: str) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 96–112why, one step back96 self.stores.add(name)9798 # If we have not see the name referenced yet, we need to figure99 # out what to set it to.100 if name not in self.refs:101 # If there is a parent scope we check if the name has a102 # reference there. If it does it means we might have to alias103 # to a variable there.104 if self.parent is not None:105 outer_ref = self.parent.find_ref(name)106 if outer_ref is not None:107 self._define_ref(name, load=(VAR_LOAD_ALIAS, outer_ref))108 return109110 # Otherwise we can just set it to undefined.111 self._define_ref(name, load=(VAR_LOAD_UNDEFINED, None))112Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 113why, one step back113 def declare_parameter(self, name: str) -> str:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 114–116why, one step back114 self.stores.add(name)115 return self._define_ref(name, load=(VAR_LOAD_PARAMETER, None))116Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 117–118why, one step back117 def load(self, name: str) -> None:118 if self.find_ref(name) is None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 119–120why, one step back119 self._define_ref(name, load=(VAR_LOAD_RESOLVE, name))120Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 121why, one step back121 def branch_update(self, branch_symbols: t.Sequence["Symbols"]) -> None:Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 122why, one step back122 stores: set[str] = set()Merge branch 'stable'David Lordbefore adoption6aeab5d
line 123why, one step back123update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 124why, one step back124 for branch in branch_symbols:Merge branch 'stable'David Lordbefore adoption6aeab5d
line 125why, one step back125 stores.update(branch.stores)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 126why, one step back126Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 127–128why, one step back127 stores.difference_update(self.stores)128update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 129–133why, one step back129 for sym in branch_symbols:130 self.refs.update(sym.refs)131 self.loads.update(sym.loads)132 self.stores.update(sym.stores)133Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 134–135why, one step back134 for name in stores:135 target = self.find_ref(name)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 136–144why, one step back136 assert target is not None, "should not happen"137138 if self.parent is not None:139 outer_target = self.parent.find_ref(name)140 if outer_target is not None:141 self.loads[target] = (VAR_LOAD_ALIAS, outer_target)142 continue143 self.loads[target] = (VAR_LOAD_RESOLVE, name)144Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
lines 145–146why, one step back145 def dump_stores(self) -> dict[str, str]:146 rv: dict[str, str] = {}drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 147why, one step back147 node: Symbols | None = selfMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 148why, one step back148update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 149why, one step back149 while node is not None:Merge remote-tracking branch 'origin/3.0.x'David Lordbefore adoption35c69e9
line 150why, one step back150 for name in sorted(node.stores):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 151why, one step back151 if name not in rv:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 152–153why, one step back152 rv[name] = self.find_ref(name) # type: ignore153update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 154why, one step back154 node = node.parentMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 155why, one step back155update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 156–157why, one step back156 return rv157Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 158why, one step back158 def dump_param_targets(self) -> set[str]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 159why, one step back159 rv = set()drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 160why, one step back160 node: Symbols | None = selfMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 161why, one step back161update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 162–165why, one step back162 while node is not None:163 for target, (instr, _) in self.loads.items():164 if instr == VAR_LOAD_PARAMETER:165 rv.add(target)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 166why, one step back166update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 167why, one step back167 node = node.parentMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 168why, one step back168update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 169–172why, one step back169 return rv170171172class RootVisitor(NodeVisitor):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 173why, one step back173 def __init__(self, symbols: "Symbols") -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 174–175why, one step back174 self.sym_visitor = FrameSymbolVisitor(symbols)175Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 176why, one step back176 def _simple_visit(self, node: nodes.Node, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 177–179why, one step back177 for child in node.iter_child_nodes():178 self.sym_visitor.visit(child)179Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 180–186why, one step back180 visit_Template = _simple_visit181 visit_Block = _simple_visit182 visit_Macro = _simple_visit183 visit_FilterBlock = _simple_visit184 visit_Scope = _simple_visit185 visit_If = _simple_visit186 visit_ScopedEvalContextModifier = _simple_visitupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 187why, one step back187Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 188why, one step back188 def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 189–191why, one step back189 for child in node.body:190 self.sym_visitor.visit(child)191Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 192why, one step back192 def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 193–195why, one step back193 for child in node.iter_child_nodes(exclude=("call",)):194 self.sym_visitor.visit(child)195Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 196why, one step back196 def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 197–199why, one step back197 for child in node.body:198 self.sym_visitor.visit(child)199Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 200–202why, one step back200 def visit_For(201 self, node: nodes.For, for_branch: str = "body", **kwargs: t.Any202 ) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 203–215why, one step back203 if for_branch == "body":204 self.sym_visitor.visit(node.target, store_as_param=True)205 branch = node.body206 elif for_branch == "else":207 branch = node.else_208 elif for_branch == "test":209 self.sym_visitor.visit(node.target, store_as_param=True)210 if node.test is not None:211 self.sym_visitor.visit(node.test)212 return213 else:214 raise RuntimeError("Unknown for branch")215Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 216–220why, one step back216 if branch:217 for item in branch:218 self.sym_visitor.visit(item)219220 def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 221–225why, one step back221 for target in node.targets:222 self.sym_visitor.visit(target)223 for child in node.body:224 self.sym_visitor.visit(child)225Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 226–227why, one step back226 def generic_visit(self, node: nodes.Node, *args: t.Any, **kwargs: t.Any) -> None:227 raise NotImplementedError(f"Cannot find symbols for {type(node).__name__!r}")update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 228–232why, one step back228229230class FrameSymbolVisitor(NodeVisitor):231 """A visitor for `Frame.inspect`."""232Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 233why, one step back233 def __init__(self, symbols: "Symbols") -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 234–235why, one step back234 self.symbols = symbols235Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 236–238why, one step back236 def visit_Name(237 self, node: nodes.Name, store_as_param: bool = False, **kwargs: t.Any238 ) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 239–246why, one step back239 """All assignments to names go through this function."""240 if store_as_param or node.ctx == "param":241 self.symbols.declare_parameter(node.name)242 elif node.ctx == "store":243 self.symbols.store(node.name)244 elif node.ctx == "load":245 self.symbols.load(node.name)246Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 247why, one step back247 def visit_NSRef(self, node: nodes.NSRef, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 248–249why, one step back248 self.symbols.load(node.name)249Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 250why, one step back250 def visit_If(self, node: nodes.If, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 251–253why, one step back251 self.visit(node.test, **kwargs)252 original_symbols = self.symbols253Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 254why, one step back254 def inner_visit(nodes: t.Iterable[nodes.Node]) -> "Symbols":update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 255why, one step back255 self.symbols = rv = original_symbols.copy()Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 256why, one step back256update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 257–258why, one step back257 for subnode in nodes:258 self.visit(subnode, **kwargs)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 259why, one step back259update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 260–267why, one step back260 self.symbols = original_symbols261 return rv262263 body_symbols = inner_visit(node.body)264 elif_symbols = inner_visit(node.elif_)265 else_symbols = inner_visit(node.else_ or ())266 self.symbols.branch_update([body_symbols, elif_symbols, else_symbols])267Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 268why, one step back268 def visit_Macro(self, node: nodes.Macro, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 269–270why, one step back269 self.symbols.store(node.name)270Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 271why, one step back271 def visit_Import(self, node: nodes.Import, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 272–274why, one step back272 self.generic_visit(node, **kwargs)273 self.symbols.store(node.target)274Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 275why, one step back275 def visit_FromImport(self, node: nodes.FromImport, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 276why, one step back276 self.generic_visit(node, **kwargs)Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 277why, one step back277update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 278–283why, one step back278 for name in node.names:279 if isinstance(name, tuple):280 self.symbols.store(name[1])281 else:282 self.symbols.store(name)283Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 284why, one step back284 def visit_Assign(self, node: nodes.Assign, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 285–288why, one step back285 """Visit assignments in the correct order."""286 self.visit(node.node, **kwargs)287 self.visit(node.target, **kwargs)288Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 289why, one step back289 def visit_For(self, node: nodes.For, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 290–294why, one step back290 """Visiting stops at for blocks. However the block sequence291 is visited as part of the outer scope.292 """293 self.visit(node.iter, **kwargs)294Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 295why, one step back295 def visit_CallBlock(self, node: nodes.CallBlock, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 296–297why, one step back296 self.visit(node.call, **kwargs)297Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 298why, one step back298 def visit_FilterBlock(self, node: nodes.FilterBlock, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 299–300why, one step back299 self.visit(node.filter, **kwargs)300Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 301why, one step back301 def visit_With(self, node: nodes.With, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 302–304why, one step back302 for target in node.values:303 self.visit(target)304Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 305why, one step back305 def visit_AssignBlock(self, node: nodes.AssignBlock, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 306–308why, one step back306 """Stop visiting at block assigns."""307 self.visit(node.target, **kwargs)308Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 309why, one step back309 def visit_Scope(self, node: nodes.Scope, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 310–311why, one step back310 """Stop visiting at scopes."""311Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 312why, one step back312 def visit_Block(self, node: nodes.Block, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 313–314why, one step back313 """Stop visiting at blocks."""314Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 315why, one step back315 def visit_OverlayScope(self, node: nodes.OverlayScope, **kwargs: t.Any) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 316why, one step back316 """Do not visit into overlay scopes."""