Skip to content
Myr

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 file

Take 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 back
1"""Functions that expose information about templates that might be
2interesting for introspection.3"""

Merge branch '3.1.x'David Lordbefore adoptionde61312

line 4why, one step back
4

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 5–6why, one step back
5import typing as t
6

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 7–8why, one step back
7from . import nodes
8from .compiler import CodeGenerator

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 9why, one step back
9from .compiler import Frame

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 10why, one step back
10

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 11–12why, one step back
11if t.TYPE_CHECKING:
12 from .environment import Environment

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 13why, one step back
13

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 14why, one step back
14

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 15–17why, one step back
15class TrackingCodeGenerator(CodeGenerator):
16 """We abuse the code generator for introspection."""17

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 18–19why, one step back
18 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 back
20 self.undeclared_identifiers: set[str] = set()

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 21why, one step back
21

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 22why, one step back
22 def write(self, x: str) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 23–24why, one step back
23 """Don't write."""
24

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 25why, one step back
25 def enter_frame(self, frame: Frame) -> None:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 26why, one step back
26 """Remember all undeclared identifiers."""

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 27–28why, one step back
27 super().enter_frame(frame)
28

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 29–33why, one step back
29 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)3233

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 34why, one step back
34def find_undeclared_variables(ast: nodes.Template) -> set[str]:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 35–52why, one step back
35 """Returns a set of all variables in the AST that will be looked up from
36 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 back
53 codegen = TrackingCodeGenerator(ast.environment) # type: ignore

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 54–57why, one step back
54 codegen.visit(ast)
55 return codegen.undeclared_identifiers5657

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 58why, one step back
58_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 59why, one step back
59_RefType = nodes.Extends | nodes.FromImport | nodes.Import | nodes.Include

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 60–61why, one step back
60
61

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 62why, one step back
62def 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 back
63 """Finds all the referenced templates from the AST. This will return an
64 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 back
77 template_name: t.Any
7879 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 back
83 # a tuple with some non consts in there

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 84–85why, one step back
84 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 back
86 # something const, only yield the strings and ignore
87 # 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 name

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 99–100why, one step back
99 if isinstance(template.value, str):
100 yield template.value

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 101–104why, one step back
101 # 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 back
105 template.value, (tuple, list)

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 106why, one step back
106 ):

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 107why, one step back
107 for template_name in template.value:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 108–112why, one step back
108 if isinstance(template_name, str):
109 yield template_name110 # something else we don't care about, we could warn here111 else:112 yield None