[Python-Dev] Two laments about CPython's AST Nodes

Benjamin Peterson benjamin at python.org
Wed Aug 19 22:06:52 CEST 2009

2009/8/19 Frank Wierzbicki <fwierzbicki at gmail.com>:
> Before I start complaining, I want to mention what a huge help it has
> been to be able to directly compare the AST exposed by ast.py in
> making Jython a better Python.  Thanks for that!
> Now on to the complaints: Though I recently added support for this in
> Jython, I don't like that nodes can be defined without required
> attributes, for example:
> node = ast.Assign()
> Is valid, even though it requires "node.targets" and "node.value" (I'm
> less concerned about the required lineno and col_offset, as I can
> understand holding off on these so that you can just use
> fix_missing_locations to fill these in for you).


> My other (bigger) gripe is that you can define these values with
> arbitrary objects that will blow up at parse time.  So for example you
> can write:
> node = ast.Assign()
> node.targets = "whatever"
> Which, when you try to parse, blows up with "TypeError: Assign field
> "targets" must be a list, not a str".  I'd be much happier if this
> blew up right away when you try to make the assignment.  At the
> moment, this is how it works in Jython (though I could support this
> with some contorting).

I also think this is a good idea, but this also causes an asymmetry. I
would still be able to do this:

node = ast.Module([])
node.body.append("random stuff")

and not have it type checked until it is compiled. This would be hard
to fix, though, and I think it is worth living with.

> BTW -- I *am* volunteering to attempt to implement these things in
> CPython if there is support :)

Very generous. :)


More information about the Python-Dev mailing list