Skip to content

Commit

Permalink
make WSGIHandler compatible with stdlib wsgi definition (#1639)
Browse files Browse the repository at this point in the history
Co-authored-by: Marti Raudsepp <[email protected]>
  • Loading branch information
asottile and intgr authored Aug 4, 2023
1 parent 45bfe05 commit e2dc4ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
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(),
)

0 comments on commit e2dc4ea

Please sign in to comment.