marshmallow-code / marshmallow-code/marshmallow

Consider moving `marshmallow-oneofschema` into the core library?

Open
#1,751 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

OneOfSchema is the go-to answer for questions about polymorphism.

I would like to see it on-course to have the following two things happen:

  1. Avoid overriding load and dump, so that hooks work

As I have proposed elsewhere, I think it should hook in at _deserialize and _serialize. It's hard to make the case for extending marshmallow to support this use-case right now.

  1. Built-in support in apispec

This is possible without bringing it into marshmallow, but harder to justify.

The current marshmallow-oneofschema code is quite small in size, only a little over 100 LLOC.
Plus, I think we can make it even smaller and simpler.

We could boil the whole thing down to something like this:

class OneOfSchema(Schema):
    oneof: Dict[str, Type[Schema]] = {}
    discriminator: Optional[str] = None
    def discriminate_on_load(self, data):
        return self.oneof[self.discriminator]
    def discriminate_on_dump(self, data):
        return self.oneof[data["discriminator"]]
    def _serialize(...): ...
    def _deserialize(...): ...

I also think moving it into marshmallow would result in more active maintenance.

@sloria, @lafrech, @deckar01, has this been discussed in the past? I wasn't able to find any useful clues other than some comments about not wanting to do this in the past. I understand not wanting to grow the public surface of a package and take on the related doc, bugfix, Q&A, etc workload, but in this case I think it's counterproductive.

I'd be happy to work on this if it stood a chance at being accepted.

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 reviewing the existing marshmallow-oneofschema implementation and marshmallow's _serialize and _deserialize hooks, then assess the proposed apispec support. Done would require an accepted design and agreement on whether this functionality belongs in the core library; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.