Add type signature for a WSGI Application to wsgiref.#725
Add type signature for a WSGI Application to wsgiref.#725rowillia wants to merge 3 commits intopython:masterfrom
Conversation
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.
ce54f29 to
81431d2
Compare
ambv
left a comment
There was a problem hiding this comment.
I'm undecided here. At the same time, I see the need for specifying the application's (callable's) signature, and at the same time a more kosher place for it would be wsgiref/simple_server.pyi.
stdlib/2/wsgiref/__init__.pyi
Outdated
| @@ -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 | |||
There was a problem hiding this comment.
I think you mean something along the lines of "WSGIFunction doesn't exist in wsgiref/init.py, it's a type provided for type checking purposes."
stdlib/2/wsgiref/__init__.pyi
Outdated
| exc_info = Tuple[Optional[Type[BaseException]], | ||
| Optional[BaseException], | ||
| Optional[TracebackType]] | ||
| WSGIFunction = Callable[[Dict[Union[unicode, str], Union[unicode, str]], |
There was a problem hiding this comment.
Wouldn't typing.AnyStr work better here?
There was a problem hiding this comment.
@ambv no, AnyStr is a TypeVar and I don't want this function to be generic with respect to a single string type - https://github.com/python/typing/blob/master/python2/typing.py#L560 . It would be perfectly valid to have something like
environ = {
u'hello': 'world'
}
result = app(environ, ....
stdlib/3/wsgiref/__init__.pyi
Outdated
| @@ -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 | |||
stdlib/2/wsgiref/__init__.pyi
Outdated
| exc_info = Tuple[Optional[Type[BaseException]], | ||
| Optional[BaseException], | ||
| Optional[TracebackType]] | ||
| WSGIFunction = Callable[[Dict[Union[unicode, str], Union[unicode, str]], |
There was a problem hiding this comment.
Also, this should rather be called WSGICallable as it can very well be a class, as documented in the comment in this example:
https://docs.python.org/3/library/wsgiref.html#examples
Or maybe WSGIApplication which it's called in most frameworks?
|
@ambv Fixed up the names. I'd somewhat prefer to leave it under if TYPE_CHECKING:
from wsgiref.simple_server import WSGIApplicationFeels less authoritative than: if TYPE_CHECKING:
from wsgiref import WSGIApplication |
|
Ssssh, I'm just trying to talk you into providing stubs for more files ;-) |
|
@gvanrossum This looks reasonable to me, do you have an opinion? |
|
I'm not against having this alias in the wsgiref stubs per se, but:
|
|
@gvanrossum moved things around. LMK what you think! |
gvanrossum
left a comment
There was a problem hiding this comment.
Just various editorial nits. Also, do you have real-world code that uses this yet? It would be nice if you tested with that, to make sure that it's actually reasonable to write a function that conforms to the signature.
| # from typing import TYPE_CHECKING | ||
| # | ||
| # if TYPE_CHECKING: | ||
| # from wsgiref import WSGIFunction |
There was a problem hiding this comment.
Change to from wsgiref.types import ...
| # 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 |
| @@ -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 | |||
| # | ||
| # if TYPE_CHECKING: | ||
| # from wsgiref import WSGIFunction | ||
| # |
There was a problem hiding this comment.
In the PY3 version (but not in the PY2 version) you should also tell users to use "forward reference" notation when using this (except in # type: comments). An example would be good.
| 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]] |
There was a problem hiding this comment.
This is pretty unreadable. Maybe you can clear it up a bit using some more type aliases? (An added benefit might be that there are fewer differences between PY2 and PY3.)
|
@rowillia Do you have time to update this? (I couldn't get GitHub to let me edit the files else I would have done it myself. :-) |
|
@gvanrossum Looking into it now, thanks for the feedback. |
63ba0ab to
7853c26
Compare
|
@rowillia Ping? Do you have any time to update? Or should I just close this? |
|
Superseded by #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.