Skip to content

Commit

Permalink
use isinstance error
Browse files Browse the repository at this point in the history
  • Loading branch information
changhc committed Aug 8, 2024
1 parent 6c8c222 commit 8919589
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
1 change: 0 additions & 1 deletion python/pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4001,7 +4001,6 @@ def definition_reference_schema(
'decimal_max_places',
'decimal_whole_digits',
'complex_type',
'complex_type_py_strict',
'complex_str_parsing',
]

Expand Down
2 changes: 0 additions & 2 deletions src/errors/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ error_types! {
},
// Complex errors
ComplexType {},
ComplexTypePyStrict {},
ComplexStrParsing {},
}

Expand Down Expand Up @@ -574,7 +573,6 @@ impl ErrorType {
Self::DecimalMaxPlaces {..} => "Decimal input should have no more than {decimal_places} decimal place{expected_plural}",
Self::DecimalWholeDigits {..} => "Decimal input should have no more than {whole_digits} digit{expected_plural} before the decimal point",
Self::ComplexType {..} => "Input should be a valid python complex object, a number, or a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex",
Self::ComplexTypePyStrict {..} => "Input should be a valid Python complex object",
Self::ComplexStrParsing {..} => "Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex",
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use pyo3::types::{
};

use pyo3::PyTypeCheck;
use pyo3::PyTypeInfo;
use speedate::MicrosecondsPrecisionOverflowBehavior;

use crate::errors::{ErrorType, ErrorTypeDefaults, InputValue, LocItem, ValError, ValResult};
Expand Down Expand Up @@ -601,16 +602,21 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
Err(ValError::new(ErrorTypeDefaults::TimeDeltaType, self))
}

fn validate_complex<'a>(
&'a self,
strict: bool,
_py: Python<'py>,
) -> ValResult<ValidationMatch<EitherComplex<'py>>> {
fn validate_complex<'a>(&'a self, strict: bool, py: Python<'py>) -> ValResult<ValidationMatch<EitherComplex<'py>>> {
if let Ok(complex) = self.downcast::<PyComplex>() {
return Ok(ValidationMatch::strict(EitherComplex::Py(complex.to_owned())));
}
if strict {
return Err(ValError::new(ErrorTypeDefaults::ComplexTypePyStrict, self));
return Err(ValError::new(
ErrorType::IsInstanceOf {
class: PyComplex::type_object_bound(py)
.qualname()
.and_then(|name| name.extract())
.unwrap_or_else(|_| "complex".to_owned()),
context: None,
},
self,
));
}

if let Ok(s) = self.downcast::<PyString>() {
Expand Down
5 changes: 0 additions & 5 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,6 @@ def f(input_value, info):
'Input should be a valid python complex object, a number, or a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex',
None,
),
(
'complex_type_py_strict',
'Input should be a valid Python complex object',
None,
),
(
'complex_str_parsing',
'Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex',
Expand Down
2 changes: 1 addition & 1 deletion tests/validators/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

EXPECTED_PARSE_ERROR_MESSAGE = 'Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex'
EXPECTED_TYPE_ERROR_MESSAGE = 'Input should be a valid python complex object, a number, or a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex'
EXPECTED_TYPE_ERROR_PY_STRICT_MESSAGE = 'Input should be a valid Python complex object'
EXPECTED_TYPE_ERROR_PY_STRICT_MESSAGE = 'Input should be an instance of complex'


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/validators/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_dict_complex_key():
{'type': 'dict', 'keys_schema': {'type': 'complex', 'strict': True}, 'values_schema': {'type': 'str'}}
)
assert v.validate_python({complex(1, 2): '1'}) == {complex(1, 2): '1'}
with pytest.raises(ValidationError, match='Input should be a valid Python complex object'):
with pytest.raises(ValidationError, match='Input should be an instance of complex'):
assert v.validate_python({'1+2j': b'1'}) == {complex(1, 2): '1'}

v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'complex'}, 'values_schema': {'type': 'str'}})
Expand Down

0 comments on commit 8919589

Please sign in to comment.