ast.fix_missing_locations
Prev: ast.dump | Up: ast | Next: ast.get_docstring
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.fix_missing_locations
Tags: #public #python3 #stdlib #ast
Import
import ast
from ast import fix_missing_locations
Signature & Overview
ast.fix_missing_locations(node)
When you compile a node tree with compile(), the compiler expects lineno and col_offset attributes for every node that supports them. This is rather tedious to fill in for generated nodes, so this helper adds these attributes
Usage
import ast
tree = ast.Module(body=[ast.Expr(value=ast.Constant(42))], type_ignores=[])
ast.fix_missing_locations(tree)
assert compile(tree, "<mem>", "exec") is not None
Gotchas
- Required before
compile()on synthesized/modified ASTs: Fills in missinglineno/col_offsetfrom parent nodes socompile()doesn't fail.
REPL Inspect
from ast import fix_missing_locations
help(fix_missing_locations)
Linked from
Related notes
- tuts/python3/stdlib_module/ast/dump Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/get_docstring Linked here; links to this page; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/Add Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/alias Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/And Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AnnAssign Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/AST Linked here; shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast/arg shared tags: #ast, #stdlib
- tuts/python3/stdlib_module/ast/arguments shared tags: #ast, #stdlib