-
-
Notifications
You must be signed in to change notification settings - Fork 629
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
Wrapped validates_schema
method is not called when .load
is provided with a generator.
#1898
Comments
I don't think this argument is intended to accept generator iterators. The |
That still poses the risk of the validator accepting a generator at runtime and not behaving as one would expect. Is it viable to implement an assertion or a warning if a generator is provided so that there is some form of runtime guard? |
Oh, there is a validator and it does explicitly allow any iterable. marshmallow/src/marshmallow/utils.py Lines 48 to 55 in e9f151f
I suspect we would want to wrap it in |
I started on a patch to do the diff --git a/src/marshmallow/schema.py b/src/marshmallow/schema.py
index ff119a4..449a45c 100644
--- a/src/marshmallow/schema.py
+++ b/src/marshmallow/schema.py
@@ -836,11 +836,22 @@ class Schema(base.SchemaABC, metaclass=SchemaMeta):
unknown = unknown or self.unknown
if partial is None:
partial = self.partial
+ # before preprocessors, convert data to a list if it is an iterable and many=True
+ # this ensures that if a generator is used, it will be iterated only once
+ processed_data: typing.Mapping[str, typing.Any] | list[
+ typing.Mapping[str, typing.Any]
+ ] = data
+ if is_collection(data) and many:
+ processed_data = list(data)
# Run preprocessors
if self._has_processors(PRE_LOAD):
try:
processed_data = self._invoke_load_processors(
- PRE_LOAD, data, many=many, original_data=data, partial=partial
+ PRE_LOAD,
+ processed_data,
+ many=many,
+ original_data=data,
+ partial=partial,
)
except ValidationError as err:
errors = err.normalized_messages() But this means that if I have a I would prefer updating the annotation to take As for checking and rejecting generators as inputs... we could, but you can also use other types which implement Iterator but not Sequence. I'm not convinced that an explicit |
#2795 addresses the incorrect annotation as well as the run-time type-checking in the code (i.e. checking |
Issue
When passing a generator to
.load
, the wrappedvalidates_schema
method is never called.Expected
When passing a generator to
.load
, it should behave the same as passing a list or tuple and call the method wrapped byvalidates_schema
.Environment
python v3.9.5
marshmallow 3.13.0
Example
The text was updated successfully, but these errors were encountered: