On this page
  1. ast.fix_missing_locations
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from ast import fix_missing_locations
help(fix_missing_locations)

Linked from

Related notes