Skip to content

Commit

Permalink
Deprecate context (#2731)
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria authored Jan 6, 2025
1 parent a3049aa commit 71ab95a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Bug fixes:

- Typing: Fix type hint for ``nested`` parameter of `Nested <marshmallow.fields.Nested>`.

Deprecations:

- Deprecate ``context`` parameter of `Schema <marshmallow.schema.Schema>` (:issue:`1826`).
Use `contextVars.ContextVar` to pass context data instead.

3.23.3 (2025-01-03)
*******************
Expand Down
7 changes: 7 additions & 0 deletions src/marshmallow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ def __init__(
if unknown is None
else validate_unknown_parameter_value(unknown)
)
if context:
warnings.warn(
"The `context` parameter is deprecated and will be removed in marshmallow 4.0. "
"Use `contextvars.ContextVar` to pass context instead.",
RemovedInMarshmallow4Warning,
stacklevel=2,
)
self.context = context or {}
self._normalize_nested_options()
#: Dictionary mapping field_names -> :class:`Field` objects
Expand Down
4 changes: 3 additions & 1 deletion tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from marshmallow import EXCLUDE, INCLUDE, RAISE, Schema, fields, validate
from marshmallow.exceptions import ValidationError
from marshmallow.validate import Equal
from marshmallow.warnings import RemovedInMarshmallow4Warning
from tests.base import (
ALL_FIELDS,
DateEnum,
Expand Down Expand Up @@ -992,7 +993,8 @@ class Parent(Schema):
lambda x: None,
deserialize=lambda val, context: val.upper() + context["key"],
)
field.parent = Parent(context={"key": "BAR"})
with pytest.warns(RemovedInMarshmallow4Warning):
field.parent = Parent(context={"key": "BAR"})
assert field.deserialize("foo") == "FOOBAR"

def test_function_field_passed_deserialize_only_is_load_only(self):
Expand Down
19 changes: 12 additions & 7 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
StringNotCollectionError,
ValidationError,
)
from marshmallow.warnings import RemovedInMarshmallow4Warning
from tests.base import (
Blog,
BlogOnlySchema,
Expand Down Expand Up @@ -364,11 +365,12 @@ def on_bind_field(self, field_name, field_obj):

foo = fields.Nested(NestedSchema)

schema1 = MySchema(context={"fname": "foobar"})
schema2 = MySchema(context={"fname": "quxquux"})
with pytest.warns(RemovedInMarshmallow4Warning):
schema1 = MySchema(context={"fname": "foobar"})
schema2 = MySchema(context={"fname": "quxquux"})

assert schema1.fields["foo"].schema.fields["bar"].metadata["fname"] == "foobar"
assert schema2.fields["foo"].schema.fields["bar"].metadata["fname"] == "quxquux"
assert schema1.fields["foo"].schema.fields["bar"].metadata["fname"] == "foobar"
assert schema2.fields["foo"].schema.fields["bar"].metadata["fname"] == "quxquux"


class TestValidate:
Expand Down Expand Up @@ -2518,7 +2520,8 @@ class CSchema(Schema):
ser = CSchema()
ser.context["info"] = "i like bikes"
obj = {"inner": {}}
result = ser.dump(obj)
with pytest.warns(RemovedInMarshmallow4Warning):
result = ser.dump(obj)
assert result["inner"]["likes_bikes"] is True

# Regression test for https://github.com/marshmallow-code/marshmallow/issues/820
Expand Down Expand Up @@ -2572,8 +2575,10 @@ def __deepcopy__(self, _):
class InnerSchema(Schema):
foo = fields.Field()

class OuterSchema(Schema):
inner = fields.Nested(InnerSchema(context={"unp": Unpicklable()}))
with pytest.warns(RemovedInMarshmallow4Warning):

class OuterSchema(Schema):
inner = fields.Nested(InnerSchema(context={"unp": Unpicklable()}))

outer = OuterSchema()
obj = {"inner": {"foo": 42}}
Expand Down
4 changes: 3 additions & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from marshmallow import Schema, fields
from marshmallow import missing as missing_
from marshmallow.warnings import RemovedInMarshmallow4Warning
from tests.base import ALL_FIELDS, DateEnum, GenderEnum, HairColorEnum, User, central


Expand Down Expand Up @@ -110,7 +111,8 @@ class Parent(Schema):
field = fields.Function(
serialize=lambda obj, context: obj.name.upper() + context["key"]
)
field.parent = Parent(context={"key": "BAR"})
with pytest.warns(RemovedInMarshmallow4Warning):
field.parent = Parent(context={"key": "BAR"})
assert "FOOBAR" == field.serialize("key", user)

def test_function_field_passed_uncallable_object(self):
Expand Down

0 comments on commit 71ab95a

Please sign in to comment.