django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Ruff's `ARG001` False Positive for an Endpoint w/o Usage of `request`

Open UnoYakshi opened this issue 7 months ago • 3 comments

In an endpoint with explicitly unused request argument, Ruff triggers ARG001. One of the member asked me to open an issue in Django-Ninja to check if it's possible or not (figuratively) to make request argument optional.

For more detailed context, please, refer to the Ruff issue.

# Imports and stuff...

@router.get('/', response=list[UserSchema])
def get_users(request: HttpRequest) -> list[UserSchema]:  # Triggers ARG001!
    """Retrieve all Users..."""

    return [UserSchema.from_orm(user) for user in BaseUser.objects.all()]

UnoYakshi avatar May 04 '25 22:05 UnoYakshi

@UnoYakshi - long story short - no - cannot make it optional - we try to be as much similar to django views where request is always passed to view function (lot of 3rd party apps/plugins/decorators depend on it)

what you can try is adding _:

def get_users(_request: HttpRequest) -> list[UserSchema]: 

vitalik avatar May 05 '25 11:05 vitalik

@vitalik, I immediately (when I launch Django app) get

NameError: Fields must not use names with leading underscores; e.g., use 'request' instead of '_request'.

UnoYakshi avatar May 14 '25 07:05 UnoYakshi

Got into the same problem, don't know how to fix it elegantly

sujh avatar Sep 04 '25 02:09 sujh