@@ -1443,6 +1443,13 @@ def test_replace_reject_unknown_instance_fields(self):
14431443 self .assertIs (node .ctx , context )
14441444 self .assertRaises (AttributeError , getattr , node , 'unknown' )
14451445
1446+ def test_replace_non_str_kwarg (self ):
1447+ node = ast .Name (id = "x" )
1448+ errmsg = "got an unexpected keyword argument <object object"
1449+ with self .assertRaisesRegex (TypeError , errmsg ):
1450+ node .__replace__ (** {object (): "y" })
1451+
1452+
14461453class ASTHelpers_Test (unittest .TestCase ):
14471454 maxDiff = None
14481455
@@ -3304,6 +3311,27 @@ class _AllFieldTypes(ast.AST):
33043311 self .assertIs (obj .a , None )
33053312 self .assertEqual (obj .b , [])
33063313
3314+ def test_non_str_kwarg (self ):
3315+ warn_msg = "got an unexpected keyword argument <object object"
3316+ with (
3317+ self .assertRaises (TypeError ),
3318+ self .assertWarnsRegex (DeprecationWarning , warn_msg ),
3319+ ):
3320+ ast .Name (** {object (): 'y' })
3321+
3322+ class FakeStr :
3323+ def __init__ (self , value ):
3324+ self .value = value
3325+
3326+ def __hash__ (self ):
3327+ return hash (self .value )
3328+
3329+ def __eq__ (self , other ):
3330+ return isinstance (other , str ) and self .value == other
3331+
3332+ with self .assertRaisesRegex (TypeError , "got multiple values for argument" ):
3333+ ast .Name ("x" , ** {FakeStr ('id' ): 'y' })
3334+
33073335
33083336@support .cpython_only
33093337class ModuleStateTests (unittest .TestCase ):
0 commit comments