Documentation of ast.walk misleadingly characterizes its behavior as recursive when it's really iterative
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Documentation
Although the documentation claims that ast.walk generates nodes "in no specified order" and even describes its behavior as "Recursively yield all descendant nodes...", a look at the source code reveals that it really performs a breadth-first traversal by iteratively yielding child nodes in a queue:
def walk(node):
"""
Recursively yield all descendant nodes in the tree starting at *node*
(including *node* itself), in no specified order. This is useful if you
only want to modify nodes in place and don't care about the context.
"""
from collections import deque
todo = deque([node])
while todo:
node = todo.popleft()
todo.extend(iter_child_nodes(node))
yield node
I thought that maybe this function used to be recursive but found that it has not been modified since its first appearance in CPython 2.6.3.
I think we should at the minimum remove the wording "Recursively" from the description, and optionally:
- Clarify the actual ordering by changing "in no specified order" to "in breadth-first order".
- Add a keyword argument such as
depth_firstthat defaults toFalsesuch that when it is true, switches to a depth-first traversal that behaves like:
def dfs_walk(node):
yield node
for child in ast.iter_child_nodes(node):
yield from dfs_walk(child)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ast.walk documentation linked in the issue and compare its wording with the implementation in Lib/ast.py. Decide whether to make the minimum wording correction or also document breadth-first ordering; the documentation should accurately describe the existing behavior without adding an unrequested API change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100