Skip to content

Commit a774065

Browse files
committed
first pass at fix
1 parent 903a1a9 commit a774065

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

python/pydantic_core/core_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3964,6 +3964,7 @@ def definition_reference_schema(
39643964
'no_such_attribute',
39653965
'json_invalid',
39663966
'json_type',
3967+
'cannot_validate_from_json',
39673968
'recursion_loop',
39683969
'missing',
39693970
'frozen_field',

src/errors/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ error_types! {
167167
error: {ctx_type: String, ctx_fn: field_from_context},
168168
},
169169
JsonType {},
170+
CannotValidateFromJson { method_name: {ctx_type: String, ctx_fn: field_from_context} },
170171
// ---------------------
171172
// recursion error
172173
RecursionLoop {},
@@ -477,6 +478,7 @@ impl ErrorType {
477478
Self::NoSuchAttribute {..} => "Object has no attribute '{attribute}'",
478479
Self::JsonInvalid {..} => "Invalid JSON: {error}",
479480
Self::JsonType {..} => "JSON input should be string, bytes or bytearray",
481+
Self::CannotValidateFromJson {..} => "Cannot check `{method_name}` when validating from json, use a JsonOrPython validator instead",
480482
Self::RecursionLoop {..} => "Recursion error - cyclic reference detected",
481483
Self::Missing {..} => "Field required",
482484
Self::FrozenField {..} => "Field is frozen",
@@ -626,6 +628,7 @@ impl ErrorType {
626628
match self {
627629
Self::NoSuchAttribute { attribute, .. } => render!(tmpl, attribute),
628630
Self::JsonInvalid { error, .. } => render!(tmpl, error),
631+
Self::CannotValidateFromJson { method_name, .. } => render!(tmpl, method_name),
629632
Self::GetAttributeError { error, .. } => render!(tmpl, error),
630633
Self::ModelType { class_name, .. } => render!(tmpl, class_name),
631634
Self::DataclassType { class_name, .. } => render!(tmpl, class_name),

src/validators/is_instance.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use pyo3::exceptions::PyNotImplementedError;
21
use pyo3::intern;
32
use pyo3::prelude::*;
43
use pyo3::types::{PyDict, PyType};
@@ -56,10 +55,14 @@ impl Validator for IsInstanceValidator {
5655
_state: &mut ValidationState<'_, 'py>,
5756
) -> ValResult<PyObject> {
5857
let Some(obj) = input.as_python() else {
59-
return Err(ValError::InternalErr(PyNotImplementedError::new_err(
60-
"Cannot check isinstance when validating from json, \
61-
use a JsonOrPython validator instead.",
62-
)));
58+
let method_name = "isinstance".to_string();
59+
return Err(ValError::new(
60+
ErrorType::CannotValidateFromJson {
61+
context: None,
62+
method_name,
63+
},
64+
input,
65+
));
6366
};
6467
match obj.is_instance(self.class.bind(py))? {
6568
true => Ok(obj.clone().unbind()),

src/validators/is_subclass.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use pyo3::exceptions::PyNotImplementedError;
21
use pyo3::intern;
32
use pyo3::prelude::*;
43
use pyo3::types::{PyDict, PyType};
@@ -51,10 +50,14 @@ impl Validator for IsSubclassValidator {
5150
_state: &mut ValidationState<'_, 'py>,
5251
) -> ValResult<PyObject> {
5352
let Some(obj) = input.as_python() else {
54-
return Err(ValError::InternalErr(PyNotImplementedError::new_err(
55-
"Cannot check issubclass when validating from json, \
56-
use a JsonOrPython validator instead.",
57-
)));
53+
let method_name = "issubclass".to_string();
54+
return Err(ValError::new(
55+
ErrorType::CannotValidateFromJson {
56+
context: None,
57+
method_name,
58+
},
59+
input,
60+
));
5861
};
5962
match obj.downcast::<PyType>() {
6063
Ok(py_type) if py_type.is_subclass(self.class.bind(py))? => Ok(obj.clone().unbind()),

0 commit comments

Comments
 (0)