When walking an ast it impossible to know the type of an empty list without
writing down some giant lookup from node types and field names to field

More concretely it would nice be to able to programatically visit all
blocks (stmt*)  without having to something like:

class BlockVisitor(NodeVisitor):
    def visit_If(self, node: If):

    def visit_FunctionDef(self, node: FunctionDef):
        for field, value in iter_fields(node):
            if field == 'body':
                # the implementation of generic_visit
Now it turns out that all fields that are lists and are named "body",
"orelse", or "finalbody" are stmt* and only such fields are stmt*.  A rule
could also be synthesized to identify expr* and so forth but this seems
incredibly hacky to me.

It would be much cleaner if <type>* were actual nodes in the ast. E.g.
something like:
class ast_list(AST, MutableSequence[T_co]): ...
class StmtList(ast_list[stmt]): ...
class ExprList(ast_list[expr]): ...
class FunctionDef(stmt):
    name: identifier
    args: arguments
    body: StmtList
    decorator_list: ExprList
    returns: Optional[expr]
This would not change the behavior or structure in any way other than
tagging <type>* and allowing <type>* to be visited.

It would potentially break old code which relies on stuff like `if
isinstance(node.field, list)` e.g. the implementation of generic_visit.

Caleb Donovick
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
Message archived at 
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to