From: Seth Michael Larson Date: Thu, 18 Jul 2019 10:41:50 +0000 (-0500) Subject: Start using flake8, fix lint issues (#126) X-Git-Tag: 0.6.8~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1044cab84124072c15a17332216d3e5ae440f7f;p=thirdparty%2Fhttpx.git Start using flake8, fix lint issues (#126) --- diff --git a/http3/__init__.py b/http3/__init__.py index abc03caf..8de5276e 100644 --- a/http3/__init__.py +++ b/http3/__init__.py @@ -60,3 +60,72 @@ from .models import ( URLTypes, ) from .status_codes import StatusCode, codes + +__all__ = [ + "__description__", + "__title__", + "__version__", + "delete", + "get", + "head", + "options", + "patch", + "post", + "patch", + "put", + "request", + "AsyncClient", + "Client", + "AsyncioBackend", + "USER_AGENT", + "CertTypes", + "PoolLimits", + "SSLConfig", + "TimeoutConfig", + "VerifyTypes", + "HTTPConnection", + "ConnectionPool", + "ConnectTimeout", + "CookieConflict", + "DecodingError", + "InvalidURL", + "PoolTimeout", + "ProtocolError", + "ReadTimeout", + "RedirectBodyUnavailable", + "RedirectLoop", + "ResponseClosed", + "ResponseNotRead", + "StreamConsumed", + "Timeout", + "TooManyRedirects", + "WriteTimeout", + "AsyncDispatcher", + "BaseReader", + "BaseWriter", + "ConcurrencyBackend", + "Dispatcher", + "Protocol", + "URL", + "URLTypes", + "StatusCode", + "codes", + "TimeoutTypes", + "AsyncRequest", + "AsyncRequestData", + "AsyncResponse", + "AsyncResponseContent", + "AuthTypes", + "Cookies", + "CookieTypes", + "Headers", + "HeaderTypes", + "Origin", + "QueryParams", + "QueryParamTypes", + "Request", + "RequestData", + "Response", + "ResponseContent", + "RequestFiles", +] diff --git a/http3/client.py b/http3/client.py index 36569da2..8e20cf2b 100644 --- a/http3/client.py +++ b/http3/client.py @@ -36,7 +36,6 @@ from .models import ( Headers, HeaderTypes, QueryParamTypes, - Request, RequestData, RequestFiles, Response, @@ -196,7 +195,8 @@ class BaseClient: if response.is_redirect: async def send_next() -> AsyncResponse: - nonlocal request, response, verify, cert, allow_redirects, timeout, history + nonlocal request, response, verify, cert + nonlocal allow_redirects, timeout, history request = self.build_redirect_request(request, response) response = await self.send_handling_redirects( request, diff --git a/http3/concurrency.py b/http3/concurrency.py index 928efcfe..b8246b53 100644 --- a/http3/concurrency.py +++ b/http3/concurrency.py @@ -14,7 +14,7 @@ import ssl import typing from types import TracebackType -from .config import DEFAULT_TIMEOUT_CONFIG, PoolLimits, TimeoutConfig +from .config import PoolLimits, TimeoutConfig from .exceptions import ConnectTimeout, PoolTimeout, ReadTimeout, WriteTimeout from .interfaces import ( BaseBackgroundManager, diff --git a/http3/config.py b/http3/config.py index 8ef35708..fba47c93 100644 --- a/http3/config.py +++ b/http3/config.py @@ -76,6 +76,7 @@ class SSLConfig: None, self.load_ssl_context_verify ) + assert self.ssl_context is not None return self.ssl_context def load_ssl_context_no_verify(self) -> ssl.SSLContext: @@ -108,15 +109,15 @@ class SSLConfig: # Signal to server support for PHA in TLS 1.3. Raises an # AttributeError if only read-only access is implemented. try: - context.post_handshake_auth = True - except AttributeError: + context.post_handshake_auth = True # type: ignore + except AttributeError: # pragma: nocover pass # Disable using 'commonName' for SSLContext.check_hostname # when the 'subjectAltName' extension isn't available. try: - context.hostname_checks_common_name = False - except AttributeError: + context.hostname_checks_common_name = False # type: ignore + except AttributeError: # pragma: nocover pass if os.path.isfile(ca_bundle_path): @@ -129,9 +130,11 @@ class SSLConfig: context.load_cert_chain(certfile=self.cert) elif isinstance(self.cert, tuple) and len(self.cert) == 2: context.load_cert_chain(certfile=self.cert[0], keyfile=self.cert[1]) - else: + elif isinstance(self.cert, tuple) and len(self.cert) == 3: context.load_cert_chain( - certfile=self.cert[0], keyfile=self.cert[1], password=self.cert[2] + certfile=self.cert[0], + keyfile=self.cert[1], + password=self.cert[2], # type: ignore ) return context @@ -202,7 +205,10 @@ class TimeoutConfig: class_name = self.__class__.__name__ if len({self.connect_timeout, self.read_timeout, self.write_timeout}) == 1: return f"{class_name}(timeout={self.connect_timeout})" - return f"{class_name}(connect_timeout={self.connect_timeout}, read_timeout={self.read_timeout}, write_timeout={self.write_timeout})" + return ( + f"{class_name}(connect_timeout={self.connect_timeout}, " + f"read_timeout={self.read_timeout}, write_timeout={self.write_timeout})" + ) class PoolLimits: @@ -231,7 +237,10 @@ class PoolLimits: def __repr__(self) -> str: class_name = self.__class__.__name__ - return f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit}, pool_timeout={self.pool_timeout})" + return ( + f"{class_name}(soft_limit={self.soft_limit}, " + f"hard_limit={self.hard_limit}, pool_timeout={self.pool_timeout})" + ) DEFAULT_SSL_CONFIG = SSLConfig(cert=None, verify=True) diff --git a/http3/dispatch/connection.py b/http3/dispatch/connection.py index 68dea5c4..b1400afd 100644 --- a/http3/dispatch/connection.py +++ b/http3/dispatch/connection.py @@ -1,12 +1,8 @@ import functools import typing -import h2.connection -import h11 - from ..concurrency import AsyncioBackend from ..config import ( - DEFAULT_SSL_CONFIG, DEFAULT_TIMEOUT_CONFIG, CertTypes, SSLConfig, @@ -14,7 +10,6 @@ from ..config import ( TimeoutTypes, VerifyTypes, ) -from ..exceptions import ConnectTimeout from ..interfaces import AsyncDispatcher, ConcurrencyBackend, Protocol from ..models import AsyncRequest, AsyncResponse, Origin from .http2 import HTTP2Connection diff --git a/http3/dispatch/connection_pool.py b/http3/dispatch/connection_pool.py index d9794db4..0b827c12 100644 --- a/http3/dispatch/connection_pool.py +++ b/http3/dispatch/connection_pool.py @@ -2,7 +2,6 @@ import typing from ..concurrency import AsyncioBackend from ..config import ( - DEFAULT_CA_BUNDLE_PATH, DEFAULT_POOL_LIMITS, DEFAULT_TIMEOUT_CONFIG, CertTypes, @@ -10,8 +9,7 @@ from ..config import ( TimeoutTypes, VerifyTypes, ) -from ..decoders import ACCEPT_ENCODING -from ..exceptions import NotConnected, PoolTimeout +from ..exceptions import NotConnected from ..interfaces import AsyncDispatcher, ConcurrencyBackend from ..models import AsyncRequest, AsyncResponse, Origin from .connection import HTTPConnection diff --git a/http3/dispatch/http11.py b/http3/dispatch/http11.py index e4124412..c2395356 100644 --- a/http3/dispatch/http11.py +++ b/http3/dispatch/http11.py @@ -3,8 +3,8 @@ import typing import h11 from ..concurrency import TimeoutFlag -from ..config import DEFAULT_TIMEOUT_CONFIG, TimeoutConfig, TimeoutTypes -from ..exceptions import ConnectTimeout, NotConnected, ReadTimeout +from ..config import TimeoutConfig, TimeoutTypes +from ..exceptions import NotConnected from ..interfaces import BaseReader, BaseWriter, ConcurrencyBackend from ..models import AsyncRequest, AsyncResponse @@ -71,7 +71,7 @@ class HTTP11Connection: event = h11.ConnectionClosed() try: self.h11_state.send(event) - except h11.LocalProtocolError as exc: # pragma: no cover + except h11.LocalProtocolError: # pragma: no cover # Premature client disconnect pass await self.writer.close() diff --git a/http3/dispatch/http2.py b/http3/dispatch/http2.py index 3dd778d5..35d487ad 100644 --- a/http3/dispatch/http2.py +++ b/http3/dispatch/http2.py @@ -5,8 +5,8 @@ import h2.connection import h2.events from ..concurrency import TimeoutFlag -from ..config import DEFAULT_TIMEOUT_CONFIG, TimeoutConfig, TimeoutTypes -from ..exceptions import ConnectTimeout, NotConnected, ReadTimeout +from ..config import TimeoutConfig, TimeoutTypes +from ..exceptions import NotConnected from ..interfaces import BaseReader, BaseWriter, ConcurrencyBackend from ..models import AsyncRequest, AsyncResponse diff --git a/http3/interfaces.py b/http3/interfaces.py index 5d9b99c7..02d11ce5 100644 --- a/http3/interfaces.py +++ b/http3/interfaces.py @@ -5,11 +5,9 @@ from types import TracebackType from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypes from .models import ( - URL, AsyncRequest, AsyncRequestData, AsyncResponse, - Headers, HeaderTypes, QueryParamTypes, Request, diff --git a/http3/status_codes.py b/http3/status_codes.py index 1c547770..e7b15dfc 100644 --- a/http3/status_codes.py +++ b/http3/status_codes.py @@ -35,11 +35,16 @@ class StatusCode(IntEnum): @classmethod def is_redirect(cls, value: int) -> bool: return value in ( - StatusCode.MOVED_PERMANENTLY, # 301 (Cacheable redirect. Method may change to GET.) - StatusCode.FOUND, # 302 (Uncacheable redirect. Method may change to GET.) - StatusCode.SEE_OTHER, # 303 (Client should make a GET or HEAD request.) - StatusCode.TEMPORARY_REDIRECT, # 307 (Equiv. 302, but retain method) - StatusCode.PERMANENT_REDIRECT, # 308 (Equiv. 301, but retain method) + # 301 (Cacheable redirect. Method may change to GET.) + StatusCode.MOVED_PERMANENTLY, + # 302 (Uncacheable redirect. Method may change to GET.) + StatusCode.FOUND, + # 303 (Client should make a GET or HEAD request.) + StatusCode.SEE_OTHER, + # 307 (Equiv. 302, but retain method) + StatusCode.TEMPORARY_REDIRECT, + # 308 (Equiv. 301, but retain method) + StatusCode.PERMANENT_REDIRECT, ) @classmethod diff --git a/requirements.txt b/requirements.txt index 5e147046..13f45745 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,6 +16,7 @@ mkdocs-material autoflake black cryptography +flake8 isort mypy pytest diff --git a/scripts/lint b/scripts/lint index 89e1e93a..1c02eb6c 100755 --- a/scripts/lint +++ b/scripts/lint @@ -7,9 +7,10 @@ fi set -x -${PREFIX}autoflake --in-place --recursive http3 tests -${PREFIX}black http3 tests -${PREFIX}isort --multi-line=3 --trailing-comma --force-grid-wrap=0 --combine-as --line-width 88 --recursive --apply http3 tests +${PREFIX}autoflake --in-place --recursive http3 tests setup.py +${PREFIX}isort --multi-line=3 --trailing-comma --force-grid-wrap=0 --combine-as --line-width 88 --recursive --apply http3 tests setup.py +${PREFIX}black http3 tests setup.py +${PREFIX}flake8 --max-line-length=88 --ignore=W503,E203 http3 tests setup.py ${PREFIX}mypy http3 --ignore-missing-imports --disallow-untyped-defs scripts/clean diff --git a/setup.py b/setup.py index a6a33d0d..c2fe8707 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ setup( "h11==0.8.*", "h2==3.*", "idna==2.*", - "rfc3986==1.*" + "rfc3986==1.*", ], classifiers=[ "Development Status :: 3 - Alpha", diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index 597aae77..03c3e2dc 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -1,9 +1,6 @@ import json -import pytest - from http3 import ( - URL, AsyncDispatcher, AsyncRequest, AsyncResponse, diff --git a/tests/client/test_cookies.py b/tests/client/test_cookies.py index f4f7ceb6..77d8095a 100644 --- a/tests/client/test_cookies.py +++ b/tests/client/test_cookies.py @@ -1,10 +1,7 @@ import json from http.cookiejar import Cookie, CookieJar -import pytest - from http3 import ( - URL, AsyncDispatcher, AsyncRequest, AsyncResponse, diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index 30967f33..8484af6e 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -12,8 +12,6 @@ from http3 import ( CertTypes, RedirectBodyUnavailable, RedirectLoop, - Request, - Response, TimeoutTypes, TooManyRedirects, VerifyTypes, diff --git a/tests/dispatch/test_connections.py b/tests/dispatch/test_connections.py index 639ed917..f449e859 100644 --- a/tests/dispatch/test_connections.py +++ b/tests/dispatch/test_connections.py @@ -1,6 +1,6 @@ import pytest -from http3 import HTTPConnection, Request, SSLConfig +from http3 import HTTPConnection @pytest.mark.asyncio diff --git a/tests/dispatch/test_http2.py b/tests/dispatch/test_http2.py index 8da5b0d5..574a0a10 100644 --- a/tests/dispatch/test_http2.py +++ b/tests/dispatch/test_http2.py @@ -1,7 +1,5 @@ import json -import pytest - from http3 import Client, Response from .utils import MockHTTP2Backend diff --git a/tests/dispatch/test_threaded.py b/tests/dispatch/test_threaded.py index 04a9a2e6..a7519004 100644 --- a/tests/dispatch/test_threaded.py +++ b/tests/dispatch/test_threaded.py @@ -1,7 +1,5 @@ import json -import pytest - from http3 import ( CertTypes, Client, diff --git a/tests/test_asgi.py b/tests/test_asgi.py index 79318b9e..3b7d1cc0 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -56,10 +56,10 @@ def test_asgi_upload(): def test_asgi_exc(): client = http3.Client(app=raise_exc) with pytest.raises(ValueError): - response = client.get("http://www.example.org/") + client.get("http://www.example.org/") def test_asgi_exc_after_response(): client = http3.Client(app=raise_exc_after_response) with pytest.raises(ValueError): - response = client.get("http://www.example.org/") + client.get("http://www.example.org/") diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 50e1511b..fbccab76 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -1,8 +1,6 @@ import cgi import io -import pytest - from http3 import ( CertTypes, Client, @@ -41,7 +39,8 @@ def test_multipart(): pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length} multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict) - # Note that the expected return type for text fields appears to differs from 3.6 to 3.7+ + # Note that the expected return type for text fields + # appears to differs from 3.6 to 3.7+ assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"] assert multipart["file"] == [b""] @@ -62,6 +61,7 @@ def test_multipart_file_tuple(): pdict = {"boundary": boundary.encode("ascii"), "CONTENT-LENGTH": content_length} multipart = cgi.parse_multipart(io.BytesIO(response.content), pdict) - # Note that the expected return type for text fields appears to differs from 3.6 to 3.7+ + # Note that the expected return type for text fields + # appears to differs from 3.6 to 3.7+ assert multipart["text"] == ["abc"] or multipart["text"] == [b"abc"] assert multipart["file"] == [b""] diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py index 4a277c9a..4e2a003c 100644 --- a/tests/test_wsgi.py +++ b/tests/test_wsgi.py @@ -61,7 +61,7 @@ def raise_exc(environ, start_response): try: raise ValueError() - except: + except ValueError: exc_info = sys.exc_info() start_response(status, response_headers, exc_info=exc_info) @@ -92,4 +92,4 @@ def test_wsgi_upload_with_response_stream(): def test_wsgi_exc(): client = http3.Client(app=raise_exc) with pytest.raises(ValueError): - response = client.get("http://www.example.org/") + client.get("http://www.example.org/")