Skip to content

Commit

Permalink
just skip first param instead of by name "request"
Browse files Browse the repository at this point in the history
this re-enables (worked before 1.0) view signatures like

`def view(_: HttpRequest)` to avoid linters complaining about the unused
`request` param
  • Loading branch information
davidszotten committed Dec 23, 2023
1 parent 32230c1 commit 7dd4126
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 6 additions & 6 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import warnings
from collections import defaultdict, namedtuple
from itertools import islice
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple

import pydantic
Expand Down Expand Up @@ -52,12 +53,11 @@ def __init__(self, path: str, view_func: Callable) -> None:
self.has_kwargs = False

self.params = []
for name, arg in self.signature.parameters.items():
if name == "request":
# TODO: maybe better assert that 1st param is request or check by type?
# maybe even have attribute like `has_request`
# so that users can ignore passing request if not needed
continue
for name, arg in islice(self.signature.parameters.items(), 1, None):
# skip the first param since that must be the request
# TODO: maybe better assert that 1st param is request or check by type?
# maybe even have attribute like `has_request`
# so that users can ignore passing request if not needed

if arg.kind == arg.VAR_KEYWORD:
# Skipping **kwargs
Expand Down
19 changes: 18 additions & 1 deletion tests/test_signature_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from sys import version_info

import pytest
from django.http import HttpRequest

from ninja.signature.details import is_collection_type
from ninja.signature.details import ViewSignature, is_collection_type


@pytest.mark.parametrize(
Expand Down Expand Up @@ -49,3 +50,19 @@
)
def test_is_collection_type_returns(annotation: typing.Any, expected: bool):
assert is_collection_type(annotation) is expected


def test_finds_request_param_by_name():
def view(request):
pass

signature = ViewSignature("", view)
assert signature.models == []


def test_finds_request_param_by_type():
def view(_: HttpRequest):
pass

signature = ViewSignature("", view)
assert signature.models == []

0 comments on commit 7dd4126

Please sign in to comment.