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)
| 32 | |
| 33 | |
| 34 | def 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) |
nothing calls this directly
no test coverage detected