flask-smorest icon indicating copy to clipboard operation
flask-smorest copied to clipboard

as_kwargs=True always?

Open revmischa opened this issue 5 years ago • 5 comments

I like using as_kwargs, coming from flask-apispec which has a @use_kwargs decorator.

Feature request: have a decorator that has as_kwargs=True so I don't have to type it every time

Thanks!

revmischa avatar Mar 19 '19 13:03 revmischa

Thanks for the feedback.

I see several options

  • Create a new decorator that would be an alias to arguments with as_kwargs set to True (your suggestion).

  • Add a DEFAULT_AS_KWARGS class attribute to set the default value for as_kwargs.

  • If we went ahead with https://github.com/marshmallow-code/flask-smorest/issues/44#issuecomment-471619084, we could have use_args and use_kwargs to specify deserialization schemas, and arguments (or parameters) to add extra information in the docs.

I'd like to give it some thought before doing anything.

Meanwhile, if you want to avoid duplication, you can subclass Blueprint to add use_kwargs. It should be straightforward.

lafrech avatar Mar 19 '19 13:03 lafrech

Oh and while I'm making a wishlist - always having strict=True enabled would be nice too :)

revmischa avatar Mar 20 '19 15:03 revmischa

I think this is a marshmallow issue.

strict is always True in marshmallow 3. If you're starting a new project, you could try to use marshmallow 3.

If you're stuck on marshmallow 2 for now, you may use a BaseSchema but don't erase Meta when subclassing:

class BaseSchema(ma.Schema):
    class Meta:
        strict = True

class MySchema(BaseSchema):
    class Meta(BaseSchema.Meta):  # Don't forget to also subclass Meta (or duplicate strict = True below)
        fields = ...

lafrech avatar Mar 20 '19 15:03 lafrech

@revmischa I'm open to adding a kwarguments decorator. Would you like to submit a PR?

lafrech avatar Mar 30 '19 09:03 lafrech

It might be a good idea to edit ArgumentsMixin class as following:

from functools import partial

class ArgumentsMixin:
    def kwarguments(self):
        return partial(self.arguments, as_kwargs=True)

BnGx avatar Feb 18 '21 13:02 BnGx