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

Pass constructor arguments to new schema instances #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

djlambert
Copy link

Type specific schemas don't respect excludes passed to OneOfSchema.

This adds an __init__ method to the OneOfSchema class so the instantiation arguments can be saved and passed to the type specific schemas when they are created.

@creeefs
Copy link

creeefs commented May 2, 2019

👍 Could really use this!

@sloria
Copy link
Member

sloria commented Jul 14, 2019

Closing this, as it has gone stale. Feel free to re-open if this is still relevant.

@sloria sloria closed this Jul 14, 2019
@creeefs
Copy link

creeefs commented Jul 16, 2019

@sloria This is a PR. Rather than closing this as a stale issue, shouldn't you evaluate the PR and the corresponding test?

@sloria
Copy link
Member

sloria commented Jul 16, 2019

My availability for reviewing PRs for this project has been quite low, unfortunately. I close stale PRs to reduce the maintenance burden. My thought is that if the PRs are still relevant, the author will either re-make the PR or someone will speak up, which you've just done.. so I'll re-open 😅

@sloria sloria reopened this Jul 16, 2019
@reritom
Copy link

reritom commented Jul 14, 2022

Any updates on this?

I am encountering this problem because I want to do this:

from marshmallow_oneofschema import OneOfSchema
from marshmallow import Schema, fields


class BaseSchema(Schema):
    type = fields.Str()
    status = fields.Str()

class ChildOneSchema(BaseSchema):
    child_one_attr = fields.Str()

class ChildTwoSchema(BaseSchema):
    child_two_attr = fields.Str()

class ChildSchema(OneOfSchema):
    type_field = "type"
    type_schemas = {
        "child_one": ChildOneSchema,
        "child_two": ChildTwoSchema
    }


ChildSchema(only=("status",)).load({"type": "child_one", "status": "updated"})

which raises ValueError: Invalid fields for <ChildSchema(many=False)>: {'status'}.

Note: The PR in its current form would still raise this ValueError because "only" would get passed to the parent Schema, where the field isn't defined.

So it might need to be updated as:

    def __init__(self, *args, **kwargs):
        self._schema_args = args
        self._schema_kwargs = kwargs

        super().__init__(
            many=kwargs.get("many", False),
            partial=kwargs.get("partial", False),
            context=kwargs.get("context", {})
        )

As a workaround I am currently using:

from marshmallow_oneofschema import OneOfSchema as _OneOfSchema

class OneOfSchema(_OneOfSchema):
    def __init__(self, *args, **kwargs):
        self._init_args = args
        self._init_kwargs = kwargs

        # Init with attrs required by OneOfSchema
        super().__init__(
            many=kwargs.get("many", {}),
            partial=kwargs.get("partials", {}),
            context=kwargs.get("context", {})
        )

    @property
    def type_schemas(self):
        _map =  getattr(self, "type_schema_map")

        class _Getter:
            @staticmethod
            def get(key):
                return _map[key](*self._init_args, **self._init_kwargs)

        return _Getter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants