@@ -1419,6 +1419,13 @@ def test_replace_reject_unknown_instance_fields(self):
14191419 self .assertIs (node .ctx , context )
14201420 self .assertRaises (AttributeError , getattr , node , 'unknown' )
14211421
1422+ def test_replace_non_str_kwarg (self ):
1423+ node = ast .Name (id = "x" )
1424+ errmsg = "got an unexpected keyword argument <object object"
1425+ with self .assertRaisesRegex (TypeError , errmsg ):
1426+ node .__replace__ (** {object (): "y" })
1427+
1428+
14221429class ASTHelpers_Test (unittest .TestCase ):
14231430 maxDiff = None
14241431
@@ -3281,6 +3288,27 @@ class _AllFieldTypes(ast.AST):
32813288 self .assertIs (obj .a , None )
32823289 self .assertEqual (obj .b , [])
32833290
3291+ def test_non_str_kwarg (self ):
3292+ warn_msg = "got an unexpected keyword argument <object object"
3293+ with (
3294+ self .assertRaises (TypeError ),
3295+ self .assertWarnsRegex (DeprecationWarning , warn_msg ),
3296+ ):
3297+ ast .Name (** {object (): 'y' })
3298+
3299+ class FakeStr :
3300+ def __init__ (self , value ):
3301+ self .value = value
3302+
3303+ def __hash__ (self ):
3304+ return hash (self .value )
3305+
3306+ def __eq__ (self , other ):
3307+ return isinstance (other , str ) and self .value == other
3308+
3309+ with self .assertRaisesRegex (TypeError , "got multiple values for argument" ):
3310+ ast .Name ("x" , ** {FakeStr ('id' ): 'y' })
3311+
32843312
32853313@support .cpython_only
32863314class ModuleStateTests (unittest .TestCase ):
0 commit comments