Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate enum values against other possible forms of expected values #1502

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/serializers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::tools::SchemaDict;

use super::errors::py_err_se_err;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
#[allow(clippy::struct_field_names)]
pub(crate) struct SerializationConfig {
pub timedelta_mode: TimedeltaMode,
Expand Down Expand Up @@ -57,6 +57,15 @@ macro_rules! serialization_mode {
$($variant,)*
}

impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
$(Self::$variant => write!(f, $value),)*
}

}
}

impl FromStr for $name {
type Err = PyErr;

Expand Down
2 changes: 1 addition & 1 deletion src/serializers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::definitions::{Definitions, DefinitionsBuilder};
use crate::py_gc::PyGcTraverse;

pub(crate) use config::BytesMode;
use config::SerializationConfig;
pub(crate) use config::SerializationConfig;
pub use errors::{PydanticSerializationError, PydanticSerializationUnexpectedValue};
use extra::{CollectWarnings, SerRecursionState, WarningsMode};
pub(crate) use extra::{DuckTypingSerMode, Extra, SerMode, SerializationState};
Expand Down
47 changes: 41 additions & 6 deletions src/validators/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use pyo3::types::{PyDict, PyFloat, PyInt, PyList, PyString, PyType};
use crate::build_tools::{is_strict, py_schema_err};
use crate::errors::{ErrorType, ValError, ValResult};
use crate::input::Input;
use crate::serializers::{to_jsonable_python, SerializationConfig};
use crate::tools::{safe_repr, SchemaDict};

use super::is_instance::class_repr;
use super::literal::{expected_repr_name, LiteralLookup};
use super::InputType;
use super::{BuildValidator, CombinedValidator, DefinitionsBuilder, Exactness, ValidationState, Validator};

#[derive(Debug, Clone)]
Expand All @@ -33,27 +35,55 @@ impl BuildValidator for BuildEnumValidator {

let py = schema.py();
let value_str = intern!(py, "value");
let expected: Vec<(Bound<'_, PyAny>, PyObject)> = members
let expected_py: Vec<(Bound<'_, PyAny>, PyObject)> = members
.iter()
.map(|v| Ok((v.getattr(value_str)?, v.into())))
.collect::<PyResult<_>>()?;
let ser_config = SerializationConfig::from_config(config).unwrap_or_default();
let expected_json: Vec<(Bound<'_, PyAny>, PyObject)> = members
.iter()
.map(|v| {
Ok((
to_jsonable_python(
py,
&v.getattr(value_str)?,
None,
None,
false,
false,
false,
&ser_config.timedelta_mode.to_string(),
&ser_config.bytes_mode.to_string(),
&ser_config.inf_nan_mode.to_string(),
false,
None,
true,
None,
)?
.into_bound(py),
v.into(),
))
})
.collect::<PyResult<_>>()?;

let repr_args: Vec<String> = expected
let repr_args: Vec<String> = expected_py
.iter()
.map(|(k, _)| k.repr()?.extract())
.collect::<PyResult<_>>()?;

let class: Bound<PyType> = schema.get_as_req(intern!(py, "cls"))?;
let class_repr = class_repr(schema, &class)?;

let lookup = LiteralLookup::new(py, expected.into_iter())?;
let py_lookup = LiteralLookup::new(py, expected_py.into_iter())?;
let json_lookup = LiteralLookup::new(py, expected_json.into_iter())?;

macro_rules! build {
($vv:ty, $name_prefix:literal) => {
EnumValidator {
phantom: PhantomData::<$vv>,
class: class.clone().into(),
lookup,
py_lookup,
json_lookup,
missing: schema.get_as(intern!(py, "missing"))?,
expected_repr: expected_repr_name(repr_args, "").0,
strict: is_strict(schema, config)?,
Expand Down Expand Up @@ -87,7 +117,8 @@ pub trait EnumValidateValue: std::fmt::Debug + Clone + Send + Sync {
pub struct EnumValidator<T: EnumValidateValue> {
phantom: PhantomData<T>,
class: Py<PyType>,
lookup: LiteralLookup<PyObject>,
py_lookup: LiteralLookup<PyObject>,
json_lookup: LiteralLookup<PyObject>,
missing: Option<PyObject>,
expected_repr: String,
strict: bool,
Expand Down Expand Up @@ -120,7 +151,11 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> {

state.floor_exactness(Exactness::Lax);

if let Some(v) = T::validate_value(py, input, &self.lookup, strict)? {
let lookup = match state.extra().input_type {
InputType::Json => &self.json_lookup,
_ => &self.py_lookup,
};
if let Some(v) = T::validate_value(py, input, lookup, strict)? {
return Ok(v);
} else if let Ok(res) = class.as_unbound().call1(py, (input.as_python(),)) {
return Ok(res);
Expand Down
52 changes: 52 additions & 0 deletions tests/validators/test_enums.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import re
import sys
from decimal import Decimal
Expand Down Expand Up @@ -262,6 +263,57 @@ class MyEnum(Enum):
assert v.validate_python([2]) is MyEnum.b


def test_plain_enum_tuple():
class MyEnum(Enum):
a = 1, 2
b = 2, 3

assert MyEnum((1, 2)) is MyEnum.a
v = SchemaValidator(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())))
assert v.validate_python((1, 2)) is MyEnum.a
assert v.validate_python((2, 3)) is MyEnum.b
assert v.validate_json('[1, 2]') is MyEnum.a


def test_plain_enum_datetime():
class MyEnum(Enum):
a = datetime.datetime.fromisoformat('2024-01-01T00:00:00')

v = SchemaValidator(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())))
assert v.validate_python(datetime.datetime.fromisoformat('2024-01-01T00:00:00')) is MyEnum.a
assert v.validate_json('"2024-01-01T00:00:00"') is MyEnum.a


def test_plain_enum_complex():
class MyEnum(Enum):
a = complex(1, 2)

v = SchemaValidator(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())))
assert v.validate_python(complex(1, 2)) is MyEnum.a
assert v.validate_json('"1+2j"') is MyEnum.a


def test_plain_enum_identical_serialized_form():
class MyEnum(Enum):
tuple_ = 1, 2
list_ = [1, 2]

v = SchemaValidator(core_schema.enum_schema(MyEnum, list(MyEnum.__members__.values())))
assert v.validate_python((1, 2)) is MyEnum.tuple_
assert v.validate_python([1, 2]) is MyEnum.list_
assert v.validate_json('[1,2]') is MyEnum.tuple_

# Change the order of `a` and `b` in MyEnum2; validate_json should pick [1, 2] this time
class MyEnum2(Enum):
list_ = [1, 2]
tuple_ = 1, 2

v = SchemaValidator(core_schema.enum_schema(MyEnum2, list(MyEnum2.__members__.values())))
assert v.validate_python((1, 2)) is MyEnum2.tuple_
assert v.validate_python([1, 2]) is MyEnum2.list_
assert v.validate_json('[1,2]') is MyEnum2.list_


def test_plain_enum_empty():
class MyEnum(Enum):
pass
Expand Down