meta.py
This file
Path
src/jinja2/meta.py
112 lines · 4.3 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"""Functions that expose information about templates that might be2interesting for 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 . import nodes8from .compiler import CodeGeneratorMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 9why, one step back9from .compiler import Frameupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 10why, one step back10Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 11–12why, one step back11if t.TYPE_CHECKING:12 from .environment import Environmentupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 13why, one step back13Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 14why, one step back14update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 15–17why, one step back15class TrackingCodeGenerator(CodeGenerator):16 """We abuse the code generator for introspection."""17Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 18–19why, one step back18 def __init__(self, environment: "Environment") -> None:19 super().__init__(environment, "<introspection>", "<introspection>")Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 20why, one step back20 self.undeclared_identifiers: set[str] = set()update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 21why, one step back21Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 22why, one step back22 def write(self, x: str) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 23–24why, one step back23 """Don't write."""24Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 25why, one step back25 def enter_frame(self, frame: Frame) -> None:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 26why, one step back26 """Remember all undeclared identifiers."""Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 27–28why, one step back27 super().enter_frame(frame)28update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 29–33why, one step back29 for _, (action, param) in frame.symbols.loads.items():30 if action == "resolve" and param not in self.environment.globals:31 self.undeclared_identifiers.add(param)3233Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 34why, one step back34def find_undeclared_variables(ast: nodes.Template) -> set[str]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 35–52why, one step back35 """Returns a set of all variables in the AST that will be looked up from36 the context at runtime. Because at compile time it's not known which37 variables will be used depending on the path the execution takes at38 runtime, all variables are returned.3940 >>> from jinja2 import Environment, meta41 >>> env = Environment()42 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')43 >>> meta.find_undeclared_variables(ast) == {'bar'}44 True4546 .. admonition:: Implementation4748 Internally the code generator is used for finding undeclared variables.49 This is good to know because the code generator might raise a50 :exc:`TemplateAssertionError` during compilation and as a matter of51 fact this function can currently raise that exception as well.52 """Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 53why, one step back53 codegen = TrackingCodeGenerator(ast.environment) # type: ignoreupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 54–57why, one step back54 codegen.visit(ast)55 return codegen.undeclared_identifiers5657Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 58why, one step back58_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 59why, one step back59_RefType = nodes.Extends | nodes.FromImport | nodes.Import | nodes.IncludeMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 60–61why, one step back6061drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 62why, one step back62def find_referenced_templates(ast: nodes.Template) -> t.Iterator[str | None]:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 63–76why, one step back63 """Finds all the referenced templates from the AST. This will return an64 iterator over all the hardcoded template extensions, inclusions and65 imports. If dynamic inheritance or inclusion is used, `None` will be66 yielded.6768 >>> from jinja2 import Environment, meta69 >>> env = Environment()70 >>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')71 >>> list(meta.find_referenced_templates(ast))72 ['layout.html', None]7374 This function is useful for dependency tracking. For example if you want75 to rebuild parts of the website after a layout template has changed.76 """Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 77–82why, one step back77 template_name: t.Any7879 for node in ast.find_all(_ref_types):80 template: nodes.Expr = node.template # type: ignore8182 if not isinstance(template, nodes.Const):update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 83why, one step back83 # a tuple with some non consts in thereMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 84–85why, one step back84 if isinstance(template, (nodes.Tuple, nodes.List)):85 for template_name in template.items:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 86–98why, one step back86 # something const, only yield the strings and ignore87 # non-string consts that really just make no sense88 if isinstance(template_name, nodes.Const):89 if isinstance(template_name.value, str):90 yield template_name.value91 # something dynamic in there92 else:93 yield None94 # something dynamic we don't know about here95 else:96 yield None97 continue98 # constant is a basestring, direct template nameMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 99–100why, one step back99 if isinstance(template.value, str):100 yield template.valueupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 101–104why, one step back101 # a tuple or list (latter *should* not happen) made of consts,102 # yield the consts that are strings. We could warn here for103 # non string values104 elif isinstance(node, nodes.Include) and isinstance(Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 105why, one step back105 template.value, (tuple, list)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 106why, one step back106 ):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 107why, one step back107 for template_name in template.value:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 108–112why, one step back108 if isinstance(template_name, str):109 yield template_name110 # something else we don't care about, we could warn here111 else:112 yield None