mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-07 04:34:29 +08:00
Make decorator functions transparent to Mypy (#306)
By declaring return type as -> Callable[[_C], _C], Mypy can infer that the decorated function has also the same arguments and return type as the original. View functions are constrained to return HttpResponseBase (or any subclass of it). Also added typecheck test coverage to most of the cases.
This commit is contained in:
43
test-data/typecheck/contrib/auth/test_decorators.yml
Normal file
43
test-data/typecheck/contrib/auth/test_decorators.yml
Normal file
@@ -0,0 +1,43 @@
|
||||
- case: login_required_bare
|
||||
main: |
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@login_required
|
||||
def view_func(request): ...
|
||||
reveal_type(view_func) # N: Revealed type is 'def (request: Any) -> Any'
|
||||
- case: login_required_fancy
|
||||
main: |
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.http import HttpResponse
|
||||
@login_required(redirect_field_name='a', login_url='b')
|
||||
def view_func(request: WSGIRequest, arg: str) -> HttpResponse: ...
|
||||
reveal_type(view_func) # N: Revealed type is 'def (request: django.core.handlers.wsgi.WSGIRequest, arg: builtins.str) -> django.http.response.HttpResponse'
|
||||
- case: login_required_weird
|
||||
main: |
|
||||
from django.contrib.auth.decorators import login_required
|
||||
# This is non-conventional usage, but covered in Django tests, so we allow it.
|
||||
def view_func(request): ...
|
||||
wrapped_view = login_required(view_func, redirect_field_name='a', login_url='b')
|
||||
reveal_type(wrapped_view) # N: Revealed type is 'def (request: Any) -> Any'
|
||||
- case: login_required_incorrect_return
|
||||
main: |
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@login_required() # E: Value of type variable "_VIEW" of function cannot be "Callable[[Any], str]"
|
||||
def view_func2(request) -> str: ...
|
||||
- case: user_passes_test
|
||||
main: |
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
@user_passes_test(lambda u: u.username.startswith('super'))
|
||||
def view_func(request): ...
|
||||
reveal_type(view_func) # N: Revealed type is 'def (request: Any) -> Any'
|
||||
- case: user_passes_test_bare_is_error
|
||||
main: |
|
||||
from django.http.response import HttpResponse
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[Any], HttpResponse]"; expected "Callable[[AbstractUser], bool]"
|
||||
def view_func(request) -> HttpResponse: ...
|
||||
- case: permission_required
|
||||
main: |
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
@permission_required('polls.can_vote')
|
||||
def view_func(request): ...
|
||||
28
test-data/typecheck/db/test_transaction.yml
Normal file
28
test-data/typecheck/db/test_transaction.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
- case: atomic_bare
|
||||
main: |
|
||||
from django.db.transaction import atomic
|
||||
@atomic
|
||||
def func(x: int) -> list: ...
|
||||
reveal_type(func) # N: Revealed type is 'def (x: builtins.int) -> builtins.list[Any]'
|
||||
- case: atomic_args
|
||||
main: |
|
||||
from django.db.transaction import atomic
|
||||
@atomic(using='bla', savepoint=False)
|
||||
def func(x: int) -> list: ...
|
||||
reveal_type(func) # N: Revealed type is 'def (x: builtins.int) -> builtins.list[Any]'
|
||||
- case: non_atomic_requests_bare
|
||||
main: |
|
||||
from django.db.transaction import non_atomic_requests
|
||||
@non_atomic_requests
|
||||
def view_func(request): ...
|
||||
reveal_type(view_func) # N: Revealed type is 'def (request: Any) -> Any'
|
||||
|
||||
- case: non_atomic_requests_args
|
||||
main: |
|
||||
from django.http.request import HttpRequest
|
||||
from django.http.response import HttpResponse
|
||||
from django.db.transaction import non_atomic_requests
|
||||
@non_atomic_requests
|
||||
def view_func(request: HttpRequest, arg: str) -> HttpResponse: ...
|
||||
reveal_type(view_func) # N: Revealed type is 'def (request: django.http.request.HttpRequest, arg: builtins.str) -> django.http.response.HttpResponse'
|
||||
|
||||
20
test-data/typecheck/utils/test_decorators.yml
Normal file
20
test-data/typecheck/utils/test_decorators.yml
Normal file
@@ -0,0 +1,20 @@
|
||||
- case: method_decorator_class
|
||||
main: |
|
||||
from django.views.generic.base import View
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class TestView(View): ...
|
||||
reveal_type(TestView()) # N: Revealed type is 'main.TestView'
|
||||
- case: method_decorator_function
|
||||
main: |
|
||||
from django.views.generic.base import View
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http.response import HttpResponse
|
||||
from django.http.request import HttpRequest
|
||||
class TestView(View):
|
||||
@method_decorator(login_required)
|
||||
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
reveal_type(dispatch) # N: Revealed type is 'def (self: main.TestView, request: django.http.request.HttpRequest, *args: Any, **kwargs: Any) -> django.http.response.HttpResponse'
|
||||
Reference in New Issue
Block a user