@@ -976,3 +976,115 @@ def func_with_aliases() -> ModelWithAliases:
976976 assert "field_second" not in structured_content_defaults
977977 assert structured_content_defaults ["first" ] is None
978978 assert structured_content_defaults ["second" ] is None
979+
980+
981+ def test_basemodel_reserved_names ():
982+ """Test that functions with parameters named after BaseModel methods work correctly"""
983+
984+ def func_with_reserved_names (
985+ model_dump : str ,
986+ model_validate : int ,
987+ dict : list [str ],
988+ json : dict [str , Any ],
989+ validate : bool ,
990+ copy : float ,
991+ normal_param : str ,
992+ ) -> str :
993+ return f"{ model_dump } , { model_validate } , { dict } , { json } , { validate } , { copy } , { normal_param } "
994+
995+ meta = func_metadata (func_with_reserved_names )
996+
997+ # Check that the schema has all the original parameter names (using aliases)
998+ schema = meta .arg_model .model_json_schema (by_alias = True )
999+ assert "model_dump" in schema ["properties" ]
1000+ assert "model_validate" in schema ["properties" ]
1001+ assert "dict" in schema ["properties" ]
1002+ assert "json" in schema ["properties" ]
1003+ assert "validate" in schema ["properties" ]
1004+ assert "copy" in schema ["properties" ]
1005+ assert "normal_param" in schema ["properties" ]
1006+
1007+
1008+ @pytest .mark .anyio
1009+ async def test_basemodel_reserved_names_validation ():
1010+ """Test that validation and calling works with reserved parameter names"""
1011+
1012+ def func_with_reserved_names (
1013+ model_dump : str ,
1014+ model_validate : int ,
1015+ dict : list [str ],
1016+ json : dict [str , Any ],
1017+ validate : bool ,
1018+ normal_param : str ,
1019+ ) -> str :
1020+ return f"{ model_dump } |{ model_validate } |{ len (dict )} |{ json } |{ validate } |{ normal_param } "
1021+
1022+ meta = func_metadata (func_with_reserved_names )
1023+
1024+ # Test validation with reserved names
1025+ result = await meta .call_fn_with_arg_validation (
1026+ func_with_reserved_names ,
1027+ fn_is_async = False ,
1028+ arguments_to_validate = {
1029+ "model_dump" : "test_dump" ,
1030+ "model_validate" : 42 ,
1031+ "dict" : ["a" , "b" , "c" ],
1032+ "json" : {"key" : "value" },
1033+ "validate" : True ,
1034+ "normal_param" : "normal" ,
1035+ },
1036+ arguments_to_pass_directly = None ,
1037+ )
1038+
1039+ assert result == "test_dump|42|3|{'key': 'value'}|True|normal"
1040+
1041+ # Test that the model can still call its own methods
1042+ model_instance = meta .arg_model .model_validate (
1043+ {
1044+ "model_dump" : "dump_value" ,
1045+ "model_validate" : 123 ,
1046+ "dict" : ["x" , "y" ],
1047+ "json" : {"foo" : "bar" },
1048+ "validate" : False ,
1049+ "normal_param" : "test" ,
1050+ }
1051+ )
1052+
1053+ # The model should still have its methods accessible
1054+ assert hasattr (model_instance , "model_dump" )
1055+ assert callable (model_instance .model_dump )
1056+
1057+ # model_dump_one_level should return the original parameter names
1058+ dumped = model_instance .model_dump_one_level ()
1059+ assert dumped ["model_dump" ] == "dump_value"
1060+ assert dumped ["model_validate" ] == 123
1061+ assert dumped ["dict" ] == ["x" , "y" ]
1062+ assert dumped ["json" ] == {"foo" : "bar" }
1063+ assert dumped ["validate" ] is False
1064+ assert dumped ["normal_param" ] == "test"
1065+
1066+
1067+ def test_basemodel_reserved_names_with_json_preparsing ():
1068+ """Test that pre_parse_json works correctly with reserved parameter names"""
1069+
1070+ def func_with_reserved_json (
1071+ json : dict [str , Any ],
1072+ model_dump : list [int ],
1073+ normal : str ,
1074+ ) -> str :
1075+ return "ok"
1076+
1077+ meta = func_metadata (func_with_reserved_json )
1078+
1079+ # Test pre-parsing with reserved names
1080+ result = meta .pre_parse_json (
1081+ {
1082+ "json" : '{"nested": "data"}' , # JSON string that should be parsed
1083+ "model_dump" : "[1, 2, 3]" , # JSON string that should be parsed
1084+ "normal" : "plain string" , # Should remain as string
1085+ }
1086+ )
1087+
1088+ assert result ["json" ] == {"nested" : "data" }
1089+ assert result ["model_dump" ] == [1 , 2 , 3 ]
1090+ assert result ["normal" ] == "plain string"
0 commit comments