Skip to content

Commit

Permalink
Include Field extra keys name in warning (#7136)
Browse files Browse the repository at this point in the history
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
  • Loading branch information
hramezani and dmontagu committed Aug 16, 2023
1 parent 884015d commit 61737ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pydantic/fields.py
Expand Up @@ -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:
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

0 comments on commit 61737ea

Please sign in to comment.