From e8c2e26ee6bd1e018d86939799b238dd225eaa7c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 30 Jul 2024 17:30:01 +0200 Subject: [PATCH] Pre-commit: Add Python linter Ruff --- .pre-commit-config.yaml | 16 +++++ django_celery_beat/admin.py | 4 +- django_celery_beat/utils.py | 6 +- pyproject.toml | 129 ++++++++++++++++++++++++++++++++++++ t/unit/conftest.py | 2 +- t/unit/test_models.py | 1 - 6 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 pyproject.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 08e64875..da6410ca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,3 +36,19 @@ repos: hooks: - id: django-upgrade args: [--target-version, "3.2"] + + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.5.5 + hooks: # Format before linting + # - id: ruff-format + - id: ruff + + - repo: https://github.com/tox-dev/pyproject-fmt + rev: 2.1.4 + hooks: + - id: pyproject-fmt + + - repo: https://github.com/abravalheri/validate-pyproject + rev: v0.18 + hooks: + - id: validate-pyproject diff --git a/django_celery_beat/admin.py b/django_celery_beat/admin.py index fd022d70..2c462656 100644 --- a/django_celery_beat/admin.py +++ b/django_celery_beat/admin.py @@ -24,8 +24,8 @@ class TaskSelectWidget(Select): def tasks_as_choices(self): _ = self._modules - tasks = list(sorted(name for name in self.celery_app.tasks - if not name.startswith('celery.'))) + tasks = sorted(name for name in self.celery_app.tasks + if not name.startswith('celery.')) return (('', ''), ) + tuple(zip(tasks, tasks)) @property diff --git a/django_celery_beat/utils.py b/django_celery_beat/utils.py index f22b65b6..5751a718 100644 --- a/django_celery_beat/utils.py +++ b/django_celery_beat/utils.py @@ -23,10 +23,8 @@ def make_aware(value): # then convert to the Django configured timezone. default_tz = timezone.get_default_timezone() value = timezone.localtime(value, default_tz) - else: - # naive datetimes are assumed to be in local timezone. - if timezone.is_naive(value): - value = timezone.make_aware(value, timezone.get_default_timezone()) + elif timezone.is_naive(value): + value = timezone.make_aware(value, timezone.get_default_timezone()) return value diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..b6729577 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,129 @@ +[tool.ruff] +target-version = "py38" + +lint.select = [ + "A", # flake8-builtins + "AIR", # Airflow + "ASYNC", # flake8-async + "BLE", # flake8-blind-except + "C4", # flake8-comprehensions + "C90", # McCabe cyclomatic complexity + "DJ", # flake8-django + "E", # pycodestyle + "EXE", # flake8-executable + "F", # Pyflakes + "FA", # flake8-future-annotations + "FLY", # flynt + "FURB", # refurb + "G", # flake8-logging-format + "ICN", # flake8-import-conventions + "INP", # flake8-no-pep420 + "ISC", # flake8-implicit-str-concat + "LOG", # flake8-logging + "N", # pep8-naming + "NPY", # NumPy-specific rules + "PD", # pandas-vet + "PERF", # Perflint + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # Pylint + "PT", # flake8-pytest-style + "RSE", # flake8-raise + "S", # flake8-bandit + "SIM", # flake8-simplify + "SLOT", # flake8-slots + "T10", # flake8-debugger + "TCH", # flake8-type-checking + "TID", # flake8-tidy-imports + "UP", # pyupgrade + "W", # pycodestyle + "YTT", # flake8-2020 + # "ANN", # flake8-annotations + # "ARG", # flake8-unused-arguments + # "B", # flake8-bugbear + # "COM", # flake8-commas + # "CPY", # flake8-copyright + # "D", # pydocstyle + # "DTZ", # flake8-datetimez + # "EM", # flake8-errmsg + # "ERA", # eradicate + # "FBT", # flake8-boolean-trap + # "FIX", # flake8-fixme + # "I", # isort + # "INT", # flake8-gettext + # "PTH", # flake8-use-pathlib + # "PYI", # flake8-pyi + # "Q", # flake8-quotes + # "RET", # flake8-return + # "RUF", # Ruff-specific rules + # "SLF", # flake8-self + # "T20", # flake8-print + # "TD", # flake8-todos + # "TRY", # tryceratops +] +lint.ignore = [ + "DJ001", + "DJ006", + "DJ008", + "DJ012", + "N801", + "N802", + "N803", + "N806", + "PIE790", + "PT004", + "PT009", + "PT027", + "UP031", + "UP032", +] +lint.per-file-ignores."*/migrations/*" = [ + "C405", + "D", + "E501", + "I", +] +lint.per-file-ignores."django_celery_beat/models.py" = [ + "ISC002", +] +lint.per-file-ignores."django_celery_beat/schedulers.py" = [ + "PERF203", + "SIM105", +] +lint.per-file-ignores."django_celery_beat/validators.py" = [ + "BLE001", +] +lint.per-file-ignores."docker/base/celery.py" = [ + "INP001", +] +lint.per-file-ignores."docs/conf.py" = [ + "INP001", +] +lint.per-file-ignores."setup.py" = [ + "EXE001", + "FURB129", + "SIM115", +] +lint.per-file-ignores."t/*" = [ + "S101", +] +lint.per-file-ignores."t/proj/__init__.py" = [ + "PGH004", +] +lint.per-file-ignores."t/proj/settings.py" = [ + "S105", +] +lint.per-file-ignores."t/unit/conftest.py" = [ + "F401", +] +lint.per-file-ignores."t/unit/test_schedulers.py" = [ + "C408", + "PERF102", + "PT018", +] +lint.pylint.allow-magic-value-types = [ + "float", + "int", + "str", +] +lint.pylint.max-args = 8 # Default: 5 diff --git a/t/unit/conftest.py b/t/unit/conftest.py index ef133f2a..836ed40f 100644 --- a/t/unit/conftest.py +++ b/t/unit/conftest.py @@ -44,7 +44,7 @@ def add(x, y): request.instance.app = None -@pytest.fixture +@pytest.fixture() def patching(monkeypatch): def _patching(attr): monkeypatch.setattr(attr, MagicMock()) diff --git a/t/unit/test_models.py b/t/unit/test_models.py index 13ba56c6..df776963 100644 --- a/t/unit/test_models.py +++ b/t/unit/test_models.py @@ -104,7 +104,6 @@ def test_duplicate_schedules(self): "day_of_week": "*", "day_of_month": "*", "month_of_year": "*", - "day_of_week": "*", } schedule = schedules.crontab(hour="4") self._test_duplicate_schedules(CrontabSchedule, kwargs, schedule)