Skip to content

RFC: support passing the whole parsed arguments as a keyword argument #830

@greyli

Description

@greyli

Hi team,

Currently, we provide two options to pass the parsed arguments to the view function:

  • use_args
  • use_kwargs

It seems there is a mismatch here. use_args passes the parsed arguments as a single positional argument, while the use_kwargs passes all the arguments as separate keyword arguments.

I'm thinking of adding the missing feature between this mismatch, which is to support passing the whole parsed arguments as a keyword argument.

What are the benefits? It can help to ensure the natural order for Flask views:

@app.post('/pets/<pet_id>')
@use_args(PetQueryIn, location='json')
@use_args(PetBodyIn, location='query')
def create_pet(json_data, query_data, pet_id):
    pass

Since the URL variables (i.e. pet_id) will be passed as keyword arguments, we have to declare the arguments from use_args first.

Although we can use use_kwargs to pass all arguments as keyword arguments, but consider we will use three stacked use_kwargs and each schema contains five fields...

I made some experiments, there are two ways to achieve this:

  • Create a new function to pass the whole parsed arguments as a keyword argument (maybe called use_named_args):
@app.post('/pets/<pet_id>')
@use_named_args(PetQueryIn, location='json', arg_name='data')
@use_named_args(PetBodyIn, location='query', arg_name='query_data')
def create_pet(pet_id, data, query_data):
    pass
  1. Add an arg_name argument to use_args and use_kwargs.
@app.post('/pets/<pet_id>')
@use_kwargs(PetQueryIn, location='json', arg_name='json_data')
@use_kwargs(PetBodyIn, location='query', arg_name='query_data')
def create_pet(json_data, query_data, pet_id):
    pass

Do you think it's a useful feature? Which solution is preferred?

I'm happy to submit a PR. Thanks!

Related issue: apiflask/apiflask#427

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions