-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-60191: Implement ast.compare #19211
Conversation
isidentical
commented
Mar 28, 2020
•
edited by AlexWaygood
Loading
edited by AlexWaygood
- Issue: Provide a way to compare AST nodes for equality recursively #60191
fb272e8
to
d16077b
Compare
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a few comments, but realized I wanted to try a slightly revised implementation to answer the questions I had. I'll share the revision in a moment.
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.
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>
hey @jeremyhylton, i am currently a bit busy with non-oss stuff so if you'd like feel free to take over the PR (and close this one). whatever is the most convenient (sorry for the CLA being blocked, @ambv just made me aware). |
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Misc/NEWS.d/next/Library/2020-03-28-21-00-54.bpo-15987.aBL8XS.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
* 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>