MCPcopy
hub / github.com/pallets/jinja / find_undeclared_variables

Function find_undeclared_variables

src/jinja2/meta.py:34–55  ·  view source on GitHub ↗

Returns a set of all variables in the AST that will be looked up from the context at runtime. Because at compile time it's not known which variables will be used depending on the path the execution takes at runtime, all variables are returned. >>> from jinja2 import Environment, me

(ast: nodes.Template)

Source from the content-addressed store, hash-verified

32
33
34def find_undeclared_variables(ast: nodes.Template) -> t.Set[str]:
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 which
37 variables will be used depending on the path the execution takes at
38 runtime, all variables are returned.
39
40 >>> from jinja2 import Environment, meta
41 >>> env = Environment()
42 >>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')
43 >>> meta.find_undeclared_variables(ast) == {'bar'}
44 True
45
46 .. admonition:: Implementation
47
48 Internally the code generator is used for finding undeclared variables.
49 This is good to know because the code generator might raise a
50 :exc:`TemplateAssertionError` during compilation and as a matter of
51 fact this function can currently raise that exception as well.
52 """
53 codegen = TrackingCodeGenerator(ast.environment) # type: ignore
54 codegen.visit(ast)
55 return codegen.undeclared_identifiers
56
57
58_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)

Callers

nothing calls this directly

Calls 2

visitMethod · 0.80

Tested by

no test coverage detected