This patch adds two things to the _ast module: * Nodes can be initialized with keyword arguments: m = _ast.Module(body=[...]) * Only attributes that are in _fields or _attributes can be set on nodes. Martin, what do you think?
> On second thought, restricting node attributes may prevent custom AST > processing tools from adding useful information themselves... Indeed. If anything is to be checked, it should be whether the child nodes or attributes have the right types. However, that should rather be done in a recursive check function on _mod, which then would also check whether all necessary fields have been set. Or, such checks could be delayed until actual compilation of the tree is attempted (i.e. conversion to the C AST structures). As for being able to pass constructor arguments: I'd expect them to be positional arguments, not keyword arguments - anybody creating AST nodes would normally have Python.asdl open, no?
Okay, I'm dropping the attribute restriction part. Attaching new patch that allows creating nodes with fields (not attributes) as positional arguments, and setting all keyword arguments as attributes on self.