From 1c324ab36d782278b2fd34aba4c598a39079d8fc Mon Sep 17 00:00:00 2001 From: Yoann Schneider <yschneider@teklia.com> Date: Tue, 21 Mar 2023 17:10:27 +0100 Subject: [PATCH] try out ruff as it's faster --- .flake8 | 4 --- .pre-commit-config.yaml | 15 +++----- .ruff.toml | 34 +++++++++++++++++++ arkindex_worker/cache.py | 2 +- arkindex_worker/git.py | 6 ++-- arkindex_worker/image.py | 3 +- arkindex_worker/models.py | 2 +- arkindex_worker/reporting.py | 4 +-- arkindex_worker/worker/__init__.py | 10 +++--- arkindex_worker/worker/base.py | 4 +-- arkindex_worker/worker/training.py | 4 +-- tests/conftest.py | 4 +-- tests/test_base_worker.py | 1 + tests/test_cache.py | 2 +- .../test_classifications.py | 2 +- tests/test_elements_worker/test_elements.py | 4 +-- tests/test_elements_worker/test_entities.py | 2 +- tests/test_elements_worker/test_metadata.py | 2 +- .../test_transcriptions.py | 2 +- tests/test_elements_worker/test_worker.py | 2 +- tests/test_git.py | 2 +- tests/test_image.py | 6 ++-- tests/test_reporting.py | 6 ++-- worker-{{cookiecutter.slug}}/.flake8 | 4 --- .../.pre-commit-config.yaml | 14 +++----- worker-{{cookiecutter.slug}}/.ruff.toml | 34 +++++++++++++++++++ 26 files changed, 113 insertions(+), 62 deletions(-) delete mode 100644 .flake8 create mode 100644 .ruff.toml delete mode 100644 worker-{{cookiecutter.slug}}/.flake8 create mode 100644 worker-{{cookiecutter.slug}}/.ruff.toml diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 788e5856..00000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max-line-length = 88 -exclude = .git,__pycache__ -ignore = E203,E501,W503 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 65827470..de7fac96 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,19 +1,14 @@ repos: - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: 'v0.0.257' hooks: - - id: isort + - id: ruff + args: [--exit-non-zero-on-fix] + exclude: "^worker-{{cookiecutter.slug}}/" - repo: https://github.com/ambv/black rev: 23.1.0 hooks: - id: black - - repo: https://github.com/pycqa/flake8 - rev: 6.0.0 - hooks: - - id: flake8 - additional_dependencies: - - 'flake8-coding==1.3.2' - - 'flake8-debugger==4.1.2' - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 00000000..fa6e473e --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,34 @@ +select = [ + # Pyflakes + "F", + # Pycodestyle + "E", + "W", + # isort + "I001", + # flake8-debugger + "T10", +] + +# Never enforce `E501` (line length violations). +ignore = ["E501"] + +# Do not lint __pycache__ folders +exclude = ["__pycache__"] + +# Autofix issues +fix = true + +# By default, always enumerate fixed violations. +show-fixes = true + +# Group violations by containing file. +format = "gitlab" + +# By default, always show source code snippets. +show-source = true + +[isort] +known-first-party = ["arkindex","arkindex_common","arkindex_worker"] +known-third-party = ["PIL","apistar","gitlab","gnupg","peewee","playhouse","pytest","requests","responses","setuptools","sh","shapely","tenacity","yaml","zstandard"] +force-sort-within-sections = true diff --git a/arkindex_worker/cache.py b/arkindex_worker/cache.py index 42871350..c748ea6d 100644 --- a/arkindex_worker/cache.py +++ b/arkindex_worker/cache.py @@ -6,10 +6,10 @@ On methods that support caching, the database will be used for all reads, and writes will go both to the Arkindex API and the database, reducing network usage. """ - import json import sqlite3 from pathlib import Path +import sqlite3 from typing import Optional, Union from peewee import ( diff --git a/arkindex_worker/git.py b/arkindex_worker/git.py index f30b7818..140627af 100644 --- a/arkindex_worker/git.py +++ b/arkindex_worker/git.py @@ -2,16 +2,16 @@ """ Helper classes for workers that interact with Git repositories and the GitLab API. """ -import shutil -import time from datetime import datetime from pathlib import Path +import shutil +import time from typing import Optional, Union import gitlab +from gitlab.v4.objects import MergeRequest, ProjectMergeRequest import requests import sh -from gitlab.v4.objects import MergeRequest, ProjectMergeRequest from arkindex_worker import logger diff --git a/arkindex_worker/image.py b/arkindex_worker/image.py index c01f17fd..f1db6447 100644 --- a/arkindex_worker/image.py +++ b/arkindex_worker/image.py @@ -5,11 +5,12 @@ Helper methods to download and open IIIF images, and manage polygons. from collections import namedtuple from io import BytesIO from math import ceil +import os from pathlib import Path from typing import TYPE_CHECKING, List, Optional, Union -import requests from PIL import Image +import requests from shapely.affinity import rotate, scale, translate from shapely.geometry import LinearRing from tenacity import ( diff --git a/arkindex_worker/models.py b/arkindex_worker/models.py index 924610b6..4a353f4b 100644 --- a/arkindex_worker/models.py +++ b/arkindex_worker/models.py @@ -3,8 +3,8 @@ Wrappers around API results to provide more convenient attribute access and IIIF helpers. """ -import tempfile from contextlib import contextmanager +import tempfile from typing import Generator, List, Optional from PIL import Image diff --git a/arkindex_worker/reporting.py b/arkindex_worker/reporting.py index b9ee03ef..4c34a340 100644 --- a/arkindex_worker/reporting.py +++ b/arkindex_worker/reporting.py @@ -3,11 +3,11 @@ Generator for the ``ml_report.json`` file, to report created worker results and exceptions. """ -import json -import traceback from collections import Counter from datetime import datetime +import json from pathlib import Path +import traceback from typing import Dict, List, Optional, Union from uuid import UUID diff --git a/arkindex_worker/worker/__init__.py b/arkindex_worker/worker/__init__.py index 2ea54f18..4b349f5b 100644 --- a/arkindex_worker/worker/__init__.py +++ b/arkindex_worker/worker/__init__.py @@ -3,12 +3,12 @@ Base classes to implement Arkindex workers. """ +from enum import Enum import json import os import sys -import uuid -from enum import Enum from typing import Iterable, List, Union +import uuid from apistar.exceptions import ErrorResponse @@ -19,10 +19,10 @@ from arkindex_worker.reporting import Reporter from arkindex_worker.worker.base import BaseWorker from arkindex_worker.worker.classification import ClassificationMixin from arkindex_worker.worker.element import ElementMixin -from arkindex_worker.worker.entity import EntityMixin # noqa: F401 -from arkindex_worker.worker.metadata import MetaDataMixin, MetaType # noqa: F401 +from arkindex_worker.worker.entity import EntityMixin +from arkindex_worker.worker.metadata import MetaDataMixin from arkindex_worker.worker.transcription import TranscriptionMixin -from arkindex_worker.worker.version import WorkerVersionMixin # noqa: F401 +from arkindex_worker.worker.version import WorkerVersionMixin class ActivityState(Enum): diff --git a/arkindex_worker/worker/base.py b/arkindex_worker/worker/base.py index 8d2e454b..f1400e4f 100644 --- a/arkindex_worker/worker/base.py +++ b/arkindex_worker/worker/base.py @@ -10,9 +10,8 @@ import os from pathlib import Path from typing import List, Optional -import gnupg -import yaml from apistar.exceptions import ErrorResponse +import gnupg from tenacity import ( before_sleep_log, retry, @@ -20,6 +19,7 @@ from tenacity import ( stop_after_attempt, wait_exponential, ) +import yaml from arkindex import ArkindexClient, options_from_env from arkindex_worker import logger diff --git a/arkindex_worker/worker/training.py b/arkindex_worker/worker/training.py index fc517607..c4b8dea8 100644 --- a/arkindex_worker/worker/training.py +++ b/arkindex_worker/worker/training.py @@ -3,14 +3,14 @@ BaseWorker methods for training. """ -import functools from contextlib import contextmanager +import functools from pathlib import Path from typing import NewType, Optional, Tuple, Union from uuid import UUID -import requests from apistar.exceptions import ErrorResponse +import requests from arkindex_worker import logger from arkindex_worker.utils import close_delete_file, create_tar_zst_archive diff --git a/tests/conftest.py b/tests/conftest.py index c11d0bdb..ac4494c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,14 +2,14 @@ import hashlib import json import os +from pathlib import Path import sys import time -from pathlib import Path from uuid import UUID +from peewee import SqliteDatabase import pytest import yaml -from peewee import SqliteDatabase from arkindex.mock import MockApiClient from arkindex_worker.cache import ( diff --git a/tests/test_base_worker.py b/tests/test_base_worker.py index b975313e..ce8db4cf 100644 --- a/tests/test_base_worker.py +++ b/tests/test_base_worker.py @@ -3,6 +3,7 @@ import json import logging import sys from pathlib import Path +import sys import gnupg import pytest diff --git a/tests/test_cache.py b/tests/test_cache.py index 77c9ab4c..f85c3629 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -2,8 +2,8 @@ from pathlib import Path from uuid import UUID -import pytest from peewee import OperationalError +import pytest from arkindex_worker.cache import ( SQL_VERSION, diff --git a/tests/test_elements_worker/test_classifications.py b/tests/test_elements_worker/test_classifications.py index 23db7426..ca2d5ffa 100644 --- a/tests/test_elements_worker/test_classifications.py +++ b/tests/test_elements_worker/test_classifications.py @@ -2,8 +2,8 @@ import json from uuid import UUID, uuid4 -import pytest from apistar.exceptions import ErrorResponse +import pytest from arkindex_worker.cache import CachedClassification, CachedElement from arkindex_worker.models import Element diff --git a/tests/test_elements_worker/test_elements.py b/tests/test_elements_worker/test_elements.py index fda06737..4b7e3c02 100644 --- a/tests/test_elements_worker/test_elements.py +++ b/tests/test_elements_worker/test_elements.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -import json from argparse import Namespace +import json from uuid import UUID -import pytest from apistar.exceptions import ErrorResponse +import pytest from responses import matchers from arkindex_worker.cache import ( diff --git a/tests/test_elements_worker/test_entities.py b/tests/test_elements_worker/test_entities.py index b989f3b3..28bff7b3 100644 --- a/tests/test_elements_worker/test_entities.py +++ b/tests/test_elements_worker/test_entities.py @@ -2,8 +2,8 @@ import json from uuid import UUID -import pytest from apistar.exceptions import ErrorResponse +import pytest from responses import matchers from arkindex_worker.cache import ( diff --git a/tests/test_elements_worker/test_metadata.py b/tests/test_elements_worker/test_metadata.py index c18e3f60..5507b30d 100644 --- a/tests/test_elements_worker/test_metadata.py +++ b/tests/test_elements_worker/test_metadata.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import json -import pytest from apistar.exceptions import ErrorResponse +import pytest from arkindex.mock import MockApiClient from arkindex_worker.cache import CachedElement diff --git a/tests/test_elements_worker/test_transcriptions.py b/tests/test_elements_worker/test_transcriptions.py index 70bebc90..d54beb9b 100644 --- a/tests/test_elements_worker/test_transcriptions.py +++ b/tests/test_elements_worker/test_transcriptions.py @@ -2,9 +2,9 @@ import json from uuid import UUID -import pytest from apistar.exceptions import ErrorResponse from playhouse.shortcuts import model_to_dict +import pytest from arkindex_worker.cache import CachedElement, CachedTranscription from arkindex_worker.models import Element diff --git a/tests/test_elements_worker/test_worker.py b/tests/test_elements_worker/test_worker.py index 8be5a573..cac9c161 100644 --- a/tests/test_elements_worker/test_worker.py +++ b/tests/test_elements_worker/test_worker.py @@ -2,8 +2,8 @@ import json import sys -import pytest from apistar.exceptions import ErrorResponse +import pytest from arkindex_worker.cache import CachedElement from arkindex_worker.worker import ActivityState, ElementsWorker diff --git a/tests/test_git.py b/tests/test_git.py index 0c78a456..61716d93 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- from pathlib import Path -import pytest from gitlab import GitlabCreateError, GitlabError +import pytest from requests import ConnectionError from responses import matchers diff --git a/tests/test_image.py b/tests/test_image.py index 4f1002ca..a0c2ac91 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- +from io import BytesIO import math +from pathlib import Path import unittest import uuid -from io import BytesIO -from pathlib import Path -import pytest from PIL import Image, ImageChops, ImageOps +import pytest from arkindex_worker.cache import CachedElement, create_tables, init_cache_db from arkindex_worker.image import ( diff --git a/tests/test_reporting.py b/tests/test_reporting.py index af214f01..352a9898 100644 --- a/tests/test_reporting.py +++ b/tests/test_reporting.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- -import json -import uuid from datetime import datetime +import json from tempfile import NamedTemporaryFile +import uuid -import pytest from apistar.exceptions import ErrorResponse +import pytest from arkindex_worker.models import Transcription from arkindex_worker.reporting import Reporter diff --git a/worker-{{cookiecutter.slug}}/.flake8 b/worker-{{cookiecutter.slug}}/.flake8 deleted file mode 100644 index 7a3797fc..00000000 --- a/worker-{{cookiecutter.slug}}/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max-line-length = 150 -exclude = .git,__pycache__ -ignore = E203,E501,W503 diff --git a/worker-{{cookiecutter.slug}}/.pre-commit-config.yaml b/worker-{{cookiecutter.slug}}/.pre-commit-config.yaml index 93d07e69..d75ecd75 100644 --- a/worker-{{cookiecutter.slug}}/.pre-commit-config.yaml +++ b/worker-{{cookiecutter.slug}}/.pre-commit-config.yaml @@ -1,19 +1,13 @@ repos: - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: 'v0.0.257' hooks: - - id: isort + - id: ruff + args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/ambv/black rev: 23.1.0 hooks: - id: black - - repo: https://github.com/pycqa/flake8 - rev: 6.0.0 - hooks: - - id: flake8 - additional_dependencies: - - 'flake8-coding==1.3.2' - - 'flake8-debugger==4.1.2' - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: diff --git a/worker-{{cookiecutter.slug}}/.ruff.toml b/worker-{{cookiecutter.slug}}/.ruff.toml new file mode 100644 index 00000000..fa6e473e --- /dev/null +++ b/worker-{{cookiecutter.slug}}/.ruff.toml @@ -0,0 +1,34 @@ +select = [ + # Pyflakes + "F", + # Pycodestyle + "E", + "W", + # isort + "I001", + # flake8-debugger + "T10", +] + +# Never enforce `E501` (line length violations). +ignore = ["E501"] + +# Do not lint __pycache__ folders +exclude = ["__pycache__"] + +# Autofix issues +fix = true + +# By default, always enumerate fixed violations. +show-fixes = true + +# Group violations by containing file. +format = "gitlab" + +# By default, always show source code snippets. +show-source = true + +[isort] +known-first-party = ["arkindex","arkindex_common","arkindex_worker"] +known-third-party = ["PIL","apistar","gitlab","gnupg","peewee","playhouse","pytest","requests","responses","setuptools","sh","shapely","tenacity","yaml","zstandard"] +force-sort-within-sections = true -- GitLab