Coder Social home page Coder Social logo

Comments (2)

sirosen avatar sirosen commented on June 11, 2024 1

I'd be willing to discuss adding partial: bool | None as an argument to use_args, use_kwargs, and parse in a separate issue, if that's a desirable feature. Likewise for any other arguments to Schema.load

But I'll say up front that I'm -0 on it because there's a viable workaround and it could easily really mess up use_kwargs usage.

With that out of the way, I'm going to close this because it's a question which I believe I've answered sufficiently. If there's anything which is still unclear, please feel free to ask for more detail.

from webargs.

sirosen avatar sirosen commented on June 11, 2024

webargs doesn't have support for passing all of the potential arguments for Schema.load, so you can't do this directly. What you can do, however, is take advantage of the fact that partial defaults to whatever value is set on the schema instance used. That means that if you use a schema object instead of a dict for use_kwargs, you can set partial on it.

To isolate this problem from your surrounding context a bit, consider this simpler schema + webargs usage:

import marshmallow as ma
from webargs.flaskparser import use_args  # any parser, flask as an example

class Point(ma.Schema):
    x = ma.fields.Int(required=True)
    y = ma.fields.Int(required=True)

@use_args({"start": ma.fields.Nested(Point()), "end": ma.fields.Nested(Point())})
def foo(segment):
    ...

To set partial=True on the schema, you won't be able to use the dict form. Instead, create a dedicated schema and set partial=True on it when you instantiate it:

class Segment(ma.Schema):
    start = ma.fields.Nested(Point())
    end = ma.fields.Nested(Point())

@use_args(Segment(partial=True))
def foo(segment):
    ...

I'd recommend use_args or parse rather than use_kwargs if you do this though. partial=True means that fields could be missing and therefore not passed as arguments via use_kwargs.


This isn't the only solution to partial loading. The major alternative, one which I would recommend considering, is to use a schema factory function to create your schemas with the desired settings. e.g.

def make_point_schema(*, partial=False):
    class Point(ma.Schema):
        x = ma.fields.Int(required=not partial)
        y = ma.fields.Int(required=not partial)

    return Point

Point = make_point_schema()
PartialPoint = make_point_schema(partial=True)

I prefer this over partial=True/False because it offers much finer grained control. But it is a bit more verbose.

I'm not sure what we should do about not being able to use partial as a parameter to Schema.load. We could add support, and it wouldn't be too hard to do so. But it complicates the interface, and it is a relatively niche case. I don't know that, given that we haven't seen frequent requests for it, we should add support.

from webargs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.