diff --git a/pydantic/fields.py b/pydantic/fields.py index a6fe1305c9..34a0651c48 100644 --- a/pydantic/fields.py +++ b/pydantic/fields.py @@ -790,7 +790,9 @@ def Field( # noqa: C901 if extra: warn( - 'Extra keyword arguments on `Field` is deprecated and will be removed. use `json_schema_extra` instead', + 'Using extra keyword arguments on `Field` is deprecated and will be removed.' + ' Use `json_schema_extra` instead.' + f' (Extra keys: {", ".join(k.__repr__() for k in extra.keys())})', DeprecationWarning, ) if not json_schema_extra or json_schema_extra is _Unset: diff --git a/tests/test_deprecated.py b/tests/test_deprecated.py index 5484676ab2..364343e094 100644 --- a/tests/test_deprecated.py +++ b/tests/test_deprecated.py @@ -524,19 +524,22 @@ def __get_validators__(cls) -> Iterable[Any]: def test_field_extra_arguments(): - m = 'Extra keyword arguments on `Field` is deprecated and will be removed. use `json_schema_extra` instead' + m = re.escape( + 'Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. ' + "(Extra keys: 'test', 'foo')" + ) with pytest.warns(PydanticDeprecatedSince20, match=m): class Model(BaseModel): - x: str = Field('test', test='test') + x: str = Field('test', test='test', foo='bar') assert Model.model_json_schema(by_alias=True)['properties'] == { - 'x': {'default': 'test', 'test': 'test', 'title': 'X', 'type': 'string'} + 'x': {'default': 'test', 'foo': 'bar', 'test': 'test', 'title': 'X', 'type': 'string'} } def test_field_extra_does_not_rewrite_json_schema_extra(): - m = 'Extra keyword arguments on `Field` is deprecated and will be removed. use `json_schema_extra` instead' + m = 'Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead' with pytest.warns(PydanticDeprecatedSince20, match=m): class Model(BaseModel):