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