@@ -948,6 +948,15 @@ def test_compare_fieldless(self):
948948 self .assertTrue (ast .compare (ast .Add (), ast .Add ()))
949949 self .assertFalse (ast .compare (ast .Sub (), ast .Add ()))
950950
951+ # test that missing runtime fields is handled in ast.compare()
952+ a1 , a2 = ast .Name ('a' ), ast .Name ('a' )
953+ self .assertTrue (ast .compare (a1 , a2 ))
954+ self .assertTrue (ast .compare (a1 , a2 ))
955+ del a1 .id
956+ self .assertFalse (ast .compare (a1 , a2 ))
957+ del a2 .id
958+ self .assertTrue (ast .compare (a1 , a2 ))
959+
951960 def test_compare_modes (self ):
952961 for mode , sources in (
953962 ("exec" , exec_tests ),
@@ -970,6 +979,16 @@ def parse(a, b):
970979 self .assertTrue (ast .compare (a , b , compare_attributes = False ))
971980 self .assertFalse (ast .compare (a , b , compare_attributes = True ))
972981
982+ def test_compare_attributes_option_missing_attribute (self ):
983+ # test that missing runtime attributes is handled in ast.compare()
984+ a1 , a2 = ast .Name ('a' , lineno = 1 ), ast .Name ('a' , lineno = 1 )
985+ self .assertTrue (ast .compare (a1 , a2 ))
986+ self .assertTrue (ast .compare (a1 , a2 , compare_attributes = True ))
987+ del a1 .lineno
988+ self .assertFalse (ast .compare (a1 , a2 , compare_attributes = True ))
989+ del a2 .lineno
990+ self .assertTrue (ast .compare (a1 , a2 , compare_attributes = True ))
991+
973992 def test_positional_only_feature_version (self ):
974993 ast .parse ('def foo(x, /): ...' , feature_version = (3 , 8 ))
975994 ast .parse ('def bar(x=1, /): ...' , feature_version = (3 , 8 ))
0 commit comments