summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>2020-01-12 20:38:53 (GMT)
committerPablo Galindo <Pablogsal@gmail.com>2020-01-12 20:38:53 (GMT)
commit6680f4a9f5d15ab82b2ab6266c6f917cb78c919a (patch)
tree99ae78971b66ce6b06de110a95ce3b06815d3fec /Doc/library
parent9f3fc6c5b4993f2b362263b494f84793a21aa073 (diff)
downloadcpython-6680f4a9f5d15ab82b2ab6266c6f917cb78c919a.zip
cpython-6680f4a9f5d15ab82b2ab6266c6f917cb78c919a.tar.gz
cpython-6680f4a9f5d15ab82b2ab6266c6f917cb78c919a.tar.bz2
bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/ast.rst10
1 files changed, 9 insertions, 1 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index c380a81..2cee873 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -316,7 +316,7 @@ and classes for traversing abstract syntax trees:
class RewriteName(NodeTransformer):
def visit_Name(self, node):
- return copy_location(Subscript(
+ return Subscript(
value=Name(id='data', ctx=Load()),
slice=Index(value=Constant(value=node.id)),
ctx=node.ctx
@@ -330,6 +330,14 @@ and classes for traversing abstract syntax trees:
statement nodes), the visitor may also return a list of nodes rather than
just a single node.
+ If :class:`NodeTransformer` introduces new nodes (that weren't part of
+ original tree) without giving them location information (such as
+ :attr:`lineno`), :func:`fix_missing_locations` should be called with
+ the new sub-tree to recalculate the location information::
+
+ tree = ast.parse('foo', mode='eval')
+ new_tree = fix_missing_locations(RewriteName().visit(tree))
+
Usually you use the transformer like this::
node = YourTransformer().visit(node)