From 0cb82000b5998aaa552109b06c9cdea39ac4c47d Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 6 Apr 2021 18:17:34 +0100 Subject: [PATCH] Lint with pre-commit * Move existing tox qa hooks into pre-commit. * Set up GitHub Action based on https://github.com/pre-commit/action/ (we could also use https://pre-commit.ci ). * Add `pyupgrade` to drop old Python syntax. * Add `flake8-bugbear` plugin to prevent flake8 errors. --- .github/workflows/pre-commit.yml | 24 ++++++++++++++++++++++++ .pre-commit-config.yaml | 21 +++++++++++++++++++++ daphne/access.py | 2 +- daphne/cli.py | 4 ++-- daphne/server.py | 2 +- daphne/twisted/plugins/fd_endpoint.py | 2 +- daphne/ws_protocol.py | 4 ++-- setup.py | 2 +- tests/http_base.py | 4 ++-- tests/test_cli.py | 2 -- tests/test_http_request.py | 2 -- tests/test_http_response.py | 2 -- tests/test_utils.py | 2 -- tests/test_websocket.py | 2 -- tox.ini | 11 ----------- 15 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..d939432 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,24 @@ +name: pre-commit + +on: + push: + branches: + - main + pull_request: + +jobs: + pre-commit: + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - uses: pre-commit/action@v2.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5de3ae5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +repos: + - repo: https://github.com/asottile/pyupgrade + rev: v2.11.0 + hooks: + - id: pyupgrade + args: [--py36-plus] + - repo: https://github.com/psf/black + rev: 20.8b1 + hooks: + - id: black + language_version: python3 + - repo: https://github.com/pycqa/isort + rev: 5.8.0 + hooks: + - id: isort + - repo: https://github.com/PyCQA/flake8 + rev: 3.9.0 + hooks: + - id: flake8 + additional_dependencies: + - flake8-bugbear diff --git a/daphne/access.py b/daphne/access.py index 2b3b1cd..e18138a 100644 --- a/daphne/access.py +++ b/daphne/access.py @@ -1,7 +1,7 @@ import datetime -class AccessLogGenerator(object): +class AccessLogGenerator: """ Object that implements the Daphne "action logger" internal interface in order to provide an access log in something resembling NCSA format. diff --git a/daphne/cli.py b/daphne/cli.py index 8c42c43..923b9d3 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -16,7 +16,7 @@ DEFAULT_HOST = "127.0.0.1" DEFAULT_PORT = 8000 -class CommandLineInterface(object): +class CommandLineInterface: """ Acts as the main CLI entry point for running the server. """ @@ -258,7 +258,7 @@ class CommandLineInterface(object): ) endpoints = sorted(args.socket_strings + endpoints) # Start the server - logger.info("Starting server at %s" % (", ".join(endpoints),)) + logger.info("Starting server at {}".format(", ".join(endpoints))) self.server = self.server_class( application=application, endpoints=endpoints, diff --git a/daphne/server.py b/daphne/server.py index 77bd2d1..0ec72cc 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -34,7 +34,7 @@ from .ws_protocol import WebSocketFactory logger = logging.getLogger(__name__) -class Server(object): +class Server: def __init__( self, application, diff --git a/daphne/twisted/plugins/fd_endpoint.py b/daphne/twisted/plugins/fd_endpoint.py index ff5a4c7..313a315 100644 --- a/daphne/twisted/plugins/fd_endpoint.py +++ b/daphne/twisted/plugins/fd_endpoint.py @@ -7,7 +7,7 @@ from zope.interface import implementer @implementer(IPlugin, IStreamServerEndpointStringParser) -class _FDParser(object): +class _FDParser: prefix = "fd" def _parseServer(self, reactor, fileno, domain=socket.AF_INET): diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 1962450..975b1a9 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -297,7 +297,7 @@ class WebSocketProtocol(WebSocketServerProtocol): return id(self) == id(other) def __repr__(self): - return "" % (self.client_addr, self.path) + return f"" class WebSocketFactory(WebSocketServerFactory): @@ -318,7 +318,7 @@ class WebSocketFactory(WebSocketServerFactory): Builds protocol instances. We use this to inject the factory object into the protocol. """ try: - protocol = super(WebSocketFactory, self).buildProtocol(addr) + protocol = super().buildProtocol(addr) protocol.factory = self return protocol except Exception: diff --git a/setup.py b/setup.py index 57341ca..af3b3b9 100755 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ setup( packages=find_packages() + ["twisted.plugins"], include_package_data=True, install_requires=["twisted[tls]>=18.7", "autobahn>=0.18", "asgiref>=3.2.10,<4"], - python_requires='>=3.6', + python_requires=">=3.6", setup_requires=["pytest-runner"], extras_require={ "tests": ["hypothesis==4.23", "pytest~=3.10", "pytest-asyncio~=0.8"] diff --git a/tests/http_base.py b/tests/http_base.py index 7ea796c..e5a80c2 100644 --- a/tests/http_base.py +++ b/tests/http_base.py @@ -182,7 +182,7 @@ class DaphneTestCase(unittest.TestCase): if response.status != 101: raise RuntimeError("WebSocket upgrade did not result in status code 101") # Prepare headers for subprotocol searching - response_headers = dict((n.lower(), v) for n, v in response.getheaders()) + response_headers = {n.lower(): v for n, v in response.getheaders()} response.read() assert not response.closed # Return the raw socket and any subprotocol @@ -252,7 +252,7 @@ class DaphneTestCase(unittest.TestCase): """ try: socket.inet_aton(address) - except socket.error: + except OSError: self.fail("'%s' is not a valid IP address." % address) def assert_key_sets(self, required_keys, optional_keys, actual_keys): diff --git a/tests/test_cli.py b/tests/test_cli.py index 2bbcc42..17335ed 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,5 +1,3 @@ -# coding: utf8 - import logging from argparse import ArgumentError from unittest import TestCase diff --git a/tests/test_http_request.py b/tests/test_http_request.py index 7048326..52f6dd1 100644 --- a/tests/test_http_request.py +++ b/tests/test_http_request.py @@ -1,5 +1,3 @@ -# coding: utf8 - import collections from urllib import parse diff --git a/tests/test_http_response.py b/tests/test_http_response.py index 9dd728d..0f42df2 100644 --- a/tests/test_http_response.py +++ b/tests/test_http_response.py @@ -1,5 +1,3 @@ -# coding: utf8 - import http_strategies from http_base import DaphneTestCase from hypothesis import given, settings diff --git a/tests/test_utils.py b/tests/test_utils.py index 6b04939..b8ef1e1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,3 @@ -# coding: utf8 - from unittest import TestCase from twisted.web.http_headers import Headers diff --git a/tests/test_websocket.py b/tests/test_websocket.py index 26572e7..9b67aa1 100644 --- a/tests/test_websocket.py +++ b/tests/test_websocket.py @@ -1,5 +1,3 @@ -# coding: utf8 - import collections import time from urllib import parse diff --git a/tox.ini b/tox.ini index a4010d7..c507a8b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,6 @@ [tox] envlist = py{36,37,38,39}-twisted{187,latest} - qa [testenv] usedevelop = true @@ -11,13 +10,3 @@ commands = deps = twisted187: twisted==18.7.0 twistedlatest: twisted>=20.3.0 - -[testenv:qa] -deps = - black - flake8 - isort -commands = - flake8 daphne tests - black --check daphne tests - isort --check-only --diff daphne tests