Skip to content

Commit 7134861

Browse files
committed
bpo-3530: advice fix_missing_locations usage on new node introduction with NodeTransformer
1 parent e8acc86 commit 7134861

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Doc/library/ast.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ and classes for traversing abstract syntax trees:
300300
class RewriteName(NodeTransformer):
301301

302302
def visit_Name(self, node):
303-
return copy_location(Subscript(
303+
return Subscript(
304304
value=Name(id='data', ctx=Load()),
305305
slice=Index(value=Constant(value=node.id)),
306306
ctx=node.ctx
@@ -314,6 +314,11 @@ and classes for traversing abstract syntax trees:
314314
statement nodes), the visitor may also return a list of nodes rather than
315315
just a single node.
316316

317+
If :class:`NodeTransformer` introduces new nodes (that weren't part of
318+
original tree) without giving them location information (such as
319+
:attr:`lineno`), :func:`fix_missing_locations` should be called with
320+
new tree to apply location information for compiler.
321+
317322
Usually you use the transformer like this::
318323

319324
node = YourTransformer().visit(node)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In ast doc, fix misleading NodeTransformer example and add
2+
fix_missing_locations advice.

0 commit comments

Comments
 (0)