Skip to content

Commit 9446588

Browse files
authored
Fix condition before using prebuilt validator/serializer (#1625)
Typed dictionaries don't have cached validators/serializers, and the condition wasn't actually ignoring parametrized dataclasses.
1 parent 4e824df commit 9446588

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/common/prebuilt.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ pub fn get_prebuilt<T>(
1212
) -> PyResult<Option<T>> {
1313
let py = schema.py();
1414

15-
// we can only use prebuilt validators / serializers from models, typed dicts, and dataclasses
16-
// however, we don't want to use a prebuilt structure from dataclasses if we have a generic_origin
17-
// because the validator / serializer is cached on the unparametrized dataclass
18-
if !matches!(type_, "model" | "typed-dict")
19-
|| matches!(type_, "dataclass") && schema.contains(intern!(py, "generic_origin"))?
15+
// we can only use prebuilt validators/serializers from models and Pydantic dataclasses.
16+
// However, we don't want to use a prebuilt structure from dataclasses if we have a `generic_origin`
17+
// as this means the dataclass was parametrized (so a generic alias instance), and `cls` in the
18+
// core schema is still the (unparametrized) class, meaning we would fetch the wrong validator/serializer.
19+
if !matches!(type_, "model" | "dataclass")
20+
|| (type_ == "dataclass" && schema.contains(intern!(py, "generic_origin"))?)
2021
{
2122
return Ok(None);
2223
}

0 commit comments

Comments
 (0)