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

Make WSGIHandler compatible with stdlib wsgi definition #1639

Merged
merged 2 commits into from
Aug 4, 2023
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
12 changes: 9 additions & 3 deletions django-stubs/contrib/staticfiles/handlers.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Awaitable, Callable, Mapping, Sequence
import sys
from collections.abc import Awaitable, Callable, Mapping
from typing import Any
from urllib.parse import ParseResult

Expand All @@ -8,6 +9,11 @@ from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpRequest
from django.http.response import FileResponse, HttpResponseBase

if sys.version_info >= (3, 11):
from wsgiref.types import StartResponse, WSGIEnvironment
else:
from _typeshed.wsgi import StartResponse, WSGIEnvironment

class StaticFilesHandlerMixin:
handles_files: bool
application: BaseHandler
Expand All @@ -26,8 +32,8 @@ class StaticFilesHandler(StaticFilesHandlerMixin, WSGIHandler): # type: ignore
def __init__(self, application: WSGIHandler) -> None: ...
def __call__(
self,
environ: dict[str, Any],
start_response: Callable[[str, Sequence[tuple[str, str]]], None],
environ: WSGIEnvironment,
start_response: StartResponse,
) -> HttpResponseBase: ...

class ASGIStaticFilesHandler(StaticFilesHandlerMixin, ASGIHandler): # type: ignore
Expand Down
24 changes: 13 additions & 11 deletions django-stubs/core/handlers/wsgi.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from collections.abc import Callable, Sequence
import sys
from io import BytesIO
from typing import Any

from django.contrib.sessions.backends.base import SessionBase
from django.core.handlers import base
from django.http import HttpRequest
from django.http.response import HttpResponseBase
from typing_extensions import TypeAlias

_WSGIEnviron: TypeAlias = dict[str, Any]
if sys.version_info >= (3, 11):
from wsgiref.types import StartResponse, WSGIEnvironment
else:
from _typeshed.wsgi import StartResponse, WSGIEnvironment

class LimitedStream:
stream: BytesIO
Expand All @@ -20,21 +22,21 @@ class LimitedStream:
def readline(self, size: int | None = ...) -> bytes: ...

class WSGIRequest(HttpRequest):
environ: _WSGIEnviron
environ: WSGIEnvironment
session: SessionBase
encoding: Any
def __init__(self, environ: _WSGIEnviron) -> None: ...
def __init__(self, environ: WSGIEnvironment) -> None: ...

class WSGIHandler(base.BaseHandler):
request_class: type[WSGIRequest]
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
def __call__(
self,
environ: _WSGIEnviron,
start_response: Callable[[str, Sequence[tuple[str, str]]], None],
environ: WSGIEnvironment,
start_response: StartResponse,
) -> HttpResponseBase: ...

def get_path_info(environ: _WSGIEnviron) -> str: ...
def get_script_name(environ: _WSGIEnviron) -> str: ...
def get_bytes_from_wsgi(environ: _WSGIEnviron, key: str, default: str) -> bytes: ...
def get_str_from_wsgi(environ: _WSGIEnviron, key: str, default: str) -> str: ...
def get_path_info(environ: WSGIEnvironment) -> str: ...
def get_script_name(environ: WSGIEnvironment) -> str: ...
def get_bytes_from_wsgi(environ: WSGIEnvironment, key: str, default: str) -> bytes: ...
def get_str_from_wsgi(environ: WSGIEnvironment, key: str, default: str) -> str: ...
11 changes: 11 additions & 0 deletions tests/typecheck/core/handlers/test_wsgi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- case: test_make_server
main: |
from wsgiref.simple_server import make_server

from django.core.handlers.wsgi import WSGIHandler

make_server(
"0.0.0.0",
8080,
WSGIHandler(),
)