From 0d2d62eae5b0e51e416830e12aff2c6e4f023467 Mon Sep 17 00:00:00 2001 From: Aksh Gupta Date: Fri, 5 Mar 2021 13:56:03 +0530 Subject: [PATCH] chore: refactor code quality issues (#2044) --- .deepsource.toml | 12 ++++++++++++ sanic/mixins/listeners.py | 2 +- sanic/mixins/middleware.py | 2 +- sanic/websocket.py | 2 +- sanic/worker.py | 2 +- scripts/changelog.py | 3 ++- scripts/release.py | 19 ++++++++++--------- tests/test_blueprints.py | 2 +- tests/test_reloader.py | 2 +- tests/test_response.py | 4 ++-- tests/test_static.py | 2 +- tests/test_url_building.py | 2 +- tests/test_views.py | 2 +- 13 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 00000000..39e06a79 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,12 @@ +version = 1 + +test_patterns = ["tests/**"] + +exclude_patterns = ["docker/**"] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" \ No newline at end of file diff --git a/sanic/mixins/listeners.py b/sanic/mixins/listeners.py index 94e09196..53d4f144 100644 --- a/sanic/mixins/listeners.py +++ b/sanic/mixins/listeners.py @@ -19,7 +19,7 @@ class ListenerEvent(str, Enum): class ListenerMixin: def __init__(self, *args, **kwargs) -> None: - self._future_listeners: List[FutureListener] = list() + self._future_listeners: List[FutureListener] = [] def _apply_listener(self, listener: FutureListener): raise NotImplementedError # noqa diff --git a/sanic/mixins/middleware.py b/sanic/mixins/middleware.py index b10e379e..46b69c4a 100644 --- a/sanic/mixins/middleware.py +++ b/sanic/mixins/middleware.py @@ -6,7 +6,7 @@ from sanic.models.futures import FutureMiddleware class MiddlewareMixin: def __init__(self, *args, **kwargs) -> None: - self._future_middleware: List[FutureMiddleware] = list() + self._future_middleware: List[FutureMiddleware] = [] def _apply_middleware(self, middleware: FutureMiddleware): raise NotImplementedError # noqa diff --git a/sanic/websocket.py b/sanic/websocket.py index 0c728908..29d1dbab 100644 --- a/sanic/websocket.py +++ b/sanic/websocket.py @@ -177,7 +177,7 @@ class WebSocketConnection: { "type": "websocket.accept", "subprotocol": ",".join( - [subprotocol for subprotocol in self.subprotocols] + list(self.subprotocols) ), } ) diff --git a/sanic/worker.py b/sanic/worker.py index 169b875c..342900e6 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -5,7 +5,7 @@ import signal import sys import traceback -from gunicorn.workers import base as base # type: ignore +from gunicorn.workers import base # type: ignore from sanic.log import logger from sanic.server import HttpProtocol, Signal, serve, trigger_events diff --git a/scripts/changelog.py b/scripts/changelog.py index a70dbbc7..8848c426 100755 --- a/scripts/changelog.py +++ b/scripts/changelog.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from os import path +import sys if __name__ == "__main__": try: @@ -10,7 +11,7 @@ if __name__ == "__main__": print( "Please make sure you have a installed towncrier and click before using this tool" ) - exit(1) + sys.exit(1) @click.command() @click.option( diff --git a/scripts/release.py b/scripts/release.py index fce3f751..488ebe2b 100755 --- a/scripts/release.py +++ b/scripts/release.py @@ -10,6 +10,7 @@ from subprocess import Popen, PIPE from jinja2 import Environment, BaseLoader from requests import patch +import sys import towncrier GIT_COMMANDS = { @@ -124,7 +125,7 @@ def _get_current_tag(git_command_name="get_tag"): global GIT_COMMANDS command = GIT_COMMANDS.get(git_command_name) out, err, ret = _run_shell_command(command) - if len(str(out)): + if str(out): return str(out).split("\n")[0] else: return None @@ -178,7 +179,7 @@ def _update_release_version_for_sanic( err.decode("utf-8") ) ) - exit(1) + sys.exit(1) def _generate_change_log(current_version: str = None): @@ -186,13 +187,13 @@ def _generate_change_log(current_version: str = None): command = GIT_COMMANDS.get("get_change_log") command[0] = command[0].format(current_version=current_version) output, error, ret = _run_shell_command(command=command) - if not len(str(output)): + if not str(output): print("Unable to Fetch Change log details to update the Release Note") - exit(1) + sys.exit(1) commit_details = OrderedDict() - commit_details["authors"] = dict() - commit_details["commits"] = list() + commit_details["authors"] = {} + commit_details["commits"] = [] for line in str(output).split("\n"): commit, author, description = line.split(":::") @@ -228,7 +229,7 @@ def _tag_release(new_version, current_version, milestone, release_name, token): out, error, ret = _run_shell_command(command=command) if int(ret) != 0: print("Failed to execute the command: {}".format(command[0])) - exit(1) + sys.exit(1) change_log = _generate_markdown_document( milestone, release_name, current_version, new_version @@ -256,7 +257,7 @@ def release(args: Namespace): current_tag, current_version ) ) - exit(1) + sys.exit(1) new_version = args.release_version or _get_new_version( args.config, current_version, args.micro_release ) @@ -348,6 +349,6 @@ if __name__ == "__main__": }.items(): if not value: print(f"{key} is mandatory while using --tag-release") - exit(1) + sys.exit(1) with Directory(): release(args) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index d7bf231c..25e2621a 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -388,7 +388,7 @@ def test_bp_middleware_with_route(app): def test_bp_middleware_order(app): blueprint = Blueprint("test_bp_middleware_order") - order = list() + order = [] @blueprint.middleware("request") def mw_1(request): diff --git a/tests/test_reloader.py b/tests/test_reloader.py index 90b29343..a31a3d7d 100644 --- a/tests/test_reloader.py +++ b/tests/test_reloader.py @@ -83,7 +83,7 @@ argv = dict( [ (dict(port=42102, auto_reload=True), "script"), (dict(port=42103, debug=True), "module"), - (dict(), "sanic"), + ({}, "sanic"), ], ) async def test_reloader_live(runargs, mode): diff --git a/tests/test_response.py b/tests/test_response.py index 2522a324..366465e0 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -374,7 +374,7 @@ def test_file_head_response(app, file_name, static_file_directory): file_path = os.path.join(static_file_directory, filename) file_path = os.path.abspath(unquote(file_path)) stats = await async_os.stat(file_path) - headers = dict() + headers = {} headers["Accept-Ranges"] = "bytes" headers["Content-Length"] = str(stats.st_size) if request.method == "HEAD": @@ -450,7 +450,7 @@ def test_file_stream_head_response(app, file_name, static_file_directory): async def file_route(request, filename): file_path = os.path.join(static_file_directory, filename) file_path = os.path.abspath(unquote(file_path)) - headers = dict() + headers = {} headers["Accept-Ranges"] = "bytes" if request.method == "HEAD": # Return a normal HTTPResponse, not a diff --git a/tests/test_static.py b/tests/test_static.py index c67ff439..24b5c568 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -103,7 +103,7 @@ def test_static_file_bytes(app, static_file_directory, file_name): @pytest.mark.parametrize( "file_name", - [dict(), list(), object()], + [{}, [], object()], ) def test_static_file_invalid_path(app, static_file_directory, file_name): app.route("/")(lambda x: x) diff --git a/tests/test_url_building.py b/tests/test_url_building.py index 2898dbd9..cccfe543 100644 --- a/tests/test_url_building.py +++ b/tests/test_url_building.py @@ -2,7 +2,7 @@ import string from urllib.parse import parse_qsl, urlsplit -import pytest as pytest +import pytest from sanic_testing.testing import HOST as test_host from sanic_testing.testing import PORT as test_port diff --git a/tests/test_views.py b/tests/test_views.py index 2f912efe..558496c8 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,4 +1,4 @@ -import pytest as pytest +import pytest from sanic.blueprints import Blueprint from sanic.constants import HTTP_METHODS