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

Treat Pydantic dataclasses as objects that are not dataclasses at all #729

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ typedef struct {
PyObject *str__field_defaults;
PyObject *str___post_init__;
PyObject *str___dataclass_fields__;
PyObject *str___pydantic_fields__;
PyObject *str___attrs_attrs__;
PyObject *str___supertype__;
#if PY312_PLUS
Expand Down Expand Up @@ -2162,7 +2163,8 @@ PyDoc_STRVAR(unset__doc__,
"in an object needs to be treated differently than one containing an explicit\n"
"``None`` value. In this case, you may use ``UNSET`` as the default value,\n"
"rather than ``None`` when defining object schemas. This feature is supported\n"
"for any `msgspec.Struct`, `dataclasses` or `attrs` types.\n"
"for any `msgspec.Struct`, `dataclasses` or `attrs` types (excluding Pydantic\n"
"dataclasses).\n"
"\n"
"Examples\n"
"--------\n"
Expand Down Expand Up @@ -4767,7 +4769,9 @@ is_dataclass_or_attrs_class(TypeNodeCollectState *state, PyObject *t) {
PyType_Check(t) && (
PyObject_HasAttr(t, state->mod->str___dataclass_fields__) ||
PyObject_HasAttr(t, state->mod->str___attrs_attrs__)
)
) && ! (
PyObject_HasAttr(t, state->mod->str___pydantic_fields__)
)
);
}

Expand Down Expand Up @@ -22231,6 +22235,7 @@ PyInit__core(void)
CACHED_STRING(str__field_defaults, "_field_defaults");
CACHED_STRING(str___post_init__, "__post_init__");
CACHED_STRING(str___dataclass_fields__, "__dataclass_fields__");
CACHED_STRING(str___pydantic_fields__, "__pydantic_fields__");
CACHED_STRING(str___attrs_attrs__, "__attrs_attrs__");
CACHED_STRING(str___supertype__, "__supertype__");
#if PY312_PLUS
Expand Down
3 changes: 3 additions & 0 deletions msgspec/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ def get_dataclass_info(obj):
if hasattr(cls, "__dataclass_fields__"):
from dataclasses import _FIELD, _FIELD_INITVAR, MISSING

if hasattr(cls, "__pydantic_fields__"):
raise TypeError("Pydantic dataclasses are not supported")

for field in cls.__dataclass_fields__.values():
if field._field_type is not _FIELD:
if field._field_type is _FIELD_INITVAR:
Expand Down
2 changes: 1 addition & 1 deletion msgspec/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def _is_enum(t):


def _is_dataclass(t):
return hasattr(t, "__dataclass_fields__")
return hasattr(t, "__dataclass_fields__") and not hasattr(t, "__pydantic_fields__")


def _is_attrs(t):
Expand Down