Skip to content

Commit 8919589

Browse files
committed
use isinstance error
1 parent 6c8c222 commit 8919589

File tree

6 files changed

+14
-16
lines changed

6 files changed

+14
-16
lines changed

python/pydantic_core/core_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4001,7 +4001,6 @@ def definition_reference_schema(
40014001
'decimal_max_places',
40024002
'decimal_whole_digits',
40034003
'complex_type',
4004-
'complex_type_py_strict',
40054004
'complex_str_parsing',
40064005
]
40074006

src/errors/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ error_types! {
428428
},
429429
// Complex errors
430430
ComplexType {},
431-
ComplexTypePyStrict {},
432431
ComplexStrParsing {},
433432
}
434433

@@ -574,7 +573,6 @@ impl ErrorType {
574573
Self::DecimalMaxPlaces {..} => "Decimal input should have no more than {decimal_places} decimal place{expected_plural}",
575574
Self::DecimalWholeDigits {..} => "Decimal input should have no more than {whole_digits} digit{expected_plural} before the decimal point",
576575
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",
577-
Self::ComplexTypePyStrict {..} => "Input should be a valid Python complex object",
578576
Self::ComplexStrParsing {..} => "Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex",
579577
}
580578
}

src/input/input_python.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use pyo3::types::{
1010
};
1111

1212
use pyo3::PyTypeCheck;
13+
use pyo3::PyTypeInfo;
1314
use speedate::MicrosecondsPrecisionOverflowBehavior;
1415

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

604-
fn validate_complex<'a>(
605-
&'a self,
606-
strict: bool,
607-
_py: Python<'py>,
608-
) -> ValResult<ValidationMatch<EitherComplex<'py>>> {
605+
fn validate_complex<'a>(&'a self, strict: bool, py: Python<'py>) -> ValResult<ValidationMatch<EitherComplex<'py>>> {
609606
if let Ok(complex) = self.downcast::<PyComplex>() {
610607
return Ok(ValidationMatch::strict(EitherComplex::Py(complex.to_owned())));
611608
}
612609
if strict {
613-
return Err(ValError::new(ErrorTypeDefaults::ComplexTypePyStrict, self));
610+
return Err(ValError::new(
611+
ErrorType::IsInstanceOf {
612+
class: PyComplex::type_object_bound(py)
613+
.qualname()
614+
.and_then(|name| name.extract())
615+
.unwrap_or_else(|_| "complex".to_owned()),
616+
context: None,
617+
},
618+
self,
619+
));
614620
}
615621

616622
if let Ok(s) = self.downcast::<PyString>() {

tests/test_errors.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ def f(input_value, info):
400400
'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',
401401
None,
402402
),
403-
(
404-
'complex_type_py_strict',
405-
'Input should be a valid Python complex object',
406-
None,
407-
),
408403
(
409404
'complex_str_parsing',
410405
'Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex',

tests/validators/test_complex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
EXPECTED_PARSE_ERROR_MESSAGE = 'Input should be a valid complex string following the rules at https://docs.python.org/3/library/functions.html#complex'
1212
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'
13-
EXPECTED_TYPE_ERROR_PY_STRICT_MESSAGE = 'Input should be a valid Python complex object'
13+
EXPECTED_TYPE_ERROR_PY_STRICT_MESSAGE = 'Input should be an instance of complex'
1414

1515

1616
@pytest.mark.parametrize(

tests/validators/test_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_dict_complex_key():
242242
{'type': 'dict', 'keys_schema': {'type': 'complex', 'strict': True}, 'values_schema': {'type': 'str'}}
243243
)
244244
assert v.validate_python({complex(1, 2): '1'}) == {complex(1, 2): '1'}
245-
with pytest.raises(ValidationError, match='Input should be a valid Python complex object'):
245+
with pytest.raises(ValidationError, match='Input should be an instance of complex'):
246246
assert v.validate_python({'1+2j': b'1'}) == {complex(1, 2): '1'}
247247

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

0 commit comments

Comments
 (0)