Skip to content

Commit

Permalink
Remove usage of implicit field creation; add warning about context
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Jan 11, 2025
1 parent f508083 commit 4a03ba3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/custom_fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ Both :class:`Function <marshmallow.fields.Function>` and :class:`Method <marshma
Adding context to `Method` and `Function` fields
------------------------------------------------

.. warning::

The ``context`` attribute is deprecated and will be removed in marshmallow 4.
Use `contextvars.ContextVar` for passing context to fields, pre-/post-processing methods, and validators instead.
marshmallow 4 will also provide an `experimental helper API <https://marshmallow.readthedocs.io/en/latest/marshmallow.experimental.context.html>`_
for using context.

A :class:`Function <marshmallow.fields.Function>` or :class:`Method <marshmallow.fields.Method>` field may need information about its environment to know how to serialize a value.

In these cases, you can set the ``context`` attribute (a dictionary) of a `Schema <marshmallow.Schema>`. :class:`Function <marshmallow.fields.Function>` and :class:`Method <marshmallow.fields.Method>` fields will have access to this dictionary.
Expand Down
3 changes: 0 additions & 3 deletions docs/why.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ As an example, you might have a JSON endpoint for retrieving all information abo
players = fields.List(fields.Nested(PlayerSchema))
last_changed = fields.DateTime(format="rfc")
class Meta:
additional = ("title", "date_created", "type", "is_active")
# Serializes full game state
full_serializer = GameStateSchema()
Expand Down

0 comments on commit 4a03ba3

Please sign in to comment.