@@ -458,14 +458,13 @@ def test_field_attr_writable(self):
458458 self .assertEqual (x ._fields , 666 )
459459
460460 def test_classattrs (self ):
461- with self .assertWarns (DeprecationWarning ):
461+ msg = "Constant.__init__ missing 1 required positional argument: 'value'"
462+ with self .assertRaisesRegex (TypeError , re .escape (msg )):
462463 x = ast .Constant ()
463- self .assertEqual (x ._fields , ('value' , 'kind' ))
464-
465- with self .assertRaises (AttributeError ):
466- x .value
467464
468465 x = ast .Constant (42 )
466+ self .assertEqual (x ._fields , ('value' , 'kind' ))
467+
469468 self .assertEqual (x .value , 42 )
470469
471470 with self .assertRaises (AttributeError ):
@@ -485,9 +484,10 @@ def test_classattrs(self):
485484 self .assertRaises (TypeError , ast .Constant , 1 , None , 2 )
486485 self .assertRaises (TypeError , ast .Constant , 1 , None , 2 , lineno = 0 )
487486
488- # Arbitrary keyword arguments are supported (but deprecated)
489- with self .assertWarns (DeprecationWarning ):
490- self .assertEqual (ast .Constant (1 , foo = 'bar' ).foo , 'bar' )
487+ # Arbitrary keyword arguments are not supported
488+ msg = "Constant.__init__ got an unexpected keyword argument 'foo'"
489+ with self .assertRaisesRegex (TypeError , re .escape (msg )):
490+ ast .Constant (1 , foo = 'bar' )
491491
492492 with self .assertRaisesRegex (TypeError , "Constant got multiple values for argument 'value'" ):
493493 ast .Constant (1 , value = 2 )
@@ -528,23 +528,24 @@ def test_module(self):
528528 self .assertEqual (x .body , body )
529529
530530 def test_nodeclasses (self ):
531- # Zero arguments constructor explicitly allowed (but deprecated)
532- with self .assertWarns (DeprecationWarning ):
531+ # Zero arguments constructor is not allowed
532+ msg = "missing 3 required positional arguments: 'left', 'op', and 'right'"
533+ with self .assertRaisesRegex (TypeError , re .escape (msg )):
533534 x = ast .BinOp ()
534- self .assertEqual (x ._fields , ('left' , 'op' , 'right' ))
535-
536- # Random attribute allowed too
537- x .foobarbaz = 5
538- self .assertEqual (x .foobarbaz , 5 )
539535
540536 n1 = ast .Constant (1 )
541537 n3 = ast .Constant (3 )
542538 addop = ast .Add ()
543539 x = ast .BinOp (n1 , addop , n3 )
540+ self .assertEqual (x ._fields , ('left' , 'op' , 'right' ))
544541 self .assertEqual (x .left , n1 )
545542 self .assertEqual (x .op , addop )
546543 self .assertEqual (x .right , n3 )
547544
545+ # Random attribute allowed too
546+ x .foobarbaz = 5
547+ self .assertEqual (x .foobarbaz , 5 )
548+
548549 x = ast .BinOp (1 , 2 , 3 )
549550 self .assertEqual (x .left , 1 )
550551 self .assertEqual (x .op , 2 )
@@ -568,10 +569,9 @@ def test_nodeclasses(self):
568569 self .assertEqual (x .right , 3 )
569570 self .assertEqual (x .lineno , 0 )
570571
571- # Random kwargs also allowed (but deprecated)
572- with self .assertWarns ( DeprecationWarning ):
572+ # Random kwargs are not allowed
573+ with self .assertRaisesRegex ( TypeError , "unexpected keyword argument 'foobarbaz'" ):
573574 x = ast .BinOp (1 , 2 , 3 , foobarbaz = 42 )
574- self .assertEqual (x .foobarbaz , 42 )
575575
576576 def test_no_fields (self ):
577577 # this used to fail because Sub._fields was None
@@ -3209,11 +3209,10 @@ def test_FunctionDef(self):
32093209 args = ast .arguments ()
32103210 self .assertEqual (args .args , [])
32113211 self .assertEqual (args .posonlyargs , [])
3212- with self .assertWarnsRegex ( DeprecationWarning ,
3212+ with self .assertRaisesRegex ( TypeError ,
32133213 r"FunctionDef\.__init__ missing 1 required positional argument: 'name'" ):
32143214 node = ast .FunctionDef (args = args )
3215- self .assertNotHasAttr (node , "name" )
3216- self .assertEqual (node .decorator_list , [])
3215+
32173216 node = ast .FunctionDef (name = 'foo' , args = args )
32183217 self .assertEqual (node .name , 'foo' )
32193218 self .assertEqual (node .decorator_list , [])
@@ -3231,7 +3230,7 @@ def test_expr_context(self):
32313230 self .assertEqual (name3 .id , "x" )
32323231 self .assertIsInstance (name3 .ctx , ast .Del )
32333232
3234- with self .assertWarnsRegex ( DeprecationWarning ,
3233+ with self .assertRaisesRegex ( TypeError ,
32353234 r"Name\.__init__ missing 1 required positional argument: 'id'" ):
32363235 name3 = ast .Name ()
32373236
@@ -3272,20 +3271,19 @@ class MyAttrs(ast.AST):
32723271 self .assertEqual (obj .a , 1 )
32733272 self .assertEqual (obj .b , 2 )
32743273
3275- with self .assertWarnsRegex ( DeprecationWarning ,
3276- r"MyAttrs.__init__ got an unexpected keyword argument 'c'. " ):
3274+ with self .assertRaisesRegex ( TypeError ,
3275+ r"MyAttrs.__init__ got an unexpected keyword argument 'c'" ):
32773276 obj = MyAttrs (c = 3 )
32783277
32793278 def test_fields_and_types_no_default (self ):
32803279 class FieldsAndTypesNoDefault (ast .AST ):
32813280 _fields = ('a' ,)
32823281 _field_types = {'a' : int }
32833282
3284- with self .assertWarnsRegex ( DeprecationWarning ,
3285- r"FieldsAndTypesNoDefault\.__init__ missing 1 required positional argument: 'a'\. " ):
3283+ with self .assertRaisesRegex ( TypeError ,
3284+ r"FieldsAndTypesNoDefault\.__init__ missing 1 required positional argument: 'a'" ):
32863285 obj = FieldsAndTypesNoDefault ()
3287- with self .assertRaises (AttributeError ):
3288- obj .a
3286+
32893287 obj = FieldsAndTypesNoDefault (a = 1 )
32903288 self .assertEqual (obj .a , 1 )
32913289
@@ -3296,13 +3294,8 @@ class MoreFieldsThanTypes(ast.AST):
32963294 a : int | None = None
32973295 b : int | None = None
32983296
3299- with self .assertWarnsRegex (
3300- DeprecationWarning ,
3301- r"Field 'b' is missing from MoreFieldsThanTypes\._field_types"
3302- ):
3297+ with self .assertRaisesRegex (TypeError , "Field 'b' is missing" ):
33033298 obj = MoreFieldsThanTypes ()
3304- self .assertIs (obj .a , None )
3305- self .assertIs (obj .b , None )
33063299
33073300 obj = MoreFieldsThanTypes (a = 1 , b = 2 )
33083301 self .assertEqual (obj .a , 1 )
0 commit comments