Skip to content

Commit

Permalink
Add error code
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed May 13, 2024
1 parent 47e7993 commit 92a6650
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/errors/usage_errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,4 +1117,20 @@ except PydanticUserError as exc_info:
assert exc_info.code == 'model-config-invalid-field-name'
```

## [`with_config`][pydantic.config.with_config] is used on a `BaseModel` subclass {#with-config-on-model}

This error is raised when the [`with_config`][pydantic.config.with_config] decorator is used on a class which is already a Pydantic model (use the `model_config` attribute instead).

```py
from pydantic import BaseModel, PydanticUserError, with_config

try:

@with_config({"allow_inf_nan": True})
class Model(BaseModel):
bar: str

except PydanticUserError as exc_info:
assert exc_info.code == 'with-config-on-model'

{% endraw %}
2 changes: 1 addition & 1 deletion pydantic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def inner(class_: _TypeT, /) -> _TypeT:
if is_model_class(class_):
raise PydanticUserError(
f'Cannot use `with_config` on {class_.__name__} as it is a Pydantic model',
code=None,
code='with-config-on-model',
)
class_.__pydantic_config__ = config
return class_
Expand Down
1 change: 1 addition & 0 deletions pydantic/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
'dataclass-init-false-extra-allow',
'clashing-init-and-init-var',
'model-config-invalid-field-name',
'with-config-on-model',
]


Expand Down

0 comments on commit 92a6650

Please sign in to comment.