Skip to content

Commit 499e74c

Browse files
authored
Add wsgiref.types (Python 3.11+) (#7644)
_typeshed.wsgi: Import from wsgiref.types in Python 3.11+ Make types match wsgiref.types
1 parent b0611bc commit 499e74c

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed

stdlib/VERSIONS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ webbrowser: 2.7-
286286
winreg: 3.0-
287287
winsound: 2.7-
288288
wsgiref: 2.7-
289+
wsgiref.types: 3.11-
289290
xdrlib: 2.7-
290291
xml: 2.7-
291292
xmlrpc: 3.0-

stdlib/_typeshed/wsgi.pyi

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
# Types to support PEP 3333 (WSGI)
22
#
3+
# Obsolete since Python 3.11: Use wsgiref.types instead.
4+
#
35
# See the README.md file in this directory for more information.
46

7+
import sys
58
from sys import _OptExcInfo
69
from typing import Any, Callable, Iterable, Protocol
710
from typing_extensions import TypeAlias
811

9-
# stable
10-
class StartResponse(Protocol):
11-
def __call__(
12-
self, status: str, headers: list[tuple[str, str]], exc_info: _OptExcInfo | None = ...
13-
) -> Callable[[bytes], Any]: ...
12+
class _Readable(Protocol):
13+
def read(self, size: int = ...) -> bytes: ...
14+
# Optional: def close(self) -> object: ...
1415

15-
WSGIEnvironment: TypeAlias = dict[str, Any] # stable
16-
WSGIApplication: TypeAlias = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] # stable
16+
if sys.version_info >= (3, 11):
17+
from wsgiref.types import *
18+
else:
19+
# stable
20+
class StartResponse(Protocol):
21+
def __call__(
22+
self, __status: str, __headers: list[tuple[str, str]], __exc_info: _OptExcInfo | None = ...
23+
) -> Callable[[bytes], object]: ...
1724

18-
# WSGI input streams per PEP 3333, stable
19-
class InputStream(Protocol):
20-
def read(self, size: int = ...) -> bytes: ...
21-
def readline(self, size: int = ...) -> bytes: ...
22-
def readlines(self, hint: int = ...) -> list[bytes]: ...
23-
def __iter__(self) -> Iterable[bytes]: ...
25+
WSGIEnvironment: TypeAlias = dict[str, Any] # stable
26+
WSGIApplication: TypeAlias = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] # stable
2427

25-
# WSGI error streams per PEP 3333, stable
26-
class ErrorStream(Protocol):
27-
def flush(self) -> None: ...
28-
def write(self, s: str) -> None: ...
29-
def writelines(self, seq: list[str]) -> None: ...
28+
# WSGI input streams per PEP 3333, stable
29+
class InputStream(Protocol):
30+
def read(self, __size: int = ...) -> bytes: ...
31+
def readline(self, __size: int = ...) -> bytes: ...
32+
def readlines(self, __hint: int = ...) -> list[bytes]: ...
33+
def __iter__(self) -> Iterable[bytes]: ...
3034

31-
class _Readable(Protocol):
32-
def read(self, size: int = ...) -> bytes: ...
35+
# WSGI error streams per PEP 3333, stable
36+
class ErrorStream(Protocol):
37+
def flush(self) -> object: ...
38+
def write(self, __s: str) -> object: ...
39+
def writelines(self, __seq: list[str]) -> object: ...
3340

34-
# Optional file wrapper in wsgi.file_wrapper
35-
class FileWrapper(Protocol):
36-
def __call__(self, file: _Readable, block_size: int = ...) -> Iterable[bytes]: ...
41+
# Optional file wrapper in wsgi.file_wrapper
42+
class FileWrapper(Protocol):
43+
def __call__(self, __file: _Readable, __block_size: int = ...) -> Iterable[bytes]: ...

stdlib/wsgiref/handlers.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from _typeshed.wsgi import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment
12
from abc import abstractmethod
23
from types import TracebackType
34
from typing import IO, Callable, MutableMapping
45
from typing_extensions import TypeAlias
56

67
from .headers import Headers
7-
from .types import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment
88
from .util import FileWrapper
99

1010
__all__ = ["BaseHandler", "SimpleHandler", "BaseCGIHandler", "CGIHandler", "IISCGIHandler", "read_environ"]

stdlib/wsgiref/simple_server.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
from _typeshed.wsgi import ErrorStream, StartResponse, WSGIApplication, WSGIEnvironment
12
from http.server import BaseHTTPRequestHandler, HTTPServer
23
from typing import TypeVar, overload
34

45
from .handlers import SimpleHandler
5-
from .types import ErrorStream, StartResponse, WSGIApplication, WSGIEnvironment
66

77
__all__ = ["WSGIServer", "WSGIRequestHandler", "demo_app", "make_server"]
88

stdlib/wsgiref/types.pyi

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1-
# Obsolete, use _typeshed.wsgi directly.
1+
from collections.abc import Callable, Iterable
2+
from sys import _OptExcInfo
3+
from typing import Any, Protocol
4+
from typing_extensions import TypeAlias
25

3-
from _typeshed.wsgi import *
6+
__all__ = ["StartResponse", "WSGIEnvironment", "WSGIApplication", "InputStream", "ErrorStream", "FileWrapper"]
7+
8+
class StartResponse(Protocol):
9+
def __call__(
10+
self, __status: str, __headers: list[tuple[str, str]], __exc_info: _OptExcInfo | None = ...
11+
) -> Callable[[bytes], object]: ...
12+
13+
WSGIEnvironment: TypeAlias = dict[str, Any]
14+
WSGIApplication: TypeAlias = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]]
15+
16+
class InputStream(Protocol):
17+
def read(self, __size: int = ...) -> bytes: ...
18+
def readline(self, __size: int = ...) -> bytes: ...
19+
def readlines(self, __hint: int = ...) -> list[bytes]: ...
20+
def __iter__(self) -> Iterable[bytes]: ...
21+
22+
class ErrorStream(Protocol):
23+
def flush(self) -> object: ...
24+
def write(self, __s: str) -> object: ...
25+
def writelines(self, __seq: list[str]) -> object: ...
26+
27+
class _Readable(Protocol):
28+
def read(self, __size: int = ...) -> bytes: ...
29+
# Optional: def close(self) -> object: ...
30+
31+
class FileWrapper(Protocol):
32+
def __call__(self, __file: _Readable, __block_size: int = ...) -> Iterable[bytes]: ...

stdlib/wsgiref/util.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2+
from _typeshed.wsgi import WSGIEnvironment
23
from typing import IO, Any, Callable
34

4-
from .types import WSGIEnvironment
5-
65
__all__ = ["FileWrapper", "guess_scheme", "application_uri", "request_uri", "shift_path_info", "setup_testing_defaults"]
76

87
class FileWrapper:

tests/stubtest_allowlists/py3_common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ weakref.ref.__call__
240240
webbrowser.UnixBrowser.remote_action # always overridden in inheriting class
241241
webbrowser.UnixBrowser.remote_action_newtab # always overridden in inheriting class
242242
webbrowser.UnixBrowser.remote_action_newwin # always overridden in inheriting class
243-
wsgiref.types # Doesn't exist, see comments in file
244243
xml.parsers.expat.expat_CAPI
245244

246245
# ==========

0 commit comments

Comments
 (0)