Idea
To have a function in the ast
standard library to iterate only the nodes of type “statement” in an ordered manner, which basically would mean to easily iterate the nodes representing the codelines.
Motivation
The ast
library allows to use ast.walk
to traverse the tree and “unroll” all its nodes. There is also NodeVisitor
, and they are really useful.
But there can be some use cases in which one may want to just iterate through the nodes of type “Statement”, i.e. the actual codelines, in an ordered manner.
For example, at times I shall develop some simple static code analyzers and I want to iterate over the lines of a function in an ordered manner.
Now, as a regular developer I would say that this is my need and I would develop a function like the one proposed and use it for myself.
Still, the function I’m proposing came out after some experience of mine working with the ast
library, but at the beginning I mainly relied on iterating through statements in the .body
attribute that several types of nodes have. Which I personally consider a wrong practice, since there can be other attributes for some node types which lead to lists of statements, like attributes representing else
or finally
inside ast.Try
types of nodes.
So I would propose the function below, providing developers who approach the Abstract Syntax Tree for the first time with a standardized function to iterate statements/codelines.
Proposal
A function like the following one:
def iter_stmts(node: ast.AST) -> list[ast.stmt]:
stmts: list[ast.stmt] = [subnode for subnode in ast.walk(node) if isinstance(subnode, ast.stmt)]
stmts.sort(key = lambda n: n.lineno)
return stmts
which would actually take a node from the Abstract Syntax Tree and return all its descendants of type ast.stmt
once sorted.
Considerations
I propose this but of course I have some doubts on my own. I mean adding additional stuff to a codebase could have a price, it’s always additional code to mantain.
I wouldn’t have a clear idea for the name of the function, something like iter_stmts
, iter_codelines
or iter_statements
.
Could a function like this be useful to other developers? I don’t know, but I use it across some projects both personal and both for work, so speaking for myself I use this.
It would be useful also for me if somebody really expert in the ast
library evaluated this function for me.
I’m sharing this idea of mine in case it could benefit other people and, as I already said, developers who are approaching the ast
and have a similar use case, before they just iterate over .body
forgetting about other attributes. I feel like a standardized function could be useful, but maybe I’m wrong.
Thanks in advance to the people who are reading this, and for all the work done on the Python programming language.