1

I have a GET router function which searches for employee(s) and return complete information of employee(s) . It works fine but when I check code quality it shows too-many-arguments error in async function. I don't wants to disable this pylint error since its not a good practice. Here is my code snippet.

@router.get('/employee', response_model=List[schemas.Employee],
    responses={502: {"model": schemas.ErrorResponse},
    422: {"model": schemas.ErrorResponse}},  status_code=200
async def get_employee(request: Request,emp_name: str = Query(None, example="John"),
    search_string: str = Query(None, example="Canada"),
    skip: int = Query(0, ge=0),
    limit: int = Query(100, ge=0),
    db_: Session = Depends(get_db)):
    '''fetches all the employees with details
    * the optional query parameter can be used to filter the result set
    * skip=n: skips the first n objects in return list
    * limit=n: limits the no. of items to be returned to n'''
    return search_employee(db_, emp_name=emp_name,
        search_string = search_string, skip=skip, limit=limit)
        

When i use args/**kwargs as below I could only see a single field in swagger named args/kwargs.

async def get_persons(request: Request, db_: Session = Depends(get_db), **kwargs):
    emp_name = kwargs.get('emp_name', None)
    search_string = kwargs.get('search_string', None)
    skip = kwargs.get('skip', 0)
    limit = kwargs.get('limit', 100)

    return search_employee(db_, emp_name=emp_name,
          search_string = search_string, skip=skip, limit=limit)

What I need in Swagger is different fields emp_name,search_string,skip,limit where I can enter values separately. Please help. TIA

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • You might find [this](https://stackoverflow.com/a/75998823/17865804), as well as [this](https://stackoverflow.com/a/70845425/17865804) and [this](https://stackoverflow.com/a/73067473/17865804) helpful – Chris Aug 10 '23 at 15:36

1 Answers1

0

If this was not a hard requirement of @router.get there's a maintenance benefit in refactoring the argument of your function. This is what pylint is warning about.

But if the API forces you to have a particular number of arguments for your code to work properly, then you should disable the pylint warning. Following the expected practice of @router.get is a lot more valuable than following this particular refactoring message.

You know better than pylint, think about the reason for the pylint message, then disable if it's too costly to fix, not valuable enough or if following the advice would make the code worse like in this case.

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48