Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,7 @@ class TypedDictSchema(TypedDict, total=False):
type: Required[Literal['typed-dict']]
fields: Required[dict[str, TypedDictField]]
cls: type[Any]
cls_name: str
computed_fields: list[ComputedField]
strict: bool
extras_schema: CoreSchema
Expand All @@ -2898,6 +2899,7 @@ def typed_dict_schema(
fields: dict[str, TypedDictField],
*,
cls: type[Any] | None = None,
cls_name: str | None = None,
computed_fields: list[ComputedField] | None = None,
strict: bool | None = None,
extras_schema: CoreSchema | None = None,
Expand Down Expand Up @@ -2929,6 +2931,8 @@ class MyTypedDict(TypedDict):
Args:
fields: The fields to use for the typed dict
cls: The class to use for the typed dict
cls_name: The name to use in error locations. Falls back to `cls.__name__`, or the validator name if no class
is provided.
computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
strict: Whether the typed dict is strict
extras_schema: The extra validator to use for the typed dict
Expand All @@ -2942,6 +2946,7 @@ class MyTypedDict(TypedDict):
type='typed-dict',
fields=fields,
cls=cls,
cls_name=cls_name,
computed_fields=computed_fields,
strict=strict,
extras_schema=extras_schema,
Expand Down
14 changes: 12 additions & 2 deletions src/validators/typed_dict.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyString};
use pyo3::types::{PyDict, PyString, PyType};

use crate::build_tools::py_schema_err;
use crate::build_tools::{is_strict, schema_or_config, ExtraBehavior};
Expand Down Expand Up @@ -37,6 +37,7 @@ pub struct TypedDictValidator {
loc_by_alias: bool,
validate_by_alias: Option<bool>,
validate_by_name: Option<bool>,
cls_name: Option<String>,
}

impl BuildValidator for TypedDictValidator {
Expand Down Expand Up @@ -69,6 +70,14 @@ impl BuildValidator for TypedDictValidator {
let fields_dict: Bound<'_, PyDict> = schema.get_as_req(intern!(py, "fields"))?;
let mut fields: Vec<TypedDictField> = Vec::with_capacity(fields_dict.len());

let cls_name: Option<String> = match schema.get_as_req::<String>(intern!(py, "cls_name")) {
Ok(name) => Some(name),
Err(_) => match schema.get_as_req::<Bound<'_, PyType>>(intern!(py, "cls")) {
Ok(class) => Some(class.getattr(intern!(py, "__name__"))?.extract()?),
Err(_) => None,
},
};

for (key, value) in fields_dict {
let field_info = value.downcast::<PyDict>()?;
let field_name_py = key.downcast_into::<PyString>()?;
Expand Down Expand Up @@ -128,6 +137,7 @@ impl BuildValidator for TypedDictValidator {
loc_by_alias: config.get_as(intern!(py, "loc_by_alias"))?.unwrap_or(true),
validate_by_alias: config.get_as(intern!(py, "validate_by_alias"))?,
validate_by_name: config.get_as(intern!(py, "validate_by_name"))?,
cls_name,
}
.into())
}
Expand Down Expand Up @@ -367,6 +377,6 @@ impl Validator for TypedDictValidator {
}

fn get_name(&self) -> &str {
Self::EXPECTED_TYPE
self.cls_name.as_deref().unwrap_or(Self::EXPECTED_TYPE)
}
}
Loading