@@ -625,7 +625,7 @@ def test_invalid_identifier(self):
625625 ast .fix_missing_locations (m )
626626 with self .assertRaises (TypeError ) as cm :
627627 compile (m , "<test>" , "exec" )
628- self .assertIn ("identifier must be of type str " , str (cm .exception ))
628+ self .assertIn ("expecting a string object " , str (cm .exception ))
629629
630630 def test_invalid_constant (self ):
631631 for invalid_constant in int , (1 , 2 , int ), frozenset ((1 , 2 , int )):
@@ -1081,6 +1081,30 @@ def test_none_checks(self) -> None:
10811081 for node , attr , source in tests :
10821082 self .assert_none_check (node , attr , source )
10831083
1084+ def test_required_field_messages (self ):
1085+ binop = ast .BinOp (
1086+ left = ast .Constant (value = 2 ),
1087+ right = ast .Constant (value = 2 ),
1088+ op = ast .Add (),
1089+ )
1090+ expr_without_position = ast .Expression (body = binop )
1091+ expr_with_wrong_body = ast .Expression (body = [binop ])
1092+
1093+ with self .assertRaisesRegex (TypeError , "required field" ) as cm :
1094+ compile (expr_without_position , "<test>" , "eval" )
1095+ with self .assertRaisesRegex (
1096+ TypeError ,
1097+ "field 'body' was expecting node of type 'expr', got 'list'" ,
1098+ ):
1099+ compile (expr_with_wrong_body , "<test>" , "eval" )
1100+
1101+ constant = ast .parse ("u'test'" , mode = "eval" )
1102+ constant .body .kind = 0xFF
1103+ with self .assertRaisesRegex (
1104+ TypeError , "field 'kind' was expecting a string or bytes object"
1105+ ):
1106+ compile (constant , "<test>" , "eval" )
1107+
10841108 def test_repr (self ) -> None :
10851109 snapshots = AST_REPR_DATA_FILE .read_text ().split ("\n " )
10861110 for test , snapshot in zip (ast_repr_get_test_cases (), snapshots , strict = True ):
0 commit comments