Skip to content
Myr

Typing: use wsgiref.types to validate types and fix issues uncovered (#2467)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Adrian Garcia Badaracco · committed by GitHub · parent 8327e13 · Browse files · 1 file+24 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Martijn Pieters <mj@zopatista.com>

Diff stats

1 file+24 added−6 removed

  • other:+24 added−6 removed

1 file+24 added−6 removed

modified:httpx/_transports/wsgi.py+24 added-6 removed
1import io2import itertools3import sys4import types5import typing67from .._models import Request, Response8from .._types import SyncByteStream9from .base import BaseTransport1011_T = typing.TypeVar("_T")12_ExcInfo = typing.Tuple[typing.Type[BaseException], BaseException, types.TracebackType]13_OptExcInfo = typing.Union[_ExcInfo, typing.Tuple[None, None, None]]141516# backported wsgiref.types definitions from Python 3.1117StartResponse = typing.Callable[18 [str, typing.List[typing.Tuple[str, str]], typing.Optional[_OptExcInfo]],19 typing.Callable[[bytes], object],20]212223WSGIApplication = typing.Callable[24 [typing.Dict[str, typing.Any], StartResponse], typing.Iterable[bytes]25]262711def _skip_leading_empty_chunks(body: typing.Iterable[bytes]) -> typing.Iterable[bytes]:28def _skip_leading_empty_chunks(body: typing.Iterable[_T]) -> typing.Iterable[_T]:29 body = iter(body)30 for chunk in body:31 if chunk:32 return itertools.chain([chunk], body)
37 unmodified lines
70 ```7172 Arguments:7357 * `app` - The ASGI application.74 * `app` - The WSGI application.75 * `raise_app_exceptions` - Boolean indicating if exceptions in the application76 should be raised. Default to `True`. Can be set to `False` for use cases77 such as testing the content of a client 500 response.78 * `script_name` - The root path on which the WSGI application should be mounted.
2 unmodified lines
81 """8283 def __init__(84 self,68 app: typing.Callable[..., typing.Any],85 app: WSGIApplication,86 raise_app_exceptions: bool = True,87 script_name: str = "",88 remote_addr: str = "127.0.0.1",89 wsgi_errors: typing.Optional[typing.TextIO] = None,
37 unmodified lines
127128 def start_response(129 status: str,130 response_headers: typing.List[typing.Tuple[str, str]],114 exc_info: typing.Any = None,115 ) -> None:131 exc_info: typing.Optional[_OptExcInfo] = None,132 ) -> typing.Callable[[bytes], typing.Any]:133 nonlocal seen_status, seen_response_headers, seen_exc_info134 seen_status = status135 seen_response_headers = response_headers136 seen_exc_info = exc_info137 return lambda _: None138139 result = self.app(environ, start_response)140141 stream = WSGIByteStream(result)142143 assert seen_status is not None144 assert seen_response_headers is not None127 if seen_exc_info and self.raise_app_exceptions:145 if seen_exc_info and seen_exc_info[0] and self.raise_app_exceptions:146 raise seen_exc_info[1]147148 status_code = int(seen_status.split()[0])149 headers = [
5 unmodified lines