marshmallow-code / marshmallow-code/marshmallow

Field: respect single responsability principle, don't access parent object

Open
#1,046 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

backwards incompat refactor
Dominant language
Python
Stars
7.2k
Forks
738
Avg merge
1d 23h
Merged PRs (30d)
7

Description

I have the feeling we could simplify `Field._serialize` signature from `_serialize(self, value, attr, obj, **kwargs)` to `_serialize(self, value)`.

From a quick search, `obj` is used in `Nested` to populate the `ValidationError`. I need to get my head around this. Shouldn't it be `nested_obj`?

It is also used in `FormattedString`. This field achieves a function that could be achieved otherwise (property, pre_dump,...). I think it could be dropped without too much collateral damage.

There may be an issue with `Function`, too.

I didn't see where `attr` and `kwargs` are used.

It boils down to whether a field should have access to the whole structure or only its own value.

This is more or less related to https://github.com/marshmallow-code/marshmallow/issues/799 and probably #900/#940.

Edit: Marking as 3.0 because it is a breaking change, but it could be postponed, just like #799. If my intuition is correct, it could make for a simpler and saner interface, forbidding hard-to-debug corner cases.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Field._serialize and its uses in Nested, FormattedString, and possibly Function. Review the related issues #799, #900, and #940 to determine whether the proposed breaking API change is agreed upon. Done means an explicit 3.0 decision about whether fields may access the parent object and which arguments remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.