[New-bugs-announce] [issue15987] Provide a way to compare AST nodes for equality recursively

Julian Berman report at bugs.python.org
Thu Sep 20 20:43:57 CEST 2012

New submission from Julian Berman:

As is, as far as I can tell, there's no way to easily compare two AST nodes to see if they have the same children and same fields (recursively).

I'm writing some unit tests for a NodeTransformers, so I've settled for comparing `ast.dump()`s of each, which is kind of dirty, but 1) works and 2) produces reasonable failure messages. (As a side note of what else I've tried, comparing, say, a list of the `iter_child_nodes` is not a good alternative, since the tests I'm writing are making assertions that a given node was not modified, which means they deepcopy the node and then want to assert that the "transformed" node is unchanged.)

I don't know the global implications of changing ast.AST.__eq__ to know if that's feasible (hopefully someone will comment on that), but if it isn't, another provided way would be nice.

components: Library (Lib)
messages: 170826
nosy: Julian
priority: normal
severity: normal
status: open
title: Provide a way to compare AST nodes for equality recursively
type: enhancement
versions: Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list