Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Field extra keys name in warning #7136

Merged
merged 5 commits into from Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion pydantic/fields.py
Expand Up @@ -785,7 +785,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:
Expand Down
11 changes: 7 additions & 4 deletions tests/test_deprecated.py
Expand Up @@ -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):
Expand Down