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

Raise error when Pydantic dataclasses are used on models #9388

Merged
merged 3 commits into from
May 15, 2024
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
18 changes: 18 additions & 0 deletions docs/errors/usage_errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,4 +1117,22 @@ except PydanticUserError as exc_info:
assert exc_info.code == 'model-config-invalid-field-name'
```

## `dataclass` is used on a `BaseModel` subclass {#dataclass-on-model}

This error is raised when the Pydantic `dataclass` decorator is used on a class which is already
a Pydantic model.

```py
from pydantic import BaseModel, PydanticUserError
from pydantic.dataclasses import dataclass

try:

@dataclass
class Model(BaseModel):
bar: str

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

{% endraw %}
9 changes: 9 additions & 0 deletions pydantic/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ._internal import _dataclasses as _pydantic_dataclasses
from ._migration import getattr_migration
from .config import ConfigDict
from .errors import PydanticUserError
from .fields import Field, FieldInfo, PrivateAttr

if TYPE_CHECKING:
Expand Down Expand Up @@ -190,6 +191,14 @@ def create_dataclass(cls: type[Any]) -> type[PydanticDataclass]:
Returns:
A Pydantic dataclass.
"""
from ._internal._utils import is_model_class

if is_model_class(cls):
Viicos marked this conversation as resolved.
Show resolved Hide resolved
raise PydanticUserError(
f'Cannot create a Pydantic dataclass from {cls.__name__} as it is already a Pydantic model',
code='dataclass-on-model',
)

original_cls = cls

config_dict = config
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',
'dataclass-on-model',
]


Expand Down
10 changes: 10 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
from pydantic.json_schema import model_json_schema


def test_cannot_create_dataclass_from_basemodel_subclass():
msg = 'Cannot create a Pydantic dataclass from SubModel as it is already a Pydantic model'

with pytest.raises(PydanticUserError, match=msg):

@pydantic.dataclasses.dataclass
class SubModel(BaseModel):
pass


def test_simple():
@pydantic.dataclasses.dataclass
class MyDataclass:
Expand Down