Skip to content

Ignore: add tests to #610 #616

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

Merged
merged 4 commits into from
May 19, 2023
Merged
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
4 changes: 0 additions & 4 deletions pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,6 @@ class ListSchema(TypedDict, total=False):
min_length: int
max_length: int
strict: bool
allow_any_iter: bool
ref: str
metadata: Any
serialization: IncExSeqOrElseSerSchema
Expand All @@ -1200,7 +1199,6 @@ def list_schema(
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
allow_any_iter: bool | None = None,
ref: str | None = None,
metadata: Any = None,
serialization: IncExSeqOrElseSerSchema | None = None,
Expand All @@ -1221,7 +1219,6 @@ def list_schema(
min_length: The value must be a list with at least this many items
max_length: The value must be a list with at most this many items
strict: The value must be a list with exactly this many items
allow_any_iter: Whether the value can be any iterable
ref: optional unique identifier of the schema, used to reference the schema in other places
metadata: Any other information you want to include with the schema, not used by pydantic-core
serialization: Custom serialization schema
Expand All @@ -1232,7 +1229,6 @@ def list_schema(
min_length=min_length,
max_length=max_length,
strict=strict,
allow_any_iter=allow_any_iter,
ref=ref,
metadata=metadata,
serialization=serialization,
Expand Down
32 changes: 17 additions & 15 deletions src/input/input_abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{PyMultiHostUrl, PyUrl};

use super::datetime::{EitherDate, EitherDateTime, EitherTime, EitherTimedelta};
use super::return_enums::{EitherBytes, EitherString};
use super::{GenericArguments, GenericCollection, GenericIterator, GenericMapping, JsonInput};
use super::{GenericArguments, GenericIterable, GenericIterator, GenericMapping, JsonInput};

#[derive(Debug, Clone, Copy)]
pub enum InputType {
Expand Down Expand Up @@ -166,58 +166,60 @@ pub trait Input<'a>: fmt::Debug + ToPyObject {
self.validate_dict(strict)
}

fn validate_list(&'a self, strict: bool, allow_any_iter: bool) -> ValResult<GenericCollection<'a>> {
if strict && !allow_any_iter {
fn validate_list(&'a self, strict: bool) -> ValResult<GenericIterable<'a>> {
if strict {
self.strict_list()
} else {
self.lax_list(allow_any_iter)
self.lax_list()
}
}
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>>;
fn strict_list(&'a self) -> ValResult<GenericIterable<'a>>;
#[cfg_attr(has_no_coverage, no_coverage)]
fn lax_list(&'a self, _allow_any_iter: bool) -> ValResult<GenericCollection<'a>> {
fn lax_list(&'a self) -> ValResult<GenericIterable<'a>> {
self.strict_list()
}

fn validate_tuple(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_tuple(&'a self, strict: bool) -> ValResult<GenericIterable<'a>> {
if strict {
self.strict_tuple()
} else {
self.lax_tuple()
}
}
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>>;
fn strict_tuple(&'a self) -> ValResult<GenericIterable<'a>>;
#[cfg_attr(has_no_coverage, no_coverage)]
fn lax_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
fn lax_tuple(&'a self) -> ValResult<GenericIterable<'a>> {
self.strict_tuple()
}

fn validate_set(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_set(&'a self, strict: bool) -> ValResult<GenericIterable<'a>> {
if strict {
self.strict_set()
} else {
self.lax_set()
}
}
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>>;
fn strict_set(&'a self) -> ValResult<GenericIterable<'a>>;
#[cfg_attr(has_no_coverage, no_coverage)]
fn lax_set(&'a self) -> ValResult<GenericCollection<'a>> {
fn lax_set(&'a self) -> ValResult<GenericIterable<'a>> {
self.strict_set()
}

fn validate_frozenset(&'a self, strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_frozenset(&'a self, strict: bool) -> ValResult<GenericIterable<'a>> {
if strict {
self.strict_frozenset()
} else {
self.lax_frozenset()
}
}
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>>;
fn strict_frozenset(&'a self) -> ValResult<GenericIterable<'a>>;
#[cfg_attr(has_no_coverage, no_coverage)]
fn lax_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
fn lax_frozenset(&'a self) -> ValResult<GenericIterable<'a>> {
self.strict_frozenset()
}

fn extract_generic_iterable(&'a self) -> ValResult<GenericIterable<'a>>;

fn validate_iter(&self) -> ValResult<GenericIterator>;

fn validate_date(&self, strict: bool) -> ValResult<EitherDate> {
Expand Down
59 changes: 36 additions & 23 deletions src/input/input_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::datetime::{
use super::parse_json::JsonArray;
use super::shared::{float_as_int, int_as_bool, map_json_err, str_as_bool, str_as_int};
use super::{
EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericCollection, GenericIterator, GenericMapping,
EitherBytes, EitherString, EitherTimedelta, GenericArguments, GenericIterable, GenericIterator, GenericMapping,
Input, JsonArgs, JsonInput,
};

Expand Down Expand Up @@ -187,53 +187,62 @@ impl<'a> Input<'a> for JsonInput {
self.validate_dict(false)
}

fn validate_list(&'a self, _strict: bool, _allow_any_iter: bool) -> ValResult<GenericCollection<'a>> {
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
match self {
JsonInput::Array(a) => Ok(a.into()),
JsonInput::Array(a) => Ok(GenericIterable::JsonArray(a)),
_ => Err(ValError::new(ErrorType::ListType, self)),
}
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>> {
self.validate_list(false, false)
fn strict_list(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_list(false)
}

fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
// just as in set's case, List has to be allowed
match self {
JsonInput::Array(a) => Ok(a.into()),
JsonInput::Array(a) => Ok(GenericIterable::JsonArray(a)),
_ => Err(ValError::new(ErrorType::TupleType, self)),
}
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_tuple(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_tuple(false)
}

fn validate_set(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
// we allow a list here since otherwise it would be impossible to create a set from JSON
match self {
JsonInput::Array(a) => Ok(a.into()),
JsonInput::Array(a) => Ok(GenericIterable::JsonArray(a)),
_ => Err(ValError::new(ErrorType::SetType, self)),
}
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_set(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_set(false)
}

fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
// we allow a list here since otherwise it would be impossible to create a frozenset from JSON
match self {
JsonInput::Array(a) => Ok(a.into()),
JsonInput::Array(a) => Ok(GenericIterable::JsonArray(a)),
_ => Err(ValError::new(ErrorType::FrozenSetType, self)),
}
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_frozenset(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_frozenset(false)
}

fn extract_generic_iterable(&self) -> ValResult<GenericIterable> {
match self {
JsonInput::Array(a) => Ok(GenericIterable::JsonArray(a)),
JsonInput::String(s) => Ok(GenericIterable::JsonString(s)),
JsonInput::Object(object) => Ok(GenericIterable::JsonObject(object)),
_ => Err(ValError::new(ErrorType::IterableType, self)),
}
}

fn validate_iter(&self) -> ValResult<GenericIterator> {
match self {
JsonInput::Array(a) => Ok(a.clone().into()),
Expand Down Expand Up @@ -405,41 +414,45 @@ impl<'a> Input<'a> for String {
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn validate_list(&'a self, _strict: bool, _allow_any_iter: bool) -> ValResult<GenericCollection<'a>> {
fn validate_list(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
Err(ValError::new(ErrorType::ListType, self))
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_list(&'a self) -> ValResult<GenericCollection<'a>> {
self.validate_list(false, false)
fn strict_list(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_list(false)
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_tuple(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
Err(ValError::new(ErrorType::TupleType, self))
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_tuple(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_tuple(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_tuple(false)
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_set(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
Err(ValError::new(ErrorType::SetType, self))
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_set(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_set(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_set(false)
}

#[cfg_attr(has_no_coverage, no_coverage)]
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericCollection<'a>> {
fn validate_frozenset(&'a self, _strict: bool) -> ValResult<GenericIterable<'a>> {
Err(ValError::new(ErrorType::FrozenSetType, self))
}
#[cfg_attr(has_no_coverage, no_coverage)]
fn strict_frozenset(&'a self) -> ValResult<GenericCollection<'a>> {
fn strict_frozenset(&'a self) -> ValResult<GenericIterable<'a>> {
self.validate_frozenset(false)
}

fn extract_generic_iterable(&'a self) -> ValResult<GenericIterable<'a>> {
Ok(GenericIterable::JsonString(self))
}

fn validate_iter(&self) -> ValResult<GenericIterator> {
Ok(string_to_vec(self).into())
}
Expand Down
Loading