From 14a1704be507cf435fa84da75c249c2ecd1c3981 Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Fri, 20 May 2022 11:12:25 +0100 Subject: [PATCH] Switch to explicit `typing.Optional` throughout (#2096) * Fix issue with Mypy `--strict` and `AsyncExitStack` * Enable `no_implicit_optional` in Mypy * Ignore internal type errors * Bump `httpcore` * Remove unneeded type: ignore comments Co-authored-by: Florimond Manca Co-authored-by: Michael Oliver --- httpx/_api.py | 148 +++++++-------- httpx/_client.py | 346 ++++++++++++++++++----------------- httpx/_config.py | 12 +- httpx/_content.py | 21 ++- httpx/_decoders.py | 4 +- httpx/_exceptions.py | 8 +- httpx/_models.py | 87 ++++++--- httpx/_multipart.py | 4 +- httpx/_transports/base.py | 12 +- httpx/_transports/default.py | 28 +-- httpx/_types.py | 2 - httpx/_urls.py | 8 +- httpx/_utils.py | 4 +- setup.cfg | 2 + setup.py | 2 +- 15 files changed, 365 insertions(+), 323 deletions(-) diff --git a/httpx/_api.py b/httpx/_api.py index e206b1496c..571289cf2b 100644 --- a/httpx/_api.py +++ b/httpx/_api.py @@ -24,19 +24,19 @@ def request( method: str, url: URLTypes, *, - params: QueryParamTypes = None, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, follow_redirects: bool = False, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, trust_env: bool = True, ) -> Response: """ @@ -116,19 +116,19 @@ def stream( method: str, url: URLTypes, *, - params: QueryParamTypes = None, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, follow_redirects: bool = False, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, trust_env: bool = True, ) -> typing.Iterator[Response]: """ @@ -167,13 +167,13 @@ def stream( def get( url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -205,13 +205,13 @@ def get( def options( url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -243,13 +243,13 @@ def options( def head( url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -281,17 +281,17 @@ def head( def post( url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -324,17 +324,17 @@ def post( def put( url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -367,17 +367,17 @@ def put( def patch( url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, @@ -410,13 +410,13 @@ def patch( def delete( url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: AuthTypes = None, - proxies: ProxiesTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Optional[AuthTypes] = None, + proxies: typing.Optional[ProxiesTypes] = None, follow_redirects: bool = False, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, trust_env: bool = True, diff --git a/httpx/_client.py b/httpx/_client.py index ce7b92cc78..f324b19459 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -156,14 +156,16 @@ class BaseClient: def __init__( self, *, - auth: AuthTypes = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + auth: typing.Optional[AuthTypes] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, follow_redirects: bool = False, max_redirects: int = DEFAULT_MAX_REDIRECTS, - event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, + event_hooks: typing.Optional[ + typing.Mapping[str, typing.List[typing.Callable]] + ] = None, base_url: URLTypes = "", trust_env: bool = True, ): @@ -316,15 +318,15 @@ def build_request( method: str, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Request: """ Build and return a request instance. @@ -385,7 +387,7 @@ def _merge_url(self, url: URLTypes) -> URL: return merge_url def _merge_cookies( - self, cookies: CookieTypes = None + self, cookies: typing.Optional[CookieTypes] = None ) -> typing.Optional[CookieTypes]: """ Merge a cookies argument together with any cookies on the client, @@ -398,7 +400,7 @@ def _merge_cookies( return cookies def _merge_headers( - self, headers: HeaderTypes = None + self, headers: typing.Optional[HeaderTypes] = None ) -> typing.Optional[HeaderTypes]: """ Merge a headers argument together with any headers on the client, @@ -409,7 +411,7 @@ def _merge_headers( return merged_headers def _merge_queryparams( - self, params: QueryParamTypes = None + self, params: typing.Optional[QueryParamTypes] = None ) -> typing.Optional[QueryParamTypes]: """ Merge a queryparams argument together with any queryparams on the client, @@ -420,7 +422,7 @@ def _merge_queryparams( return merged_queryparams.merge(params) return params - def _build_auth(self, auth: AuthTypes) -> typing.Optional[Auth]: + def _build_auth(self, auth: typing.Optional[AuthTypes]) -> typing.Optional[Auth]: if auth is None: return None elif isinstance(auth, tuple): @@ -435,7 +437,7 @@ def _build_auth(self, auth: AuthTypes) -> typing.Optional[Auth]: def _build_request_auth( self, request: Request, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, ) -> Auth: auth = ( self._auth if isinstance(auth, UseClientDefault) else self._build_auth(auth) @@ -614,24 +616,26 @@ class Client(BaseClient): def __init__( self, *, - auth: AuthTypes = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + auth: typing.Optional[AuthTypes] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, - proxies: ProxiesTypes = None, - mounts: typing.Mapping[str, BaseTransport] = None, + proxies: typing.Optional[ProxiesTypes] = None, + mounts: typing.Optional[typing.Mapping[str, BaseTransport]] = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, follow_redirects: bool = False, limits: Limits = DEFAULT_LIMITS, max_redirects: int = DEFAULT_MAX_REDIRECTS, - event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, + event_hooks: typing.Optional[ + typing.Mapping[str, typing.List[typing.Callable]] + ] = None, base_url: URLTypes = "", - transport: BaseTransport = None, - app: typing.Callable = None, + transport: typing.Optional[BaseTransport] = None, + app: typing.Optional[typing.Callable] = None, trust_env: bool = True, ): super().__init__( @@ -693,12 +697,12 @@ def __init__( def _init_transport( self, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, - transport: BaseTransport = None, - app: typing.Callable = None, + transport: typing.Optional[BaseTransport] = None, + app: typing.Optional[typing.Callable] = None, trust_env: bool = True, ) -> BaseTransport: if transport is not None: @@ -720,7 +724,7 @@ def _init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, @@ -752,17 +756,17 @@ def request( method: str, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Build and send a request. @@ -809,17 +813,17 @@ def stream( method: str, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> typing.Iterator[Response]: """ Alternative to `httpx.request()` that streams the response body @@ -860,7 +864,7 @@ def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, ) -> Response: """ @@ -1011,13 +1015,13 @@ def get( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `GET` request. @@ -1040,13 +1044,13 @@ def options( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send an `OPTIONS` request. @@ -1069,13 +1073,13 @@ def head( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `HEAD` request. @@ -1098,17 +1102,17 @@ def post( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `POST` request. @@ -1135,17 +1139,17 @@ def put( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `PUT` request. @@ -1172,17 +1176,17 @@ def patch( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `PATCH` request. @@ -1209,13 +1213,13 @@ def delete( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `DELETE` request. @@ -1264,9 +1268,9 @@ def __enter__(self: T) -> T: def __exit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: self._state = ClientState.CLOSED @@ -1327,24 +1331,26 @@ class AsyncClient(BaseClient): def __init__( self, *, - auth: AuthTypes = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + auth: typing.Optional[AuthTypes] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, - proxies: ProxiesTypes = None, - mounts: typing.Mapping[str, AsyncBaseTransport] = None, + proxies: typing.Optional[ProxiesTypes] = None, + mounts: typing.Optional[typing.Mapping[str, AsyncBaseTransport]] = None, timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, follow_redirects: bool = False, limits: Limits = DEFAULT_LIMITS, max_redirects: int = DEFAULT_MAX_REDIRECTS, - event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, + event_hooks: typing.Optional[ + typing.Mapping[str, typing.List[typing.Callable]] + ] = None, base_url: URLTypes = "", - transport: AsyncBaseTransport = None, - app: typing.Callable = None, + transport: typing.Optional[AsyncBaseTransport] = None, + app: typing.Optional[typing.Callable] = None, trust_env: bool = True, ): super().__init__( @@ -1406,12 +1412,12 @@ def __init__( def _init_transport( self, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, - transport: AsyncBaseTransport = None, - app: typing.Callable = None, + transport: typing.Optional[AsyncBaseTransport] = None, + app: typing.Optional[typing.Callable] = None, trust_env: bool = True, ) -> AsyncBaseTransport: if transport is not None: @@ -1433,7 +1439,7 @@ def _init_proxy_transport( self, proxy: Proxy, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, @@ -1464,17 +1470,17 @@ async def request( method: str, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Build and send a request. @@ -1513,17 +1519,17 @@ async def stream( method: str, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> typing.AsyncIterator[Response]: """ Alternative to `httpx.request()` that streams the response body @@ -1564,7 +1570,7 @@ async def send( request: Request, *, stream: bool = False, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, ) -> Response: """ @@ -1715,13 +1721,13 @@ async def get( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + auth: typing.Union[AuthTypes, UseClientDefault, None] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `GET` request. @@ -1744,13 +1750,13 @@ async def options( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send an `OPTIONS` request. @@ -1773,13 +1779,13 @@ async def head( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `HEAD` request. @@ -1802,17 +1808,17 @@ async def post( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `POST` request. @@ -1839,17 +1845,17 @@ async def put( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `PUT` request. @@ -1876,17 +1882,17 @@ async def patch( self, url: URLTypes, *, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `PATCH` request. @@ -1913,13 +1919,13 @@ async def delete( self, url: URLTypes, *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, auth: typing.Union[AuthTypes, UseClientDefault] = USE_CLIENT_DEFAULT, follow_redirects: typing.Union[bool, UseClientDefault] = USE_CLIENT_DEFAULT, timeout: typing.Union[TimeoutTypes, UseClientDefault] = USE_CLIENT_DEFAULT, - extensions: dict = None, + extensions: typing.Optional[dict] = None, ) -> Response: """ Send a `DELETE` request. @@ -1968,9 +1974,9 @@ async def __aenter__(self: U) -> U: async def __aexit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: self._state = ClientState.CLOSED diff --git a/httpx/_config.py b/httpx/_config.py index ca07ea45b5..d164e4c30c 100644 --- a/httpx/_config.py +++ b/httpx/_config.py @@ -41,7 +41,7 @@ class UnsetType: def create_ssl_context( - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, trust_env: bool = True, http2: bool = False, @@ -61,7 +61,7 @@ class SSLConfig: def __init__( self, *, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, verify: VerifyTypes = True, trust_env: bool = True, http2: bool = False, @@ -290,8 +290,8 @@ class Limits: def __init__( self, *, - max_connections: int = None, - max_keepalive_connections: int = None, + max_connections: typing.Optional[int] = None, + max_keepalive_connections: typing.Optional[int] = None, keepalive_expiry: typing.Optional[float] = 5.0, ): self.max_connections = max_connections @@ -320,8 +320,8 @@ def __init__( self, url: URLTypes, *, - auth: typing.Tuple[str, str] = None, - headers: HeaderTypes = None, + auth: typing.Optional[typing.Tuple[str, str]] = None, + headers: typing.Optional[HeaderTypes] = None, ): url = URL(url) headers = Headers(headers) diff --git a/httpx/_content.py b/httpx/_content.py index 4871a8de2d..24a967d506 100644 --- a/httpx/_content.py +++ b/httpx/_content.py @@ -8,6 +8,7 @@ Dict, Iterable, Iterator, + Optional, Tuple, Union, ) @@ -149,7 +150,7 @@ def encode_urlencoded_data( def encode_multipart_data( - data: dict, files: RequestFiles, boundary: bytes = None + data: dict, files: RequestFiles, boundary: Optional[bytes] = None ) -> Tuple[Dict[str, str], MultipartStream]: multipart = MultipartStream(data=data, files=files, boundary=boundary) headers = multipart.get_headers() @@ -181,11 +182,11 @@ def encode_json(json: Any) -> Tuple[Dict[str, str], ByteStream]: def encode_request( - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: Any = None, - boundary: bytes = None, + content: Optional[RequestContent] = None, + data: Optional[RequestData] = None, + files: Optional[RequestFiles] = None, + json: Optional[Any] = None, + boundary: Optional[bytes] = None, ) -> Tuple[Dict[str, str], Union[SyncByteStream, AsyncByteStream]]: """ Handles encoding the given `content`, `data`, `files`, and `json`, @@ -216,10 +217,10 @@ def encode_request( def encode_response( - content: ResponseContent = None, - text: str = None, - html: str = None, - json: Any = None, + content: Optional[ResponseContent] = None, + text: Optional[str] = None, + html: Optional[str] = None, + json: Optional[Any] = None, ) -> Tuple[Dict[str, str], Union[SyncByteStream, AsyncByteStream]]: """ Handles encoding the given `content`, returning a two-tuple of diff --git a/httpx/_decoders.py b/httpx/_decoders.py index 2d638b7d99..69c03697db 100644 --- a/httpx/_decoders.py +++ b/httpx/_decoders.py @@ -166,7 +166,7 @@ class ByteChunker: Handles returning byte content in fixed-size chunks. """ - def __init__(self, chunk_size: int = None) -> None: + def __init__(self, chunk_size: typing.Optional[int] = None) -> None: self._buffer = io.BytesIO() self._chunk_size = chunk_size @@ -205,7 +205,7 @@ class TextChunker: Handles returning text content in fixed-size chunks. """ - def __init__(self, chunk_size: int = None) -> None: + def __init__(self, chunk_size: typing.Optional[int] = None) -> None: self._buffer = io.StringIO() self._chunk_size = chunk_size diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py index f6e6a19809..1941250b16 100644 --- a/httpx/_exceptions.py +++ b/httpx/_exceptions.py @@ -75,7 +75,9 @@ class RequestError(HTTPError): Base class for all exceptions that may occur when issuing a `.request()`. """ - def __init__(self, message: str, *, request: "Request" = None) -> None: + def __init__( + self, message: str, *, request: typing.Optional["Request"] = None + ) -> None: super().__init__(message) # At the point an exception is raised we won't typically have a request # instance to associate it with. @@ -326,7 +328,9 @@ def __init__(self) -> None: @contextlib.contextmanager -def request_context(request: "Request" = None) -> typing.Iterator[None]: +def request_context( + request: typing.Optional["Request"] = None, +) -> typing.Iterator[None]: """ A context manager that can be used to attach the given request context to any `RequestError` exceptions that are raised within the block. diff --git a/httpx/_models.py b/httpx/_models.py index 5a213c3564..4f82f01473 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -58,7 +58,11 @@ class Headers(typing.MutableMapping[str, str]): HTTP headers, as a case-insensitive multi-dict. """ - def __init__(self, headers: HeaderTypes = None, encoding: str = None) -> None: + def __init__( + self, + headers: typing.Optional[HeaderTypes] = None, + encoding: typing.Optional[str] = None, + ) -> None: if headers is None: self._list = [] # type: typing.List[typing.Tuple[bytes, bytes, bytes]] elif isinstance(headers, Headers): @@ -192,7 +196,7 @@ def get_list(self, key: str, split_commas: bool = False) -> typing.List[str]: split_values.extend([item.strip() for item in value.split(",")]) return split_values - def update(self, headers: HeaderTypes = None) -> None: # type: ignore + def update(self, headers: typing.Optional[HeaderTypes] = None) -> None: # type: ignore headers = Headers(headers) for key in headers.keys(): if key in self: @@ -306,15 +310,15 @@ def __init__( method: typing.Union[str, bytes], url: typing.Union["URL", str, RawURL], *, - params: QueryParamTypes = None, - headers: HeaderTypes = None, - cookies: CookieTypes = None, - content: RequestContent = None, - data: RequestData = None, - files: RequestFiles = None, - json: typing.Any = None, - stream: typing.Union[SyncByteStream, AsyncByteStream] = None, - extensions: dict = None, + params: typing.Optional[QueryParamTypes] = None, + headers: typing.Optional[HeaderTypes] = None, + cookies: typing.Optional[CookieTypes] = None, + content: typing.Optional[RequestContent] = None, + data: typing.Optional[RequestData] = None, + files: typing.Optional[RequestFiles] = None, + json: typing.Optional[typing.Any] = None, + stream: typing.Union[SyncByteStream, AsyncByteStream, None] = None, + extensions: typing.Optional[dict] = None, ): self.method = ( method.decode("ascii").upper() @@ -432,15 +436,15 @@ def __init__( self, status_code: int, *, - headers: HeaderTypes = None, - content: ResponseContent = None, - text: str = None, - html: str = None, + headers: typing.Optional[HeaderTypes] = None, + content: typing.Optional[ResponseContent] = None, + text: typing.Optional[str] = None, + html: typing.Optional[str] = None, json: typing.Any = None, - stream: typing.Union[SyncByteStream, AsyncByteStream] = None, - request: Request = None, - extensions: dict = None, - history: typing.List["Response"] = None, + stream: typing.Union[SyncByteStream, AsyncByteStream, None] = None, + request: typing.Optional[Request] = None, + extensions: typing.Optional[dict] = None, + history: typing.Optional[typing.List["Response"]] = None, ): self.status_code = status_code self.headers = Headers(headers) @@ -796,7 +800,9 @@ def read(self) -> bytes: self._content = b"".join(self.iter_bytes()) return self._content - def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]: + def iter_bytes( + self, chunk_size: typing.Optional[int] = None + ) -> typing.Iterator[bytes]: """ A byte-iterator over the decoded response content. This allows us to handle gzip, deflate, and brotli encoded responses. @@ -819,7 +825,9 @@ def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]: for chunk in chunker.flush(): yield chunk - def iter_text(self, chunk_size: int = None) -> typing.Iterator[str]: + def iter_text( + self, chunk_size: typing.Optional[int] = None + ) -> typing.Iterator[str]: """ A str-iterator over the decoded response content that handles both gzip, deflate, etc but also detects the content's @@ -847,7 +855,9 @@ def iter_lines(self) -> typing.Iterator[str]: for line in decoder.flush(): yield line - def iter_raw(self, chunk_size: int = None) -> typing.Iterator[bytes]: + def iter_raw( + self, chunk_size: typing.Optional[int] = None + ) -> typing.Iterator[bytes]: """ A byte-iterator over the raw response content. """ @@ -894,7 +904,9 @@ async def aread(self) -> bytes: self._content = b"".join([part async for part in self.aiter_bytes()]) return self._content - async def aiter_bytes(self, chunk_size: int = None) -> typing.AsyncIterator[bytes]: + async def aiter_bytes( + self, chunk_size: typing.Optional[int] = None + ) -> typing.AsyncIterator[bytes]: """ A byte-iterator over the decoded response content. This allows us to handle gzip, deflate, and brotli encoded responses. @@ -917,7 +929,9 @@ async def aiter_bytes(self, chunk_size: int = None) -> typing.AsyncIterator[byte for chunk in chunker.flush(): yield chunk - async def aiter_text(self, chunk_size: int = None) -> typing.AsyncIterator[str]: + async def aiter_text( + self, chunk_size: typing.Optional[int] = None + ) -> typing.AsyncIterator[str]: """ A str-iterator over the decoded response content that handles both gzip, deflate, etc but also detects the content's @@ -945,7 +959,9 @@ async def aiter_lines(self) -> typing.AsyncIterator[str]: for line in decoder.flush(): yield line - async def aiter_raw(self, chunk_size: int = None) -> typing.AsyncIterator[bytes]: + async def aiter_raw( + self, chunk_size: typing.Optional[int] = None + ) -> typing.AsyncIterator[bytes]: """ A byte-iterator over the raw response content. """ @@ -990,7 +1006,7 @@ class Cookies(MutableMapping): HTTP Cookies, as a mutable mapping. """ - def __init__(self, cookies: CookieTypes = None) -> None: + def __init__(self, cookies: typing.Optional[CookieTypes] = None) -> None: if cookies is None or isinstance(cookies, dict): self.jar = CookieJar() if isinstance(cookies, dict): @@ -1050,7 +1066,11 @@ def set(self, name: str, value: str, domain: str = "", path: str = "/") -> None: self.jar.set_cookie(cookie) def get( # type: ignore - self, name: str, default: str = None, domain: str = None, path: str = None + self, + name: str, + default: typing.Optional[str] = None, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, ) -> typing.Optional[str]: """ Get a cookie by name. May optionally include domain and path @@ -1070,7 +1090,12 @@ def get( # type: ignore return default return value - def delete(self, name: str, domain: str = None, path: str = None) -> None: + def delete( + self, + name: str, + domain: typing.Optional[str] = None, + path: typing.Optional[str] = None, + ) -> None: """ Delete a cookie by name. May optionally include domain and path in order to specify exactly which cookie to delete. @@ -1089,7 +1114,9 @@ def delete(self, name: str, domain: str = None, path: str = None) -> None: for cookie in remove: self.jar.clear(cookie.domain, cookie.path, cookie.name) - def clear(self, domain: str = None, path: str = None) -> None: + def clear( + self, domain: typing.Optional[str] = None, path: typing.Optional[str] = None + ) -> None: """ Delete all cookies. Optionally include a domain and path in order to only delete a subset of all the cookies. @@ -1102,7 +1129,7 @@ def clear(self, domain: str = None, path: str = None) -> None: args.append(path) self.jar.clear(*args) - def update(self, cookies: CookieTypes = None) -> None: # type: ignore + def update(self, cookies: typing.Optional[CookieTypes] = None) -> None: # type: ignore cookies = Cookies(cookies) for cookie in cookies.jar: self.jar.set_cookie(cookie) diff --git a/httpx/_multipart.py b/httpx/_multipart.py index 3f981c85d0..d42f5cb31b 100644 --- a/httpx/_multipart.py +++ b/httpx/_multipart.py @@ -175,7 +175,9 @@ class MultipartStream(SyncByteStream, AsyncByteStream): Request content as streaming multipart encoded form data. """ - def __init__(self, data: dict, files: RequestFiles, boundary: bytes = None) -> None: + def __init__( + self, data: dict, files: RequestFiles, boundary: typing.Optional[bytes] = None + ) -> None: if boundary is None: boundary = binascii.hexlify(os.urandom(16)) diff --git a/httpx/_transports/base.py b/httpx/_transports/base.py index e5e3283c16..ffba6a41c2 100644 --- a/httpx/_transports/base.py +++ b/httpx/_transports/base.py @@ -13,9 +13,9 @@ def __enter__(self: T) -> T: def __exit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: self.close() @@ -64,9 +64,9 @@ async def __aenter__(self: A) -> A: async def __aexit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: await self.aclose() diff --git a/httpx/_transports/default.py b/httpx/_transports/default.py index 0995c7fa00..2086454975 100644 --- a/httpx/_transports/default.py +++ b/httpx/_transports/default.py @@ -113,14 +113,14 @@ class HTTPTransport(BaseTransport): def __init__( self, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, - proxy: Proxy = None, - uds: str = None, - local_address: str = None, + proxy: typing.Optional[Proxy] = None, + uds: typing.Optional[str] = None, + local_address: typing.Optional[str] = None, retries: int = 0, ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) @@ -189,9 +189,9 @@ def __enter__(self: T) -> T: # Use generics for subclass support. def __exit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: with map_httpcore_exceptions(): self._pool.__exit__(exc_type, exc_value, traceback) @@ -248,14 +248,14 @@ class AsyncHTTPTransport(AsyncBaseTransport): def __init__( self, verify: VerifyTypes = True, - cert: CertTypes = None, + cert: typing.Optional[CertTypes] = None, http1: bool = True, http2: bool = False, limits: Limits = DEFAULT_LIMITS, trust_env: bool = True, - proxy: Proxy = None, - uds: str = None, - local_address: str = None, + proxy: typing.Optional[Proxy] = None, + uds: typing.Optional[str] = None, + local_address: typing.Optional[str] = None, retries: int = 0, ) -> None: ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) @@ -324,9 +324,9 @@ async def __aenter__(self: A) -> A: # Use generics for subclass support. async def __aexit__( self, - exc_type: typing.Type[BaseException] = None, - exc_value: BaseException = None, - traceback: TracebackType = None, + exc_type: typing.Optional[typing.Type[BaseException]] = None, + exc_value: typing.Optional[BaseException] = None, + traceback: typing.Optional[TracebackType] = None, ) -> None: with map_httpcore_exceptions(): await self._pool.__aexit__(exc_type, exc_value, traceback) diff --git a/httpx/_types.py b/httpx/_types.py index be2744dcf2..c4063fe407 100644 --- a/httpx/_types.py +++ b/httpx/_types.py @@ -41,7 +41,6 @@ Tuple[Tuple[str, PrimitiveData], ...], str, bytes, - None, ] HeaderTypes = Union[ @@ -74,7 +73,6 @@ Tuple[Union[str, bytes], Union[str, bytes]], Callable[["Request"], "Request"], "Auth", - None, ] RequestContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] diff --git a/httpx/_urls.py b/httpx/_urls.py index f6788e5568..8beeacf160 100644 --- a/httpx/_urls.py +++ b/httpx/_urls.py @@ -557,7 +557,9 @@ class QueryParams(typing.Mapping[str, str]): URL query parameters, as a multi-dict. """ - def __init__(self, *args: QueryParamTypes, **kwargs: typing.Any) -> None: + def __init__( + self, *args: typing.Optional[QueryParamTypes], **kwargs: typing.Any + ) -> None: assert len(args) < 2, "Too many arguments." assert not (args and kwargs), "Cannot mix named and unnamed arguments." @@ -715,7 +717,7 @@ def remove(self, key: str) -> "QueryParams": q._dict.pop(str(key), None) return q - def merge(self, params: QueryParamTypes = None) -> "QueryParams": + def merge(self, params: typing.Optional[QueryParamTypes] = None) -> "QueryParams": """ Return a new QueryParams instance, updated with. @@ -764,7 +766,7 @@ def __repr__(self) -> str: query_string = str(self) return f"{class_name}({query_string!r})" - def update(self, params: QueryParamTypes = None) -> None: + def update(self, params: typing.Optional[QueryParamTypes] = None) -> None: raise RuntimeError( "QueryParams are immutable since 0.18.0. " "Use `q = q.merge(...)` to create an updated copy." diff --git a/httpx/_utils.py b/httpx/_utils.py index 3632783b9a..e01c050dfa 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -30,7 +30,7 @@ def normalize_header_key( value: typing.Union[str, bytes], lower: bool, - encoding: str = None, + encoding: typing.Optional[str] = None, ) -> bytes: """ Coerce str/bytes into a strictly byte-wise HTTP header key. @@ -44,7 +44,7 @@ def normalize_header_key( def normalize_header_value( - value: typing.Union[str, bytes], encoding: str = None + value: typing.Union[str, bytes], encoding: typing.Optional[str] = None ) -> bytes: """ Coerce str/bytes into a strictly byte-wise HTTP header value. diff --git a/setup.cfg b/setup.cfg index 6b14b29bbc..0620f48deb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,6 +5,8 @@ max-line-length = 120 [mypy] disallow_untyped_defs = True ignore_missing_imports = True +no_implicit_optional = True +show_error_codes = True [mypy-tests.*] disallow_untyped_defs = False diff --git a/setup.py b/setup.py index 0ca769f611..f6edfdd2fa 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ def get_packages(package): "charset_normalizer", "sniffio", "rfc3986[idna2008]>=1.3,<2", - "httpcore>=0.14.5,<0.15.0", + "httpcore>=0.15.0,<0.16.0", ], extras_require={ "http2": "h2>=3,<5",