From ea623449e7c29801ab0a0ade043308937cca7506 Mon Sep 17 00:00:00 2001 From: Shyam Sunder Date: Fri, 5 Jun 2020 10:02:18 -0400 Subject: [PATCH] server: format code to flake8 --- server/.dockerignore | 3 +- server/.pylintrc | 37 ------------------- server/{mypy.ini => setup.cfg} | 6 +++ server/szurubooru/api/info_api.py | 2 +- server/szurubooru/config.py | 2 +- server/szurubooru/db.py | 2 +- server/szurubooru/facade.py | 3 +- server/szurubooru/func/auth.py | 2 +- server/szurubooru/func/image_hash.py | 4 +- server/szurubooru/func/mime.py | 3 +- server/szurubooru/func/net.py | 2 +- server/szurubooru/func/posts.py | 2 +- server/szurubooru/func/snapshots.py | 2 - server/szurubooru/func/util.py | 2 +- server/szurubooru/migrations/env.py | 4 +- ...f5f73f4ab_add_hashes_to_post_file_names.py | 2 +- ...4c7b22846_change_flags_column_to_string.py | 2 - server/szurubooru/model/base.py | 2 +- server/szurubooru/rest/errors.py | 2 +- server/szurubooru/rest/middleware.py | 1 - server/szurubooru/rest/routes.py | 1 - .../search/configs/post_search_config.py | 3 +- server/szurubooru/search/configs/util.py | 2 +- server/szurubooru/tests/conftest.py | 4 +- server/szurubooru/tests/func/test_util.py | 2 +- server/szurubooru/tests/model/test_post.py | 1 - server/szurubooru/tests/model/test_user.py | 1 - server/szurubooru/tests/rest/test_context.py | 1 - .../configs/test_comment_search_config.py | 1 - .../search/configs/test_pool_search_config.py | 1 - .../search/configs/test_post_search_config.py | 1 - .../search/configs/test_tag_search_config.py | 1 - .../search/configs/test_user_search_config.py | 1 - 33 files changed, 27 insertions(+), 78 deletions(-) delete mode 100644 server/.pylintrc rename server/{mypy.ini => setup.cfg} (76%) diff --git a/server/.dockerignore b/server/.dockerignore index 725fbe6e..8c1667bd 100644 --- a/server/.dockerignore +++ b/server/.dockerignore @@ -1,6 +1,5 @@ # Linter configs -.pylintrc -mypy.ini +setup.cfg # Python requirements files requirements.txt diff --git a/server/.pylintrc b/server/.pylintrc deleted file mode 100644 index 846bac61..00000000 --- a/server/.pylintrc +++ /dev/null @@ -1,37 +0,0 @@ -[basic] -function-rgx=^_?[a-z_][a-z0-9_]{2,}$|^test_ -method-rgx=^[a-z_][a-z0-9_]{2,}$|^test_ -const-rgx=^[A-Z_]+$|^_[a-zA-Z_]*$ -good-names=ex,_,logger,i - -[variables] -dummy-variables-rgx=_|dummy - -[format] -max-line-length=79 - -[messages control] -reports=no -disable= - # we're not java - missing-docstring, - broad-except, - - # covered better by pycodestyle - bad-continuation, - - # we're adults - redefined-builtin, - duplicate-code, - too-many-return-statements, - too-many-arguments, - - # plain stupid - no-self-use, - too-few-public-methods - -[typecheck] -generated-members=add|add_all - -[similarities] -min-similarity-lines=5 diff --git a/server/mypy.ini b/server/setup.cfg similarity index 76% rename from server/mypy.ini rename to server/setup.cfg index a0300b7a..21790814 100644 --- a/server/mypy.ini +++ b/server/setup.cfg @@ -1,3 +1,9 @@ +[flake8] +filename = szurubooru/ +exclude = __pycache__ +ignore = F401, W503, W504 +max-line-length = 79 + [mypy] ignore_missing_imports = True follow_imports = skip diff --git a/server/szurubooru/api/info_api.py b/server/szurubooru/api/info_api.py index b0721475..1e2fd1d4 100644 --- a/server/szurubooru/api/info_api.py +++ b/server/szurubooru/api/info_api.py @@ -10,7 +10,7 @@ _cache_result = None # type: Optional[int] def _get_disk_usage() -> int: - global _cache_time, _cache_result # pylint: disable=global-statement + global _cache_time, _cache_result threshold = timedelta(hours=48) now = datetime.utcnow() if _cache_time and _cache_time > now - threshold: diff --git a/server/szurubooru/config.py b/server/szurubooru/config.py index e9962a70..72a24b60 100644 --- a/server/szurubooru/config.py +++ b/server/szurubooru/config.py @@ -56,4 +56,4 @@ def _read_config() -> Dict: return ret -config = _read_config() # pylint: disable=invalid-name +config = _read_config() diff --git a/server/szurubooru/db.py b/server/szurubooru/db.py index 561b7484..03bfaff4 100644 --- a/server/szurubooru/db.py +++ b/server/szurubooru/db.py @@ -4,7 +4,7 @@ import sqlalchemy as sa import sqlalchemy.orm from szurubooru import config -# pylint: disable=invalid-name + _data = threading.local() _engine = sa.create_engine(config.config['database']) # type: Any _sessionmaker = sa.orm.sessionmaker(bind=_engine, autoflush=False) # type: Any diff --git a/server/szurubooru/facade.py b/server/szurubooru/facade.py index 7cac63ca..d8d0b634 100644 --- a/server/szurubooru/facade.py +++ b/server/szurubooru/facade.py @@ -10,7 +10,6 @@ import sqlalchemy.orm.exc from szurubooru import config, db, errors, rest from szurubooru.func.posts import update_all_post_signatures from szurubooru.func.file_uploads import purge_old_uploads -# pylint: disable=unused-import from szurubooru import api, middleware @@ -147,4 +146,4 @@ def create_app() -> Callable[[Any, Any], Any]: return rest.application -app = create_app() # pylint: disable=invalid-name +app = create_app() diff --git a/server/szurubooru/func/auth.py b/server/szurubooru/func/auth.py index 65be79ac..504fe61b 100644 --- a/server/szurubooru/func/auth.py +++ b/server/szurubooru/func/auth.py @@ -54,7 +54,7 @@ def create_password() -> str: 'n': list('0123456789'), } pattern = 'cvcvnncvcv' - return ''.join(random.choice(alphabet[l]) for l in list(pattern)) + return ''.join(random.choice(alphabet[type]) for type in list(pattern)) def is_valid_password(user: model.User, password: str) -> bool: diff --git a/server/szurubooru/func/image_hash.py b/server/szurubooru/func/image_hash.py index da2cd754..771302d2 100644 --- a/server/szurubooru/func/image_hash.py +++ b/server/szurubooru/func/image_hash.py @@ -7,7 +7,7 @@ import numpy as np from PIL import Image from szurubooru import config, errors -# pylint: disable=invalid-name + logger = logging.getLogger(__name__) # Math based on paper from H. Chi Wong, Marshall Bern and David Goldberg @@ -242,7 +242,6 @@ def pack_signature(signature: NpMatrix) -> bytes: This is then converted into a more packed array consisting of uint32 elements (for SIG_CHUNK_BITS = 32). ''' - base = 2 * N_LEVELS + 1 coding_vector = np.flipud(SIG_BASE**np.arange(SIG_CHUNK_WIDTH)) return np.array([ np.dot(x, coding_vector) for x in @@ -256,7 +255,6 @@ def unpack_signature(packed: bytes) -> NpMatrix: Functions as an inverse transformation of pack_signature() ''' - base = 2 * N_LEVELS + 1 return np.ravel(np.array([ [ int(digit) - N_LEVELS for digit in diff --git a/server/szurubooru/func/mime.py b/server/szurubooru/func/mime.py index 871a0a46..afab817e 100644 --- a/server/szurubooru/func/mime.py +++ b/server/szurubooru/func/mime.py @@ -53,7 +53,8 @@ def is_video(mime_type: str) -> bool: def is_image(mime_type: str) -> bool: - return mime_type.lower() in ('image/jpeg', 'image/png', 'image/gif', 'image/webp') + return mime_type.lower() in ( + 'image/jpeg', 'image/png', 'image/gif', 'image/webp') def is_animated_gif(content: bytes) -> bool: diff --git a/server/szurubooru/func/net.py b/server/szurubooru/func/net.py index 17f46548..81103400 100644 --- a/server/szurubooru/func/net.py +++ b/server/szurubooru/func/net.py @@ -49,6 +49,6 @@ def _youtube_dl_wrapper(url: str) -> bytes: except YoutubeDLError as ex: raise errors.ThirdPartyError( 'Error downloading video %s (%s)' % (url, ex)) - except FileNotFoundError as ex: + except FileNotFoundError: raise errors.ThirdPartyError( 'Error downloading video %s (file could not be saved)' % (url)) diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index d8e984bf..e18656ac 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -529,7 +529,7 @@ def update_all_post_signatures() -> None: .filter( (model.Post.type == model.Post.TYPE_IMAGE) | (model.Post.type == model.Post.TYPE_ANIMATION)) - .filter(model.Post.signature == None) + .filter(model.Post.signature == None) # noqa: E711 .order_by(model.Post.post_id.asc()) .all()) for post in posts_to_hash: diff --git a/server/szurubooru/func/snapshots.py b/server/szurubooru/func/snapshots.py index 66464679..38d42a97 100644 --- a/server/szurubooru/func/snapshots.py +++ b/server/szurubooru/func/snapshots.py @@ -61,7 +61,6 @@ def get_post_snapshot(post: model.Post) -> Dict[str, Any]: _snapshot_factories = { # lambdas allow mocking target functions in the tests - # pylint: disable=unnecessary-lambda 'tag_category': lambda entity: get_tag_category_snapshot(entity), 'tag': lambda entity: get_tag_snapshot(entity), 'post': lambda entity: get_post_snapshot(entity), @@ -108,7 +107,6 @@ def create(entity: model.Base, auth_user: Optional[model.User]) -> None: db.session.add(snapshot) -# pylint: disable=protected-access def modify(entity: model.Base, auth_user: Optional[model.User]) -> None: assert entity diff --git a/server/szurubooru/func/util.py b/server/szurubooru/func/util.py index 5e822866..1bf34fa6 100644 --- a/server/szurubooru/func/util.py +++ b/server/szurubooru/func/util.py @@ -84,7 +84,7 @@ def is_valid_email(email: Optional[str]) -> bool: return not email or re.match(r'^[^@]*@[^@]*\.[^@]*$', email) is not None -class dotdict(dict): # pylint: disable=invalid-name +class dotdict(dict): ''' dot.notation access to dictionary attributes. ''' def __getattr__(self, attr: str) -> Any: return self.get(attr) diff --git a/server/szurubooru/migrations/env.py b/server/szurubooru/migrations/env.py index e7d512ef..f0a06dd7 100644 --- a/server/szurubooru/migrations/env.py +++ b/server/szurubooru/migrations/env.py @@ -10,8 +10,8 @@ from time import sleep dir_to_self = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(dir_to_self, *[os.pardir] * 2)) -import szurubooru.model.base -import szurubooru.config +import szurubooru.model.base # noqa: E402 +import szurubooru.config # noqa: E402 alembic_config = alembic.context.config logging.config.fileConfig(alembic_config.config_file_name) diff --git a/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py b/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py index c7e5d01a..1e09e340 100644 --- a/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py +++ b/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py @@ -36,7 +36,7 @@ def downgrade(): entry.name) if match: post_id = int(match.group('name')) - security_hash = match.group('hash') + security_hash = match.group('hash') # noqa: F841 ext = match.group('ext') new_name = '%s.%s' % (post_id, ext) new_path = os.path.join(os.path.dirname(entry.path), new_name) diff --git a/server/szurubooru/migrations/versions/1cd4c7b22846_change_flags_column_to_string.py b/server/szurubooru/migrations/versions/1cd4c7b22846_change_flags_column_to_string.py index b450b1d5..ce017519 100644 --- a/server/szurubooru/migrations/versions/1cd4c7b22846_change_flags_column_to_string.py +++ b/server/szurubooru/migrations/versions/1cd4c7b22846_change_flags_column_to_string.py @@ -29,7 +29,6 @@ def upgrade(): for row in conn.execute(posts.select()): newflag = ','.join(row.oldflags) if row.oldflags else '' conn.execute( - # pylint: disable=no-value-for-parameter posts.update().where( posts.c.id == row.id ).values( @@ -53,7 +52,6 @@ def downgrade(): for row in conn.execute(posts.select()): newflag = [x for x in row.oldflags.split(',') if x] conn.execute( - # pylint: disable=no-value-for-parameter posts.update().where( posts.c.id == row.id ).values( diff --git a/server/szurubooru/model/base.py b/server/szurubooru/model/base.py index e61d35a9..00ea8e13 100644 --- a/server/szurubooru/model/base.py +++ b/server/szurubooru/model/base.py @@ -1,4 +1,4 @@ from sqlalchemy.ext.declarative import declarative_base -Base = declarative_base() # pylint: disable=invalid-name +Base = declarative_base() diff --git a/server/szurubooru/rest/errors.py b/server/szurubooru/rest/errors.py index f90ac252..45dc615f 100644 --- a/server/szurubooru/rest/errors.py +++ b/server/szurubooru/rest/errors.py @@ -1,7 +1,7 @@ from typing import Optional, Callable, Type, Dict -error_handlers = {} # pylint: disable=invalid-name +error_handlers = {} class BaseHttpError(RuntimeError): diff --git a/server/szurubooru/rest/middleware.py b/server/szurubooru/rest/middleware.py index ce457e0c..18b6b465 100644 --- a/server/szurubooru/rest/middleware.py +++ b/server/szurubooru/rest/middleware.py @@ -2,7 +2,6 @@ from typing import List, Callable from szurubooru.rest.context import Context -# pylint: disable=invalid-name pre_hooks = [] # type: List[Callable[[Context], None]] post_hooks = [] # type: List[Callable[[Context], None]] diff --git a/server/szurubooru/rest/routes.py b/server/szurubooru/rest/routes.py index 569cbe1f..93e124e7 100644 --- a/server/szurubooru/rest/routes.py +++ b/server/szurubooru/rest/routes.py @@ -3,7 +3,6 @@ from collections import defaultdict from szurubooru.rest.context import Context, Response -# pylint: disable=invalid-name RouteHandler = Callable[[Context, Dict[str, str]], Response] routes = defaultdict(dict) # type: Dict[str, Dict[str, RouteHandler]] diff --git a/server/szurubooru/search/configs/post_search_config.py b/server/szurubooru/search/configs/post_search_config.py index 281826fa..dcf02552 100644 --- a/server/szurubooru/search/configs/post_search_config.py +++ b/server/szurubooru/search/configs/post_search_config.py @@ -80,8 +80,7 @@ def _user_filter( assert criterion if isinstance(criterion, criteria.PlainCriterion) \ and not criterion.value: - # pylint: disable=singleton-comparison - expr = model.Post.user_id == None + expr = model.Post.user_id == None # noqa: E711 if negated: expr = ~expr return query.filter(expr) diff --git a/server/szurubooru/search/configs/util.py b/server/szurubooru/search/configs/util.py index 9c4d3aee..ee201a26 100644 --- a/server/szurubooru/search/configs/util.py +++ b/server/szurubooru/search/configs/util.py @@ -17,7 +17,7 @@ def unescape(text: str, make_wildcards_special: bool = False) -> str: while i < len(text): if text[i] == '\\': try: - char = text[i+1] + char = text[i + 1] i += 1 except IndexError: raise errors.SearchError( diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py index 13b1c009..c776601d 100644 --- a/server/szurubooru/tests/conftest.py +++ b/server/szurubooru/tests/conftest.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name import contextlib import os import random @@ -39,7 +38,7 @@ def query_logger(pytestconfig): @pytest.yield_fixture(scope='function', autouse=True) -def session(query_logger, postgresql_db): # pylint: disable=unused-argument +def session(query_logger, postgresql_db): db.session = postgresql_db.session postgresql_db.create_table(*model.Base.metadata.sorted_tables) try: @@ -141,7 +140,6 @@ def tag_factory(): @pytest.fixture def post_factory(): - # pylint: disable=invalid-name def factory( id=None, safety=model.Post.SAFETY_SAFE, diff --git a/server/szurubooru/tests/func/test_util.py b/server/szurubooru/tests/func/test_util.py index 24fe4e44..1307ab90 100644 --- a/server/szurubooru/tests/func/test_util.py +++ b/server/szurubooru/tests/func/test_util.py @@ -4,7 +4,7 @@ from szurubooru import errors from szurubooru.func import util -dt = datetime # pylint: disable=invalid-name +dt = datetime def test_parsing_empty_date_time(): diff --git a/server/szurubooru/tests/model/test_post.py b/server/szurubooru/tests/model/test_post.py index 75bcbae5..47c088d3 100644 --- a/server/szurubooru/tests/model/test_post.py +++ b/server/szurubooru/tests/model/test_post.py @@ -50,7 +50,6 @@ def test_saving_post(post_factory, user_factory, tag_factory): assert len(related_post2.relations) == 0 -# pylint: disable=too-many-statements def test_cascade_deletions( post_factory, user_factory, tag_factory, comment_factory): user = user_factory() diff --git a/server/szurubooru/tests/model/test_user.py b/server/szurubooru/tests/model/test_user.py index 08875fa2..ced3a5ef 100644 --- a/server/szurubooru/tests/model/test_user.py +++ b/server/szurubooru/tests/model/test_user.py @@ -111,7 +111,6 @@ def test_disliked_post_count(user_factory, post_factory): assert user1.disliked_post_count == 1 -# pylint: disable=too-many-statements def test_cascade_deletions(post_factory, user_factory, comment_factory): user = user_factory() diff --git a/server/szurubooru/tests/rest/test_context.py b/server/szurubooru/tests/rest/test_context.py index 34cf7ac0..0de1e320 100644 --- a/server/szurubooru/tests/rest/test_context.py +++ b/server/szurubooru/tests/rest/test_context.py @@ -1,4 +1,3 @@ -# pylint: disable=unexpected-keyword-arg import unittest.mock import pytest from szurubooru import rest, errors diff --git a/server/szurubooru/tests/search/configs/test_comment_search_config.py b/server/szurubooru/tests/search/configs/test_comment_search_config.py index 109629bb..7279c1be 100644 --- a/server/szurubooru/tests/search/configs/test_comment_search_config.py +++ b/server/szurubooru/tests/search/configs/test_comment_search_config.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name from datetime import datetime import pytest from szurubooru import db, search diff --git a/server/szurubooru/tests/search/configs/test_pool_search_config.py b/server/szurubooru/tests/search/configs/test_pool_search_config.py index 730511ae..731a6767 100644 --- a/server/szurubooru/tests/search/configs/test_pool_search_config.py +++ b/server/szurubooru/tests/search/configs/test_pool_search_config.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name from datetime import datetime import pytest from szurubooru import db, errors, search diff --git a/server/szurubooru/tests/search/configs/test_post_search_config.py b/server/szurubooru/tests/search/configs/test_post_search_config.py index 4d541d17..462594c6 100644 --- a/server/szurubooru/tests/search/configs/test_post_search_config.py +++ b/server/szurubooru/tests/search/configs/test_post_search_config.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name from datetime import datetime import pytest from szurubooru import db, model, errors, search diff --git a/server/szurubooru/tests/search/configs/test_tag_search_config.py b/server/szurubooru/tests/search/configs/test_tag_search_config.py index d3d2de3f..09a4c403 100644 --- a/server/szurubooru/tests/search/configs/test_tag_search_config.py +++ b/server/szurubooru/tests/search/configs/test_tag_search_config.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name from datetime import datetime import pytest from szurubooru import db, errors, search diff --git a/server/szurubooru/tests/search/configs/test_user_search_config.py b/server/szurubooru/tests/search/configs/test_user_search_config.py index c4d9402a..e9cea5a6 100644 --- a/server/szurubooru/tests/search/configs/test_user_search_config.py +++ b/server/szurubooru/tests/search/configs/test_user_search_config.py @@ -1,4 +1,3 @@ -# pylint: disable=redefined-outer-name from datetime import datetime import pytest from szurubooru import db, errors, search