Skip to content

Commit

Permalink
Merge branch '3.x-line' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Jan 20, 2025
2 parents 7eefd04 + 94234ac commit 944f6de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ Bug fixes:

- Respect ``data_key`` when schema validators raise a `ValidationError <marshmallow.exceptions.ValidationError>`
with a ``field_name`` argument (:issue:`2170`). Thanks :user:`matejsp` for reporting.
- Correctly handle multiple `@post_load <marshmallow.post_load>` methods where one method appends to
the data and another passes ``pass_original=True`` (:issue:`1755`).
Thanks :user:`ghostwheel42` for reporting.

Documentation:

Expand Down
3 changes: 2 additions & 1 deletion src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from abc import ABCMeta
from collections import defaultdict
from collections.abc import Mapping
from itertools import zip_longest

from marshmallow import class_registry, types
from marshmallow import fields as ma_fields
Expand Down Expand Up @@ -1232,7 +1233,7 @@ def _invoke_processors(
if pass_original:
data = [
processor(item, original, many=many, **kwargs)
for item, original in zip(data, original_data)
for item, original in zip_longest(data, original_data)
]
else:
data = [processor(item, many=many, **kwargs) for item in data]
Expand Down
23 changes: 23 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,26 @@ class Meta:
ExampleSchema(unknown=unknown_val).load({"foo": 42})
else:
ExampleSchema().load({"foo": 42}, unknown=unknown_val)


# https://github.com/marshmallow-code/marshmallow/issues/1755
def test_post_load_method_that_appends_to_data():
class MySchema(Schema):
foo = fields.Int()

@post_load(pass_collection=True)
def append_to_data(self, data, **kwargs):
data.append({"foo": 42})
return data

@post_load(pass_collection=False, pass_original=True)
def noop(self, data, original_data, **kwargs):
if original_data is None: # added item
assert data == {"foo": 42}
else:
assert original_data == {"foo": 24}
assert data == {"foo": 24}
return data

schema = MySchema(many=True)
assert schema.load([{"foo": 24}]) == [{"foo": 24}, {"foo": 42}]

0 comments on commit 944f6de

Please sign in to comment.