From 37fc626ffd53f95d186974608ed6ee4c71e8f10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Fri, 13 Jan 2017 13:36:34 -0800 Subject: [PATCH] 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.) --- stdlib/2/wsgiref/types.pyi | 34 ++++++++++++++++++++++++++++++++++ stdlib/3/wsgiref/types.pyi | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 stdlib/2/wsgiref/types.pyi create mode 100644 stdlib/3/wsgiref/types.pyi diff --git a/stdlib/2/wsgiref/types.pyi b/stdlib/2/wsgiref/types.pyi new file mode 100644 index 000000000..7c28415bb --- /dev/null +++ b/stdlib/2/wsgiref/types.pyi @@ -0,0 +1,34 @@ +# Type declaration for a WSGI Function in Python 2 +# +# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type +# provided for type checking purposes. +# +# This means you cannot simply import wsgiref.types in your code. Instead, +# use the `TYPE_CHECKING` flag from the typing module: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref.types import WSGIApplication +# +# This import is now only taken into account by the type checker. Consequently, +# you need to use 'WSGIApplication' and not simply WSGIApplication when type +# hinting your code. Otherwise Python will raise NameErrors. + +from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type, Union +from types import TracebackType + +_exc_info = Tuple[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]] +_Text = Union[unicode, str] +WSGIApplication = Callable[ + [ + Dict[_Text, _Text], + Union[ + Callable[[_Text, List[Tuple[_Text, _Text]]], Callable[[_Text], None]], + Callable[[_Text, List[Tuple[_Text, _Text]], _exc_info], Callable[[_Text], None]], + ] + ], + Iterable[_Text] +] diff --git a/stdlib/3/wsgiref/types.pyi b/stdlib/3/wsgiref/types.pyi new file mode 100644 index 000000000..f9df8084c --- /dev/null +++ b/stdlib/3/wsgiref/types.pyi @@ -0,0 +1,33 @@ +# Type declaration for a WSGI Function in Python 3 +# +# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type +# provided for type checking purposes. +# +# This means you cannot simply import wsgiref.types in your code. Instead, +# use the `TYPE_CHECKING` flag from the typing module: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref.types import WSGIApplication +# +# This import is now only taken into account by the type checker. Consequently, +# you need to use 'WSGIApplication' and not simply WSGIApplication when type +# hinting your code. Otherwise Python will raise NameErrors. + +from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type, Union +from types import TracebackType + +_exc_info = Tuple[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]] +WSGIApplication = Callable[ + [ + Dict[str, str], + Union[ + Callable[[str, List[Tuple[str, str]]], Callable[[Union[bytes, str]], None]], + Callable[[str, List[Tuple[str, str]], _exc_info], Callable[[Union[bytes, str]], None]], + ] + ], + Iterable[Union[bytes, str]], +]