Skip to content

eager length checks #610

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 14 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 0 additions & 10 deletions pydantic_core/core_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,6 @@ class SetSchema(TypedDict, total=False):
items_schema: CoreSchema
min_length: int
max_length: int
generator_max_length: int
strict: bool
ref: str
metadata: Any
Expand All @@ -1365,7 +1364,6 @@ def set_schema(
*,
min_length: int | None = None,
max_length: int | None = None,
generator_max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
Expand All @@ -1388,9 +1386,6 @@ def set_schema(
items_schema: The value must be a set with items that match this schema
min_length: The value must be a set with at least this many items
max_length: The value must be a set with at most this many items
generator_max_length: At most this many items will be read from a generator before failing validation
This is important because generators can be infinite, and even with a `max_length` on the set,
an infinite generator could run forever without producing more than `max_length` distinct items.
strict: The value must be a set with exactly this many items
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
Expand All @@ -1401,7 +1396,6 @@ def set_schema(
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
generator_max_length=generator_max_length,
strict=strict,
ref=ref,
metadata=metadata,
Expand All @@ -1414,7 +1408,6 @@ class FrozenSetSchema(TypedDict, total=False):
items_schema: CoreSchema
min_length: int
max_length: int
generator_max_length: int
strict: bool
ref: str
metadata: Any
Expand All @@ -1426,7 +1419,6 @@ def frozenset_schema(
*,
min_length: int | None = None,
max_length: int | None = None,
generator_max_length: int | None = None,
strict: bool | None = None,
ref: str | None = None,
metadata: Any = None,
Expand All @@ -1449,7 +1441,6 @@ def frozenset_schema(
items_schema: The value must be a frozenset with items that match this schema
min_length: The value must be a frozenset with at least this many items
max_length: The value must be a frozenset with at most this many items
generator_max_length: The value must generate a frozenset with at most this many items
strict: The value must be a frozenset with exactly this many items
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
Expand All @@ -1460,7 +1451,6 @@ def frozenset_schema(
items_schema=items_schema,
min_length=min_length,
max_length=max_length,
generator_max_length=generator_max_length,
strict=strict,
ref=ref,
metadata=metadata,
Expand Down
37 changes: 15 additions & 22 deletions src/input/input_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ use super::{
GenericMapping, Input, JsonInput, PyArgs,
};

/// Extract generators and deques into a `GenericCollection`
macro_rules! extract_shared_iter {
($type:ty, $obj:ident) => {
if $obj.downcast::<PyIterator>().is_ok() {
Some($obj.into())
} else if is_deque($obj) {
Some($obj.into())
} else {
None
}
};
}

/// Extract dict keys, values and items into a `GenericCollection`
#[cfg(not(PyPy))]
macro_rules! extract_dict_iter {
Expand Down Expand Up @@ -388,12 +375,14 @@ impl<'a> Input<'a> for PyAny {
Ok(list.into())
} else if let Ok(tuple) = self.downcast::<PyTuple>() {
Ok(tuple.into())
} else if let Ok(set) = self.downcast::<PySet>() {
Ok(set.into())
} else if let Ok(frozen_set) = self.downcast::<PyFrozenSet>() {
Ok(frozen_set.into())
} else if let Some(collection) = extract_dict_iter!(self) {
Ok(collection)
} else if allow_any_iter && self.iter().is_ok() {
} else if (allow_any_iter && self.iter().is_ok()) || self.downcast::<PyIterator>().is_ok() || is_deque(self) {
Ok(self.into())
} else if let Some(collection) = extract_shared_iter!(PyList, self) {
Ok(collection)
} else {
Err(ValError::new(ErrorType::ListType, self))
}
Expand All @@ -412,10 +401,14 @@ impl<'a> Input<'a> for PyAny {
Ok(tuple.into())
} else if let Ok(list) = self.downcast::<PyList>() {
Ok(list.into())
} else if let Ok(set) = self.downcast::<PySet>() {
Ok(set.into())
} else if let Ok(frozen_set) = self.downcast::<PyFrozenSet>() {
Ok(frozen_set.into())
} else if let Some(collection) = extract_dict_iter!(self) {
Ok(collection)
} else if let Some(collection) = extract_shared_iter!(PyTuple, self) {
Ok(collection)
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
Ok(self.into())
} else {
Err(ValError::new(ErrorType::TupleType, self))
}
Expand All @@ -440,8 +433,8 @@ impl<'a> Input<'a> for PyAny {
Ok(frozen_set.into())
} else if let Some(collection) = extract_dict_iter!(self) {
Ok(collection)
} else if let Some(collection) = extract_shared_iter!(PyTuple, self) {
Ok(collection)
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
Ok(self.into())
} else {
Err(ValError::new(ErrorType::SetType, self))
}
Expand All @@ -466,8 +459,8 @@ impl<'a> Input<'a> for PyAny {
Ok(tuple.into())
} else if let Some(collection) = extract_dict_iter!(self) {
Ok(collection)
} else if let Some(collection) = extract_shared_iter!(PyTuple, self) {
Ok(collection)
} else if self.downcast::<PyIterator>().is_ok() || is_deque(self) {
Ok(self.into())
} else {
Err(ValError::new(ErrorType::FrozenSetType, self))
}
Expand Down
Loading