Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stubs with minor fixes #1038

Merged
merged 5 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions django-stubs/contrib/auth/decorators.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Callable, Iterable, Optional, TypeVar, Union, overload

from django.contrib.auth import REDIRECT_FIELD_NAME as REDIRECT_FIELD_NAME # noqa: F401
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.http.response import HttpResponseBase

_VIEW = TypeVar("_VIEW", bound=Callable[..., HttpResponseBase])

def user_passes_test(
test_func: Callable[[AbstractUser], bool], login_url: Optional[str] = ..., redirect_field_name: str = ...
test_func: Callable[[Union[AbstractBaseUser, AnonymousUser]], bool],
login_url: Optional[str] = ...,
redirect_field_name: str = ...,
) -> Callable[[_VIEW], _VIEW]: ...

# There are two ways of calling @login_required: @with(arguments) and @bare
Expand Down
8 changes: 4 additions & 4 deletions django-stubs/contrib/staticfiles/handlers.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any, Awaitable, Callable, Dict, Mapping, Sequence, Tuple
from urllib.parse import ParseResult

from django.core.handlers.asgi import ASGIHandler, ASGIRequest
from django.core.handlers.asgi import ASGIHandler
from django.core.handlers.base import BaseHandler
from django.core.handlers.wsgi import WSGIHandler, WSGIRequest
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpRequest
from django.http.response import HttpResponseBase
from django.http.response import FileResponse, HttpResponseBase

class StaticFilesHandlerMixin:
handles_files: bool = ...
Expand All @@ -15,7 +15,7 @@ class StaticFilesHandlerMixin:
def get_base_url(self) -> str: ...
def _should_handle(self, path: str) -> bool: ...
def file_path(self, url: str) -> str: ...
def serve(self, request: HttpRequest) -> HttpResponseBase: ...
def serve(self, request: HttpRequest) -> FileResponse: ...
def get_response(self, request: HttpRequest) -> HttpResponseBase: ...
async def get_response_async(self, request: HttpRequest) -> HttpResponseBase: ...

Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/staticfiles/views.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from django.http.request import HttpRequest
from django.http.response import HttpResponseBase
from django.http.response import FileResponse

def serve(request: HttpRequest, path: str, insecure: bool = ..., **kwargs: Any) -> HttpResponseBase: ...
def serve(request: HttpRequest, path: str, insecure: bool = ..., **kwargs: Any) -> FileResponse: ...
6 changes: 3 additions & 3 deletions django-stubs/db/models/query.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ class _QuerySet(Generic[_T, _Row], Collection[_Row], Reversible[_Row], Sized):
def extra(
self,
select: Optional[Dict[str, Any]] = ...,
where: Optional[List[str]] = ...,
params: Optional[List[Any]] = ...,
tables: Optional[List[str]] = ...,
where: Optional[Sequence[str]] = ...,
params: Optional[Sequence[Any]] = ...,
tables: Optional[Sequence[str]] = ...,
order_by: Optional[Sequence[str]] = ...,
select_params: Optional[Sequence[Any]] = ...,
) -> _QuerySet[Any, Any]: ...
Expand Down
1 change: 1 addition & 0 deletions django-stubs/template/backends/jinja2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from django.template.exceptions import TemplateSyntaxError
from .base import BaseEngine

class Jinja2(BaseEngine):
env: Any = ...
context_processors: List[str] = ...
def __init__(self, params: Dict[str, Any]) -> None: ...
@property
Expand Down
9 changes: 5 additions & 4 deletions django-stubs/template/loaders/filesystem.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Iterator, List, Optional
from pathlib import Path
from typing import Iterator, List, Optional, Union

from django.template.base import Origin
from django.template.engine import Engine

from .base import Loader as BaseLoader

class Loader(BaseLoader):
dirs: Optional[List[str]] = ...
def __init__(self, engine: Engine, dirs: Optional[List[str]] = ...) -> None: ...
def get_dirs(self) -> List[str]: ...
dirs: Optional[List[Union[str, Path]]] = ...
def __init__(self, engine: Engine, dirs: Optional[List[Union[str, Path]]] = ...) -> None: ...
def get_dirs(self) -> List[Union[str, Path]]: ...
def get_contents(self, origin: Origin) -> str: ...
def get_template_sources(self, template_name: str) -> Iterator[Origin]: ...
4 changes: 2 additions & 2 deletions django-stubs/test/testcases.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.base import Model
from django.db.models.query import QuerySet, RawQuerySet
from django.forms.fields import EmailField
from django.http.response import HttpResponse, HttpResponseBase
from django.http.response import FileResponse, HttpResponseBase
from django.template.base import Template
from django.test.client import AsyncClient, Client
from django.test.html import Element
Expand Down Expand Up @@ -202,7 +202,7 @@ class FSFilesHandler(WSGIHandler):
base_url: Any = ...
def __init__(self, application: Any) -> None: ...
def file_path(self, url: Any): ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for future: this class can be improved further.

def serve(self, request: Any): ...
def serve(self, request: Any) -> FileResponse: ...

class _StaticFilesHandler(FSFilesHandler):
def get_base_dir(self): ...
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/views/static.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any, Optional

from django.http import FileResponse
from django.http.request import HttpRequest
from django.http.response import HttpResponseBase

def serve(
request: HttpRequest, path: str, document_root: Optional[str] = ..., show_indexes: bool = ...
) -> HttpResponseBase: ...
) -> FileResponse: ...

DEFAULT_DIRECTORY_INDEX_TEMPLATE: str
template_translatable: Any
Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/contrib/auth/test_decorators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
- case: user_passes_test
main: |
from django.contrib.auth.decorators import user_passes_test
@user_passes_test(lambda u: u.username.startswith('super'))
@user_passes_test(lambda u: u.get_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]"
@user_passes_test # E: Argument 1 to "user_passes_test" has incompatible type "Callable[[Any], HttpResponse]"; expected "Callable[[Union[AbstractBaseUser, AnonymousUser]], bool]"
def view_func(request) -> HttpResponse: ...
- case: permission_required
main: |
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/managers/querysets/test_from_queryset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
reveal_type(MyModel.objects.difference) # N: Revealed type is "def (*other_qs: Any) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.distinct) # N: Revealed type is "def (*field_names: Any) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.exclude) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.extra) # N: Revealed type is "def (select: Union[builtins.dict[builtins.str, Any], None] =, where: Union[builtins.list[builtins.str], None] =, params: Union[builtins.list[Any], None] =, tables: Union[builtins.list[builtins.str], None] =, order_by: Union[typing.Sequence[builtins.str], None] =, select_params: Union[typing.Sequence[Any], None] =) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.extra) # N: Revealed type is "def (select: Union[builtins.dict[builtins.str, Any], None] =, where: Union[typing.Sequence[builtins.str], None] =, params: Union[typing.Sequence[Any], None] =, tables: Union[typing.Sequence[builtins.str], None] =, order_by: Union[typing.Sequence[builtins.str], None] =, select_params: Union[typing.Sequence[Any], None] =) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.intersection) # N: Revealed type is "def (*other_qs: Any) -> myapp.models.MyQuerySet[myapp.models.MyModel]"
reveal_type(MyModel.objects.none) # N: Revealed type is "def () -> myapp.models.MyQuerySet[myapp.models.MyModel]"
Expand Down