Skip to content

Commit

Permalink
also find "request" param by type annotation
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 22, 2023
1 parent 32230c1 commit 1da2284
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple

import pydantic
from django.http import HttpResponse
from django.http import HttpRequest, HttpResponse
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined
from typing_extensions import Annotated, get_args, get_origin
Expand Down Expand Up @@ -53,8 +53,11 @@ def __init__(self, path: str, view_func: Callable) -> None:

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?
annotation = arg.annotation
if name == "request" or (
isinstance(annotation, type) and issubclass(annotation, HttpRequest)
):
# TODO: maybe better assert that 1st param is request
# maybe even have attribute like `has_request`
# so that users can ignore passing request if not needed
continue
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 1da2284

Please sign in to comment.