Repository navigation
Typing: use wsgiref.types to validate types and fix issues uncovered - #2467
Conversation
- start_response() must return a write(bytes) function, even though this is now deprecated. It's fine to be a no-op here. - sys.exc_info() can return (None, None, None), so make sure to handle that case.
|
@tomchristie this one brings type completeness up to 98% 🥳 |
| import types | ||
| import typing | ||
|
|
||
| import typing_extensions as te |
There was a problem hiding this comment.
I can't see where this is coming from in the requirements.txt... is this built-in or a package?
Also, can we just import typing_extensions rather than import typing_extensions as te?
There was a problem hiding this comment.
I changed the Protocol to Callable and removed the import. The protocol is more correct, it seems like exc_info must have a default value which you can't describe with Callable. That said, I don't know if this will cause real world issues with WSGI frameworks or not, I think it would depend on how strict their typing is, I'm going to check and report back.
|
Ups looks like #2467 (comment) was too late. I'll still check and we can always follow up in a PR adding the dependency if needed. |
|
I tested Flask and Falcon by creating an app and passing it to from httpx import WSGITransport
from flask import Flask
from falcon import App
WSGITransport(app=Flask(__name__))
WSGITransport(app=App())No errors from mypy, even in |
| _T = typing.TypeVar("_T") | ||
| _ExcInfo = typing.Tuple[typing.Type[BaseException], BaseException, types.TracebackType] | ||
| _OptExcInfo = typing.Union[_ExcInfo, typing.Tuple[None, None, None]] | ||
|
|
||
| def _skip_leading_empty_chunks(body: typing.Iterable[bytes]) -> typing.Iterable[bytes]: | ||
|
|
||
| # backported wsgiref.types definitions from Python 3.11 | ||
| StartResponse = typing.Callable[ | ||
| [str, typing.List[typing.Tuple[str, str]], typing.Optional[_OptExcInfo]], | ||
| typing.Callable[[bytes], object], | ||
| ] | ||
|
|
||
|
|
||
| WSGIApplication = typing.Callable[ | ||
| [typing.Dict[str, typing.Any], StartResponse], typing.Iterable[bytes] | ||
| ] |
There was a problem hiding this comment.
Hmm shouldn't we just import these directly from typeshed instead?
E.g.
if typing.TYPE_CHECKING:
from _typeshed import OptExcInfo
from _typeshed.wsgi import WSGIApplicationThere was a problem hiding this comment.
Probably. With an else: WSGIApplication = Any or something. Maybe make a PR?
…ncode#2467) * Typing: use wsgiref.types to validate types and fix issues uncovered - start_response() must return a write(bytes) function, even though this is now deprecated. It's fine to be a no-op here. - sys.exc_info() can return (None, None, None), so make sure to handle that case. * remove typing_extensions Co-authored-by: Martijn Pieters <mj@zopatista.com>
pyright reports:
after.txt
before.txt