Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Closed
Description
Bug report
Bug description:
ast.compare does not handle the case where attributes or fields are missing at runtime:
>>>importast>>>a=ast.parse('a').body[0].value>>>dela.id>>>ast.compare(a,a)Traceback (mostrecentcalllast):File"<python-input-5>",line1,in<module>ast.compare(a,a)~~~~~~~~~~~^^^^^File"/lib/python/cpython/Lib/ast.py",line473,incompareifnot_compare_fields(a,b):~~~~~~~~~~~~~~~^^^^^^File"/lib/python/cpython/Lib/ast.py",line452,in_compare_fieldsa_field=getattr(a,field)AttributeError:'Name'objecthasnoattribute'id'>>>a=ast.parse('a').body[0].value>>>dela.lineno>>>ast.compare(a,a,compare_attributes=True)Traceback (mostrecentcalllast):File"<python-input-8>",line2,in<module>ast.compare(a,a,compare_attributes=True)~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/lib/python/cpython/Lib/ast.py",line475,incompareifcompare_attributesandnot_compare_attributes(a,b):~~~~~~~~~~~~~~~~~~~^^^^^^File"/lib/python/cpython/Lib/ast.py",line464,in_compare_attributesa_attr=getattr(a,attr)AttributeError:'Name'objecthasnoattribute'lineno'
I suggest makingast.compare ignore a field/attribute if it's missing onboth operands (theydo compare equal in the sense that they don't have that specific field; not that even without that assumption, we still have an issue withast.compare(ast.Name('a'), ast.Name('a'), compare_attributes=True)).
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux