|
1 | 1 | use pyo3::intern;
|
2 | 2 | use pyo3::prelude::*;
|
3 |
| -use pyo3::types::{PyDict, PyString}; |
| 3 | +use pyo3::types::{PyDict, PyString, PyType}; |
4 | 4 |
|
5 | 5 | use crate::build_tools::py_schema_err;
|
6 | 6 | use crate::build_tools::{is_strict, schema_or_config, ExtraBehavior};
|
@@ -37,6 +37,7 @@ pub struct TypedDictValidator {
|
37 | 37 | loc_by_alias: bool,
|
38 | 38 | validate_by_alias: Option<bool>,
|
39 | 39 | validate_by_name: Option<bool>,
|
| 40 | + cls_name: Option<String>, |
40 | 41 | }
|
41 | 42 |
|
42 | 43 | impl BuildValidator for TypedDictValidator {
|
@@ -69,6 +70,14 @@ impl BuildValidator for TypedDictValidator {
|
69 | 70 | let fields_dict: Bound<'_, PyDict> = schema.get_as_req(intern!(py, "fields"))?;
|
70 | 71 | let mut fields: Vec<TypedDictField> = Vec::with_capacity(fields_dict.len());
|
71 | 72 |
|
| 73 | + let cls_name: Option<String> = match schema.get_as_req::<String>(intern!(py, "cls_name")) { |
| 74 | + Ok(name) => Some(name), |
| 75 | + Err(_) => match schema.get_as_req::<Bound<'_, PyType>>(intern!(py, "cls")) { |
| 76 | + Ok(class) => Some(class.getattr(intern!(py, "__name__"))?.extract()?), |
| 77 | + Err(_) => None, |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
72 | 81 | for (key, value) in fields_dict {
|
73 | 82 | let field_info = value.downcast::<PyDict>()?;
|
74 | 83 | let field_name_py = key.downcast_into::<PyString>()?;
|
@@ -128,6 +137,7 @@ impl BuildValidator for TypedDictValidator {
|
128 | 137 | loc_by_alias: config.get_as(intern!(py, "loc_by_alias"))?.unwrap_or(true),
|
129 | 138 | validate_by_alias: config.get_as(intern!(py, "validate_by_alias"))?,
|
130 | 139 | validate_by_name: config.get_as(intern!(py, "validate_by_name"))?,
|
| 140 | + cls_name, |
131 | 141 | }
|
132 | 142 | .into())
|
133 | 143 | }
|
@@ -367,6 +377,6 @@ impl Validator for TypedDictValidator {
|
367 | 377 | }
|
368 | 378 |
|
369 | 379 | fn get_name(&self) -> &str {
|
370 |
| - Self::EXPECTED_TYPE |
| 380 | + self.cls_name.as_deref().unwrap_or(Self::EXPECTED_TYPE) |
371 | 381 | }
|
372 | 382 | }
|
0 commit comments