From e2dc4eaebcd790a1f33e4ef56de32eeb55568a26 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 4 Aug 2023 02:16:48 -0400 Subject: [PATCH] make WSGIHandler compatible with stdlib wsgi definition (#1639) Co-authored-by: Marti Raudsepp --- django-stubs/contrib/staticfiles/handlers.pyi | 12 +++++++--- django-stubs/core/handlers/wsgi.pyi | 24 ++++++++++--------- tests/typecheck/core/handlers/test_wsgi.yml | 11 +++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 tests/typecheck/core/handlers/test_wsgi.yml diff --git a/django-stubs/contrib/staticfiles/handlers.pyi b/django-stubs/contrib/staticfiles/handlers.pyi index d20e7f003..3b3e4c6a3 100644 --- a/django-stubs/contrib/staticfiles/handlers.pyi +++ b/django-stubs/contrib/staticfiles/handlers.pyi @@ -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 @@ -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 @@ -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 diff --git a/django-stubs/core/handlers/wsgi.pyi b/django-stubs/core/handlers/wsgi.pyi index 0abf98ba9..9fd928b09 100644 --- a/django-stubs/core/handlers/wsgi.pyi +++ b/django-stubs/core/handlers/wsgi.pyi @@ -1,4 +1,4 @@ -from collections.abc import Callable, Sequence +import sys from io import BytesIO from typing import Any @@ -6,9 +6,11 @@ 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 @@ -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: ... diff --git a/tests/typecheck/core/handlers/test_wsgi.yml b/tests/typecheck/core/handlers/test_wsgi.yml new file mode 100644 index 000000000..bf532ba94 --- /dev/null +++ b/tests/typecheck/core/handlers/test_wsgi.yml @@ -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(), + )