gh-60191: Implement ast.compare by isidentical · Pull Request #19211 · python/cpython

hansrajdas

@isidentical

hansrajdas

@isidentical

iritkatriel

@Julian Julian mannequin mentioned this pull request

Apr 10, 2022

@AlexWaygood

@AlexWaygood AlexWaygood changed the title bpo-15987: Implement ast.compare gh-60191: Implement ast.compare

May 11, 2024

@jeremyhylton

Use separate helper functions to compare _fields and _attributes, because
_attributes are always simple strings.

Move the type comparison test to the point where it is relevant,
rather than making unnecessary type tests.

Add comments to explain the logic in more detail.

jeremyhylton

@jeremyhylton

The basic functionality here is to compare asts recursively. If
compare_fields is False, then the comparison is not recursive. It just
compares that the top-level AST objects are the same and have the same
attributes. This option doesn't seem interesting enough to offer as a
feature.

Revise the docstring for compare() to explain the options in more detail.

@jeremyhylton

@jeremyhylton

iritkatriel

@jeremyhylton

iritkatriel

In practice the primary effect of this change was for Constant() nodes
where the value of two constants were equal without being the same
literal type, e.g. ast.compare(Constant(1), Constant(True),
compare_types=True) was True.

This behavior doesn't seem like it has a very strong motivation, and
adds some complexity. We can add it back later if there's a clear need
for it.
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
The comparison fundamentally depends on _fields and _attributes, which
could be modified by the user. It's not clear that such modifications
are sensible or supported by the API, but we can at least make sure
comparison doesn't silently ignore those comparisons.

Also pass a and b as arguments to helper methods instead of using them
from the enclosing scope.
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>

iritkatriel

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>

iritkatriel

@iritkatriel

iritkatriel

@jeremyhylton

@jeremyhylton

JelleZijlstra

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>

@jeremyhylton

@jeremyhylton

JelleZijlstra

@jeremyhylton @JelleZijlstra

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>

JelleZijlstra

@jeremyhylton

@jeremyhylton

estyxx pushed a commit to estyxx/cpython that referenced this pull request

Jul 17, 2024
* bpo-15987: Implement ast.compare

Add a compare() function that compares two ASTs for structural equality. There are two set of attributes on AST node objects, fields and attributes. The fields are always compared, since they represent the actual structure of the code. The attributes can be optionally be included in the comparison. Attributes capture things like line numbers of column offsets, so comparing them involves test whether the layout of the program text is the same. Since whitespace seems inessential for comparing ASTs, the default is to compare fields but not attributes.

ASTs are just Python objects that can be modified in arbitrary ways. The API for ASTs is under-specified in the presence of user modifications to objects. The comparison respects modifications to fields and attributes, and to _fields and _attributes attributes. A user could create obviously malformed objects, and the code will probably fail with an AttributeError when that happens. (For example, adding "spam" to _fields but not adding a "spam" attribute to the object.) 

Co-authored-by: Jeremy Hylton <jeremy@alum.mit.edu>