Skip to content

Commit 37fc626

Browse files
ambvgvanrossum
authored andcommitted
Add type signature for a WSGI Application to wsgiref (#825)
This type is something core to Python and is useful when typing web applications, but doesn't actually exist in the stdlib anywhere. I put this in wsgiref, but I am open to suggestions as for a better place. (Original PR by @rowillia.)
1 parent 41ba734 commit 37fc626

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

stdlib/2/wsgiref/types.pyi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Type declaration for a WSGI Function in Python 2
2+
#
3+
# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type
4+
# provided for type checking purposes.
5+
#
6+
# This means you cannot simply import wsgiref.types in your code. Instead,
7+
# use the `TYPE_CHECKING` flag from the typing module:
8+
#
9+
# from typing import TYPE_CHECKING
10+
#
11+
# if TYPE_CHECKING:
12+
# from wsgiref.types import WSGIApplication
13+
#
14+
# This import is now only taken into account by the type checker. Consequently,
15+
# you need to use 'WSGIApplication' and not simply WSGIApplication when type
16+
# hinting your code. Otherwise Python will raise NameErrors.
17+
18+
from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type, Union
19+
from types import TracebackType
20+
21+
_exc_info = Tuple[Optional[Type[BaseException]],
22+
Optional[BaseException],
23+
Optional[TracebackType]]
24+
_Text = Union[unicode, str]
25+
WSGIApplication = Callable[
26+
[
27+
Dict[_Text, _Text],
28+
Union[
29+
Callable[[_Text, List[Tuple[_Text, _Text]]], Callable[[_Text], None]],
30+
Callable[[_Text, List[Tuple[_Text, _Text]], _exc_info], Callable[[_Text], None]],
31+
]
32+
],
33+
Iterable[_Text]
34+
]

stdlib/3/wsgiref/types.pyi

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Type declaration for a WSGI Function in Python 3
2+
#
3+
# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type
4+
# provided for type checking purposes.
5+
#
6+
# This means you cannot simply import wsgiref.types in your code. Instead,
7+
# use the `TYPE_CHECKING` flag from the typing module:
8+
#
9+
# from typing import TYPE_CHECKING
10+
#
11+
# if TYPE_CHECKING:
12+
# from wsgiref.types import WSGIApplication
13+
#
14+
# This import is now only taken into account by the type checker. Consequently,
15+
# you need to use 'WSGIApplication' and not simply WSGIApplication when type
16+
# hinting your code. Otherwise Python will raise NameErrors.
17+
18+
from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type, Union
19+
from types import TracebackType
20+
21+
_exc_info = Tuple[Optional[Type[BaseException]],
22+
Optional[BaseException],
23+
Optional[TracebackType]]
24+
WSGIApplication = Callable[
25+
[
26+
Dict[str, str],
27+
Union[
28+
Callable[[str, List[Tuple[str, str]]], Callable[[Union[bytes, str]], None]],
29+
Callable[[str, List[Tuple[str, str]], _exc_info], Callable[[Union[bytes, str]], None]],
30+
]
31+
],
32+
Iterable[Union[bytes, str]],
33+
]

0 commit comments

Comments
 (0)