From 86e1e5d05e43a304247a7bfa9b0fa82431ef26a7 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Wed, 30 Nov 2016 10:48:13 -0800 Subject: [PATCH 1/6] Add type signature for a WSGI Application to wsgiref. 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. --- stdlib/2/wsgiref/__init__.pyi | 26 ++++++++++++++++++++++++++ stdlib/3/wsgiref/__init__.pyi | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/stdlib/2/wsgiref/__init__.pyi b/stdlib/2/wsgiref/__init__.pyi index e69de29bb2d1..aa6a8fb92d5f 100644 --- a/stdlib/2/wsgiref/__init__.pyi +++ b/stdlib/2/wsgiref/__init__.pyi @@ -0,0 +1,26 @@ +# Type declaration for a WSGI Function in Python 2 +# +# This function actually exist, but we need a central place to define the type +# of a WSGI Application. +# +# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in +# typing: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref import WSGIFunction +# + +from typing import Callable, Iterable, List, Optional, Tuple, Type, Union +from types import TracebackType + +exc_info = Tuple[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]] +WSGIFunction = Callable[[Dict[Union[unicode, str], Union[unicode, str]], + Union[ + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], exc_info], Callable[[Union[unicode, str]], None]] + ]], + Iterable[Union[unicode, str]]] diff --git a/stdlib/3/wsgiref/__init__.pyi b/stdlib/3/wsgiref/__init__.pyi index e69de29bb2d1..0127d00a7fa2 100644 --- a/stdlib/3/wsgiref/__init__.pyi +++ b/stdlib/3/wsgiref/__init__.pyi @@ -0,0 +1,26 @@ +# Type declaration for a WSGI Function in Python 3 +# +# This function actually exist, but we need a central place to define the type +# of a WSGI Application. +# +# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in +# typing: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref import WSGIFunction +# + +from typing import Callable, Iterable, List, Optional, Tuple, Type, Union +from types import TracebackType + +exc_info = Tuple[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]] +WSGIFunction = 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]]] From b2a2d0f26475474c186a77fc67e1beb2afc53b77 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Wed, 30 Nov 2016 12:03:14 -0800 Subject: [PATCH 2/6] s/WSGIFunction/WSGIApplication and fix up comments --- stdlib/2/wsgiref/__init__.pyi | 16 ++++++++-------- stdlib/3/wsgiref/__init__.pyi | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/stdlib/2/wsgiref/__init__.pyi b/stdlib/2/wsgiref/__init__.pyi index aa6a8fb92d5f..c6e1255ba144 100644 --- a/stdlib/2/wsgiref/__init__.pyi +++ b/stdlib/2/wsgiref/__init__.pyi @@ -1,7 +1,7 @@ # Type declaration for a WSGI Function in Python 2 # -# This function actually exist, but we need a central place to define the type -# of a WSGI Application. +# WSGIApplication doesn't exist in wsgiref/__init__.py, it's a type provided for +# type checking purposes. # # To correctly use this type stub, utilize the `TYPE_CHECKING` flag in # typing: @@ -18,9 +18,9 @@ from types import TracebackType exc_info = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] -WSGIFunction = Callable[[Dict[Union[unicode, str], Union[unicode, str]], - Union[ - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], exc_info], Callable[[Union[unicode, str]], None]] - ]], - Iterable[Union[unicode, str]]] +WSGIApplication = Callable[[Dict[Union[unicode, str], Union[unicode, str]], + Union[ + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], exc_info], Callable[[Union[unicode, str]], None]] + ]], + Iterable[Union[unicode, str]]] diff --git a/stdlib/3/wsgiref/__init__.pyi b/stdlib/3/wsgiref/__init__.pyi index 0127d00a7fa2..c06eea73c9ec 100644 --- a/stdlib/3/wsgiref/__init__.pyi +++ b/stdlib/3/wsgiref/__init__.pyi @@ -1,7 +1,7 @@ # Type declaration for a WSGI Function in Python 3 # -# This function actually exist, but we need a central place to define the type -# of a WSGI Application. +# WSGIApplication doesn't exist in wsgiref/__init__.py, it's a type provided for +# type checking purposes. # # To correctly use this type stub, utilize the `TYPE_CHECKING` flag in # typing: @@ -18,9 +18,9 @@ from types import TracebackType exc_info = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]] -WSGIFunction = 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]]] +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]]] From 510063cd3fe177edd1844a3b2940c524c05f1e91 Mon Sep 17 00:00:00 2001 From: Roy Williams Date: Wed, 30 Nov 2016 17:30:50 -0800 Subject: [PATCH 3/6] Move WSGIAppliation to wsgiref.types --- stdlib/2/wsgiref/__init__.pyi | 26 -------------------------- stdlib/2/wsgiref/types.pyi | 26 ++++++++++++++++++++++++++ stdlib/3/wsgiref/__init__.pyi | 26 -------------------------- stdlib/3/wsgiref/types.pyi | 26 ++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 stdlib/2/wsgiref/types.pyi create mode 100644 stdlib/3/wsgiref/types.pyi diff --git a/stdlib/2/wsgiref/__init__.pyi b/stdlib/2/wsgiref/__init__.pyi index c6e1255ba144..e69de29bb2d1 100644 --- a/stdlib/2/wsgiref/__init__.pyi +++ b/stdlib/2/wsgiref/__init__.pyi @@ -1,26 +0,0 @@ -# Type declaration for a WSGI Function in Python 2 -# -# WSGIApplication doesn't exist in wsgiref/__init__.py, it's a type provided for -# type checking purposes. -# -# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in -# typing: -# -# from typing import TYPE_CHECKING -# -# if TYPE_CHECKING: -# from wsgiref import WSGIFunction -# - -from typing import Callable, Iterable, List, Optional, Tuple, Type, Union -from types import TracebackType - -exc_info = Tuple[Optional[Type[BaseException]], - Optional[BaseException], - Optional[TracebackType]] -WSGIApplication = Callable[[Dict[Union[unicode, str], Union[unicode, str]], - Union[ - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], exc_info], Callable[[Union[unicode, str]], None]] - ]], - Iterable[Union[unicode, str]]] diff --git a/stdlib/2/wsgiref/types.pyi b/stdlib/2/wsgiref/types.pyi new file mode 100644 index 000000000000..99a7e73e9c80 --- /dev/null +++ b/stdlib/2/wsgiref/types.pyi @@ -0,0 +1,26 @@ +# Type declaration for a WSGI Function in Python 2 +# +# wsgiref/typing.py doesn't exist and neither does WSGIApplication, it's a type +# provided for type checking purposes. +# +# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in +# typing: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref import WSGIFunction +# + +from typing import Callable, Iterable, List, Optional, Tuple, Type, Union +from types import TracebackType + +_exc_info = Tuple[Optional[Type[BaseException]], + Optional[BaseException], + Optional[TracebackType]] +WSGIApplication = Callable[[Dict[Union[unicode, str], Union[unicode, str]], + Union[ + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], + Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], _exc_info], Callable[[Union[unicode, str]], None]] + ]], + Iterable[Union[unicode, str]]] diff --git a/stdlib/3/wsgiref/__init__.pyi b/stdlib/3/wsgiref/__init__.pyi index c06eea73c9ec..e69de29bb2d1 100644 --- a/stdlib/3/wsgiref/__init__.pyi +++ b/stdlib/3/wsgiref/__init__.pyi @@ -1,26 +0,0 @@ -# Type declaration for a WSGI Function in Python 3 -# -# WSGIApplication doesn't exist in wsgiref/__init__.py, it's a type provided for -# type checking purposes. -# -# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in -# typing: -# -# from typing import TYPE_CHECKING -# -# if TYPE_CHECKING: -# from wsgiref import WSGIFunction -# - -from typing import Callable, 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]]] diff --git a/stdlib/3/wsgiref/types.pyi b/stdlib/3/wsgiref/types.pyi new file mode 100644 index 000000000000..8bbc33087ed0 --- /dev/null +++ b/stdlib/3/wsgiref/types.pyi @@ -0,0 +1,26 @@ +# Type declaration for a WSGI Function in Python 3 +# +# wsgiref/typing.py doesn't exist and neither does WSGIApplication, it's a type +# provided for type checking purposes. +# +# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in +# typing: +# +# from typing import TYPE_CHECKING +# +# if TYPE_CHECKING: +# from wsgiref import WSGIFunction +# + +from typing import Callable, 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]]] From 53cf3c7fc42516fbffeca3eb1067a6d97611c495 Mon Sep 17 00:00:00 2001 From: Lukasz Langa Date: Wed, 11 Jan 2017 22:44:46 -0800 Subject: [PATCH 4/6] Updates after review by @gvanrossum --- stdlib/2/wsgiref/types.pyi | 34 +++++++++++++++++++++------------- stdlib/3/wsgiref/types.pyi | 33 ++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/stdlib/2/wsgiref/types.pyi b/stdlib/2/wsgiref/types.pyi index 99a7e73e9c80..b7bd533f5496 100644 --- a/stdlib/2/wsgiref/types.pyi +++ b/stdlib/2/wsgiref/types.pyi @@ -1,26 +1,34 @@ # Type declaration for a WSGI Function in Python 2 # -# wsgiref/typing.py doesn't exist and neither does WSGIApplication, it's a type +# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type # provided for type checking purposes. # -# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in -# typing: +# 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 +# from typing import TYPE_CHECKING # -# if TYPE_CHECKING: -# from wsgiref import WSGIFunction +# 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, Iterable, List, Optional, Tuple, Type, Union +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[Union[unicode, str], Union[unicode, str]], - Union[ - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]]], Callable[[Union[unicode, str]], None]], - Callable[[Union[unicode, str], List[Tuple[Union[unicode, str], Union[unicode, str]]], _exc_info], Callable[[Union[unicode, str]], None]] - ]], - Iterable[Union[unicode, str]]] +_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 index 8bbc33087ed0..f6acc2e691f0 100644 --- a/stdlib/3/wsgiref/types.pyi +++ b/stdlib/3/wsgiref/types.pyi @@ -1,26 +1,33 @@ # Type declaration for a WSGI Function in Python 3 # -# wsgiref/typing.py doesn't exist and neither does WSGIApplication, it's a type +# wsgiref/types.py doesn't exist and neither does WSGIApplication, it's a type # provided for type checking purposes. # -# To correctly use this type stub, utilize the `TYPE_CHECKING` flag in -# typing: +# 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 +# from typing import TYPE_CHECKING # -# if TYPE_CHECKING: -# from wsgiref import WSGIFunction +# 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, Iterable, List, Optional, Tuple, Type, Union +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]]] +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]], +] From 75d8f3a7ef0041a8230606321a383eeb550d2297 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 13 Jan 2017 13:32:02 -0800 Subject: [PATCH 5/6] Whitespace/trailing comma nits. --- stdlib/2/wsgiref/types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/2/wsgiref/types.pyi b/stdlib/2/wsgiref/types.pyi index b7bd533f5496..7c28415bb348 100644 --- a/stdlib/2/wsgiref/types.pyi +++ b/stdlib/2/wsgiref/types.pyi @@ -27,7 +27,7 @@ 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]] + Callable[[_Text, List[Tuple[_Text, _Text]], _exc_info], Callable[[_Text], None]], ] ], Iterable[_Text] From 927220dae8d8533476f0013ad1d37ade1bb37261 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 13 Jan 2017 13:35:28 -0800 Subject: [PATCH 6/6] Trailing comma --- stdlib/3/wsgiref/types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3/wsgiref/types.pyi b/stdlib/3/wsgiref/types.pyi index f6acc2e691f0..f9df8084cf79 100644 --- a/stdlib/3/wsgiref/types.pyi +++ b/stdlib/3/wsgiref/types.pyi @@ -26,7 +26,7 @@ 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]] + Callable[[str, List[Tuple[str, str]], _exc_info], Callable[[Union[bytes, str]], None]], ] ], Iterable[Union[bytes, str]],