File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed
Misc/NEWS.d/next/Documentation Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -316,7 +316,7 @@ and classes for traversing abstract syntax trees:
316
316
class RewriteName(NodeTransformer):
317
317
318
318
def visit_Name(self, node):
319
- return copy_location( Subscript(
319
+ return Subscript(
320
320
value=Name(id='data', ctx=Load()),
321
321
slice=Index(value=Constant(value=node.id)),
322
322
ctx=node.ctx
@@ -330,6 +330,14 @@ and classes for traversing abstract syntax trees:
330
330
statement nodes), the visitor may also return a list of nodes rather than
331
331
just a single node.
332
332
333
+ If :class: `NodeTransformer ` introduces new nodes (that weren't part of
334
+ original tree) without giving them location information (such as
335
+ :attr: `lineno `), :func: `fix_missing_locations ` should be called with
336
+ the new sub-tree to recalculate the location information::
337
+
338
+ tree = ast.parse('foo', mode='eval')
339
+ new_tree = fix_missing_locations(RewriteName().visit(tree))
340
+
333
341
Usually you use the transformer like this::
334
342
335
343
node = YourTransformer().visit(node)
Original file line number Diff line number Diff line change
1
+ In the :mod: `ast ` module documentation, fix a misleading ``NodeTransformer `` example and add
2
+ advice on when to use the ``fix_missing_locations `` function.
You can’t perform that action at this time.
0 commit comments