From 944658816aa8c08b6e23d41b59fd2df05cb9fdb9 Mon Sep 17 00:00:00 2001 From: Victorien <65306057+Viicos@users.noreply.github.com> Date: Thu, 6 Feb 2025 16:45:44 +0100 Subject: [PATCH] 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. --- src/common/prebuilt.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/common/prebuilt.rs b/src/common/prebuilt.rs index 961123691..f4307064f 100644 --- a/src/common/prebuilt.rs +++ b/src/common/prebuilt.rs @@ -12,11 +12,12 @@ pub fn get_prebuilt( ) -> PyResult> { let py = schema.py(); - // we can only use prebuilt validators / serializers from models, typed dicts, and dataclasses - // however, we don't want to use a prebuilt structure from dataclasses if we have a generic_origin - // because the validator / serializer is cached on the unparametrized dataclass - if !matches!(type_, "model" | "typed-dict") - || matches!(type_, "dataclass") && schema.contains(intern!(py, "generic_origin"))? + // we can only use prebuilt validators/serializers from models and Pydantic dataclasses. + // However, we don't want to use a prebuilt structure from dataclasses if we have a `generic_origin` + // as this means the dataclass was parametrized (so a generic alias instance), and `cls` in the + // core schema is still the (unparametrized) class, meaning we would fetch the wrong validator/serializer. + if !matches!(type_, "model" | "dataclass") + || (type_ == "dataclass" && schema.contains(intern!(py, "generic_origin"))?) { return Ok(None); }