Skip to content
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

Make arguments to decorators keyword-only #2789

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ Thanks :user:`ddelange` for the PR.
- Methods decorated with `marshmallow.pre_load`, `marshmallow.post_load`, `marshmallow.validates_schema`,
receive ``unknown`` as a keyword argument (:pr:`1632`).
Thanks :user:`jforand` for the PR.
- Rename ``json_data`` parameter of `marshmallow.Schema.loads` to ``s``
- *Backwards-incompatible*: Arguments to `decorators <marshmallow.decorators>` are keyword-only arguments.
- *Backwards-incompatible*: Rename ``json_data`` parameter of `marshmallow.Schema.loads` to ``s``
for compatibility with most render module implementations (`json`, ``simplejson``, etc.) (:pr:`2764`).
Also make it a positional-only argument.
- Incorrectly declaring a field using a field class rather than instance
Expand Down
64 changes: 36 additions & 28 deletions src/marshmallow/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ def validates(field_name: str) -> Callable[..., Any]:

def validates_schema(
fn: Callable[..., Any] | None = None,
pass_collection: bool = False, # noqa: FBT001, FBT002
pass_original: bool = False, # noqa: FBT001, FBT002
skip_on_field_errors: bool = True, # noqa: FBT001, FBT002
*,
pass_collection: bool = False,
pass_original: bool = False,
skip_on_field_errors: bool = True,
) -> Callable[..., Any]:
"""Register a schema-level validator.

Expand All @@ -109,13 +110,13 @@ def validates_schema(
If ``skip_on_field_errors=True``, this validation method will be skipped whenever
validation errors have been detected when validating fields.

.. versionchanged:: 3.0.0b1
``skip_on_field_errors`` defaults to `True`.
.. versionchanged:: 3.0.0
``partial`` and ``many`` are always passed as keyword arguments to
.. versionchanged:: 3.0.0b1 ``skip_on_field_errors`` defaults to `True`.
.. versionchanged:: 3.0.0 ``partial`` and ``many`` are always passed as keyword arguments to
the decorated method.
.. versionchanged:: 4.0.0
``unknown`` is passed as a keyword argument to the decorated method.
.. versionchanged:: 4.0.0 ``unknown`` is passed as a keyword argument to the decorated method.
.. versionchanged:: 4.0.0 ``pass_many`` is renamed to ``pass_collection``.
.. versionchanged:: 4.0.0 ``pass_collection``, ``pass_original``, and ``skip_on_field_errors``
are keyword-only arguments.
"""
return set_hook(
fn,
Expand All @@ -128,7 +129,8 @@ def validates_schema(

def pre_dump(
fn: Callable[..., Any] | None = None,
pass_collection: bool = False, # noqa: FBT001, FBT002
*,
pass_collection: bool = False,
) -> Callable[..., Any]:
"""Register a method to invoke before serializing an object. The method
receives the object to be serialized and returns the processed object.
Expand All @@ -137,16 +139,18 @@ def pre_dump(
argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.dump` call.
If ``pass_collection=True``, the raw data (which may be a collection) is passed.

.. versionchanged:: 3.0.0
``many`` is always passed as a keyword arguments to the decorated method.
.. versionchanged:: 3.0.0 ``many`` is always passed as a keyword arguments to the decorated method.
.. versionchanged:: 4.0.0 ``pass_many`` is renamed to ``pass_collection``.
.. versionchanged:: 4.0.0 ``pass_collection`` is a keyword-only argument.
"""
return set_hook(fn, PRE_DUMP, many=pass_collection)


def post_dump(
fn: Callable[..., Any] | None = None,
pass_collection: bool = False, # noqa: FBT001, FBT002
pass_original: bool = False, # noqa: FBT001, FBT002
*,
pass_collection: bool = False,
pass_original: bool = False,
) -> Callable[..., Any]:
"""Register a method to invoke after serializing an object. The method
receives the serialized object and returns the processed object.
Expand All @@ -158,15 +162,17 @@ def post_dump(
If ``pass_original=True``, the original data (before serializing) will be passed as
an additional argument to the method.

.. versionchanged:: 3.0.0
``many`` is always passed as a keyword arguments to the decorated method.
.. versionchanged:: 3.0.0 ``many`` is always passed as a keyword arguments to the decorated method.
.. versionchanged:: 4.0.0 ``pass_many`` is renamed to ``pass_collection``.
.. versionchanged:: 4.0.0 ``pass_collection`` and ``pass_original`` are keyword-only arguments.
"""
return set_hook(fn, POST_DUMP, many=pass_collection, pass_original=pass_original)


def pre_load(
fn: Callable[..., Any] | None = None,
pass_collection: bool = False, # noqa: FBT001, FBT002
*,
pass_collection: bool = False,
) -> Callable[..., Any]:
"""Register a method to invoke before deserializing an object. The method
receives the data to be deserialized and returns the processed data.
Expand All @@ -175,19 +181,20 @@ def pre_load(
argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.load` call.
If ``pass_collection=True``, the raw data (which may be a collection) is passed.

.. versionchanged:: 3.0.0
``partial`` and ``many`` are always passed as keyword arguments to
.. versionchanged:: 3.0.0 ``partial`` and ``many`` are always passed as keyword arguments to
the decorated method.
.. versionchanged:: 4.0.0
``unknown`` is passed as a keyword argument to the decorated method.
.. versionchanged:: 4.0.0 ``pass_many`` is renamed to ``pass_collection``.
.. versionchanged:: 4.0.0 ``pass_collection`` is a keyword-only argument.
.. versionchanged:: 4.0.0 ``unknown`` is passed as a keyword argument to the decorated method.
"""
return set_hook(fn, PRE_LOAD, many=pass_collection)


def post_load(
fn: Callable[..., Any] | None = None,
pass_collection: bool = False, # noqa: FBT001, FBT002
pass_original: bool = False, # noqa: FBT001, FBT002
*,
pass_collection: bool = False,
pass_original: bool = False,
) -> Callable[..., Any]:
"""Register a method to invoke after deserializing an object. The method
receives the deserialized data and returns the processed data.
Expand All @@ -199,19 +206,20 @@ def post_load(
If ``pass_original=True``, the original data (before deserializing) will be passed as
an additional argument to the method.

.. versionchanged:: 3.0.0
``partial`` and ``many`` are always passed as keyword arguments to
.. versionchanged:: 3.0.0 ``partial`` and ``many`` are always passed as keyword arguments to
the decorated method.
.. versionchanged:: 4.0.0
``unknown`` is passed as a keyword argument to the decorated method.
.. versionchanged:: 4.0.0 ``pass_many`` is renamed to ``pass_collection``.
.. versionchanged:: 4.0.0 ``pass_collection`` and ``pass_original`` are keyword-only arguments.
.. versionchanged:: 4.0.0 ``unknown`` is passed as a keyword argument to the decorated method.
"""
return set_hook(fn, POST_LOAD, many=pass_collection, pass_original=pass_original)


def set_hook(
fn: Callable[..., Any] | None,
tag: str,
many: bool = False, # noqa: FBT001, FBT002
*,
many: bool = False,
**kwargs: Any,
) -> Callable[..., Any]:
"""Mark decorated function as a hook to be picked up later.
Expand Down
Loading