Documentation of ast.walk misleadingly characterizes its behavior as recursive when it's really iterative
未关闭
还没有人认领这个 Issue。
docs
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中链接的 ast.walk 文档开始,将其措辞与 Lib/ast.py 中的实现进行比较。决定是只进行最小限度的措辞修正,还是同时记录广度优先顺序;文档应准确描述现有行为,而不添加未被请求的 API 更改。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- documentation
- Issue 类型
- 文档
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100