]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-60191: Implement ast.compare (#19211)
authorBatuhan Taskaya <isidentical@gmail.com>
Wed, 22 May 2024 01:39:26 +0000 (04:39 +0300)
committerGitHub <noreply@github.com>
Wed, 22 May 2024 01:39:26 +0000 (01:39 +0000)
commitd065edfb66470bbf06367b3570661d0346aa6707
treeb05bef2dcc06a4302a6e63627b36c5c90b2b6f0e
parent0e3c8cda1f04c983994e76aea93600dbb4714832
gh-60191: Implement ast.compare (#19211)

* 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>
Doc/library/ast.rst
Doc/whatsnew/3.14.rst
Lib/ast.py
Lib/test/test_ast.py
Misc/NEWS.d/next/Library/2020-03-28-21-00-54.bpo-15987.aBL8XS.rst [new file with mode: 0644]