Skip to content

Commit 20380ae

Browse files
authored
Migrate formatting and linting to Ruff (#1145)
1 parent c56655b commit 20380ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+470
-639
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,23 @@ repos:
3939
rev: ff671d6a030a3141634793e6d1e8909ab6091830 # frozen: v1.0.0
4040
hooks:
4141
- id: sphinx-lint
42-
- repo: https://github.com/asottile/pyupgrade
43-
rev: ce40a160603ab0e7d9c627ae33d7ef3906e2d2b2 # frozen: v3.19.1
44-
hooks:
45-
- id: pyupgrade
46-
args: [--py39-plus]
4742
- repo: https://github.com/adamchainz/django-upgrade
4843
rev: 700530171ecf380bc829a64388f49d14ecd61c53 # frozen: 1.25.0
4944
hooks:
5045
- id: django-upgrade
5146
args: [--target-version, '4.2']
52-
- repo: https://github.com/psf/black-pre-commit-mirror
53-
rev: a4920527036bb9a3f3e6055d595849d67d0da066 # frozen: 25.1.0
54-
hooks:
55-
- id: black
5647
- repo: https://github.com/adamchainz/blacken-docs
5748
rev: 78a9dcbecf4f755f65d1f3dec556bc249d723600 # frozen: 1.19.1
5849
hooks:
5950
- id: blacken-docs
6051
additional_dependencies:
6152
- black==25.1.0
62-
- repo: https://github.com/pycqa/isort
63-
rev: c8ab4a5b21bac924d106e3103dd7c979fdd0f9bc # frozen: 6.0.1
53+
- repo: https://github.com/astral-sh/ruff-pre-commit
54+
rev: 12753357c00c3fb8615100354c9fdc6ab80b044d # frozen: v0.11.10
6455
hooks:
65-
- id: isort
66-
name: isort (python)
67-
- repo: https://github.com/PyCQA/flake8
68-
rev: 4b5e89b4b108a6c1a000c591d334a99a80d34c7b # frozen: 7.2.0
69-
hooks:
70-
- id: flake8
71-
additional_dependencies:
72-
- flake8-bugbear
73-
- flake8-comprehensions
74-
- flake8-logging
75-
- flake8-tidy-imports
56+
- id: ruff-check
57+
args: [ --fix ]
58+
- id: ruff-format
7659
- repo: https://github.com/pre-commit/mirrors-mypy
7760
rev: f40886d54c729f533f864ed6ce584e920feb0af7 # frozen: v1.15.0
7861
hooks:

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from __future__ import annotations
77

88
import sys
9-
import tomllib
109
from pathlib import Path
1110

11+
import tomllib
12+
1213
# -- Path setup --------------------------------------------------------------
1314

1415
here = Path(__file__).parent.resolve()

pyproject.toml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,42 @@ django50 = [ "django>=5.0a1,<5.1; python_version>='3.10'" ]
6666
django51 = [ "django>=5.1a1,<5.2; python_version>='3.10'" ]
6767
django52 = [ "django>=5.2a1,<6; python_version>='3.10'" ]
6868

69-
[tool.isort]
70-
add_imports = [
71-
"from __future__ import annotations",
69+
[tool.ruff]
70+
lint.select = [
71+
# flake8-bugbear
72+
"B",
73+
# flake8-comprehensions
74+
"C4",
75+
# pycodestyle
76+
"E",
77+
# Pyflakes errors
78+
"F",
79+
# isort
80+
"I",
81+
# flake8-simplify
82+
"SIM",
83+
# flake8-tidy-imports
84+
"TID",
85+
# pyupgrade
86+
"UP",
87+
# Pyflakes warnings
88+
"W",
7289
]
73-
force_single_line = true
74-
profile = "black"
90+
lint.ignore = [
91+
# flake8-bugbear opinionated rules
92+
"B9",
93+
# line-too-long
94+
"E501",
95+
# suppressible-exception
96+
"SIM105",
97+
# if-else-block-instead-of-if-exp
98+
"SIM108",
99+
]
100+
lint.extend-safe-fixes = [
101+
# non-pep585-annotation
102+
"UP006",
103+
]
104+
lint.isort.required-imports = [ "from __future__ import annotations" ]
75105

76106
[tool.pyproject-fmt]
77107
max_supported_python = "3.13"

src/django_mysql/apps.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any
4-
from typing import Callable
3+
from typing import Any, Callable
54

65
from django.apps import AppConfig
76
from django.conf import settings
@@ -10,8 +9,7 @@
109
from django.utils.translation import gettext_lazy as _
1110

1211
from django_mysql.checks import register_checks
13-
from django_mysql.rewrite_query import REWRITE_MARKER
14-
from django_mysql.rewrite_query import rewrite_query
12+
from django_mysql.rewrite_query import REWRITE_MARKER, rewrite_query
1513
from django_mysql.utils import mysql_connections
1614

1715

@@ -34,12 +32,9 @@ def add_database_instrumentation(self) -> None:
3432
connection_created.connect(install_rewrite_hook)
3533

3634
def add_lookups(self) -> None:
37-
from django.db.models import CharField
38-
from django.db.models import TextField
35+
from django.db.models import CharField, TextField
3936

40-
from django_mysql.models.lookups import CaseSensitiveExact
41-
from django_mysql.models.lookups import Soundex
42-
from django_mysql.models.lookups import SoundsLike
37+
from django_mysql.models.lookups import CaseSensitiveExact, Soundex, SoundsLike
4338

4439
CharField.register_lookup(CaseSensitiveExact)
4540
CharField.register_lookup(SoundsLike)

src/django_mysql/cache.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@
88
from random import random
99
from textwrap import dedent
1010
from time import time
11-
from typing import Any
12-
from typing import Callable
13-
from typing import Literal
14-
from typing import cast
15-
16-
from django.core.cache.backends.base import DEFAULT_TIMEOUT
17-
from django.core.cache.backends.base import BaseCache
18-
from django.core.cache.backends.base import default_key_func
19-
from django.db import connections
20-
from django.db import router
11+
from typing import Any, Callable, Literal, cast
12+
13+
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache, default_key_func
14+
from django.db import connections, router
2115
from django.utils.encoding import force_bytes
2216
from django.utils.module_loading import import_string
2317

24-
from django_mysql.utils import collapse_spaces
25-
from django_mysql.utils import get_list_sql
18+
from django_mysql.utils import collapse_spaces, get_list_sql
2619

2720
_EncodedKeyType = Literal["i", "p", "z"]
2821

@@ -146,7 +139,7 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
146139
"KEY_PREFIX."
147140
)
148141
else:
149-
reverse_key_func = params.get("REVERSE_KEY_FUNCTION", None)
142+
reverse_key_func = params.get("REVERSE_KEY_FUNCTION")
150143
self.reverse_key_func = get_reverse_key_func(reverse_key_func)
151144

152145
# Django API + helpers
@@ -412,7 +405,7 @@ def _base_delta(
412405
)
413406

414407
if not updated:
415-
raise ValueError("Key '%s' not found, or not an integer" % key)
408+
raise ValueError(f"Key '{key}' not found, or not an integer")
416409

417410
# New value stored in insert_id
418411
return cursor.lastrowid
@@ -549,11 +542,9 @@ def keys_with_prefix(
549542

550543
with connections[db].cursor() as cursor:
551544
cursor.execute(
552-
"""SELECT cache_key FROM {table}
545+
f"""SELECT cache_key FROM {table}
553546
WHERE cache_key LIKE %s AND
554-
expires >= %s""".format(
555-
table=table
556-
),
547+
expires >= %s""",
557548
(prefix, self._now()),
558549
)
559550
rows = cursor.fetchall()
@@ -584,12 +575,10 @@ def get_with_prefix(
584575

585576
with connections[db].cursor() as cursor:
586577
cursor.execute(
587-
"""SELECT cache_key, value, value_type
578+
f"""SELECT cache_key, value, value_type
588579
FROM {table}
589580
WHERE cache_key LIKE %s AND
590-
expires >= %s""".format(
591-
table=table
592-
),
581+
expires >= %s""",
593582
(prefix, self._now()),
594583
)
595584
rows = cursor.fetchall()
@@ -612,10 +601,8 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
612601

613602
with connections[db].cursor() as cursor:
614603
return cursor.execute(
615-
"""DELETE FROM {table}
616-
WHERE cache_key LIKE %s""".format(
617-
table=table
618-
),
604+
f"""DELETE FROM {table}
605+
WHERE cache_key LIKE %s""",
619606
(prefix,),
620607
)
621608

@@ -646,19 +633,15 @@ def cull(self) -> int:
646633
else:
647634
cull_num = num // self._cull_frequency
648635
cursor.execute(
649-
"""SELECT cache_key FROM {table}
636+
f"""SELECT cache_key FROM {table}
650637
ORDER BY cache_key
651-
LIMIT 1 OFFSET %s""".format(
652-
table=table
653-
),
638+
LIMIT 1 OFFSET %s""",
654639
(cull_num,),
655640
)
656641
max_key = cursor.fetchone()[0]
657642
num_deleted += cursor.execute(
658-
"""DELETE FROM {table}
659-
WHERE cache_key < %s""".format(
660-
table=table
661-
),
643+
f"""DELETE FROM {table}
644+
WHERE cache_key < %s""",
662645
(max_key,),
663646
)
664647
return num_deleted

src/django_mysql/forms.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
from django.utils.text import format_lazy
99
from django.utils.translation import gettext_lazy as _
1010

11-
from django_mysql.validators import ListMaxLengthValidator
12-
from django_mysql.validators import ListMinLengthValidator
13-
from django_mysql.validators import SetMaxLengthValidator
14-
from django_mysql.validators import SetMinLengthValidator
11+
from django_mysql.validators import (
12+
ListMaxLengthValidator,
13+
ListMinLengthValidator,
14+
SetMaxLengthValidator,
15+
SetMinLengthValidator,
16+
)
1517

1618

1719
class SimpleListField(forms.CharField):
@@ -137,7 +139,7 @@ class SimpleSetField(forms.CharField):
137139
"item_n_invalid": _("Item %(nth)s in the set did not validate: "),
138140
"no_double_commas": _("No leading, trailing, or double commas."),
139141
"no_duplicates": _(
140-
"Duplicates are not supported. " "'%(item)s' appears twice or more."
142+
"Duplicates are not supported. '%(item)s' appears twice or more."
141143
),
142144
}
143145

src/django_mysql/locks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from django.db import connections
77
from django.db.backends.utils import CursorWrapper
88
from django.db.models import Model
9-
from django.db.transaction import TransactionManagementError
10-
from django.db.transaction import atomic
9+
from django.db.transaction import TransactionManagementError, atomic
1110
from django.db.utils import DEFAULT_DB_ALIAS
1211

1312
from django_mysql.exceptions import TimeoutError

src/django_mysql/management/commands/cull_mysql_caches.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
from typing import Any
55

66
from django.conf import settings
7-
from django.core.cache import InvalidCacheBackendError
8-
from django.core.cache import caches
9-
from django.core.management import BaseCommand
10-
from django.core.management import CommandError
7+
from django.core.cache import InvalidCacheBackendError, caches
8+
from django.core.management import BaseCommand, CommandError
119

1210
from django_mysql.cache import MySQLCache
1311
from django_mysql.utils import collapse_spaces

src/django_mysql/management/commands/dbparams.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import argparse
44
from typing import Any
55

6-
from django.core.management import BaseCommand
7-
from django.core.management import CommandError
8-
from django.db import DEFAULT_DB_ALIAS
9-
from django.db import connections
6+
from django.core.management import BaseCommand, CommandError
7+
from django.db import DEFAULT_DB_ALIAS, connections
108
from django.db.utils import ConnectionDoesNotExist
119

1210
from django_mysql.utils import settings_to_cmd_args
@@ -19,8 +17,8 @@ class Command(BaseCommand):
1917
"Outputs shell parameters representing database connection "
2018
"suitable for inclusion in various tools' commandlines. The "
2119
"connection alias should be a name from DATABASES - defaults to "
22-
"'{default}'."
23-
).format(default=DEFAULT_DB_ALIAS)
20+
f"'{DEFAULT_DB_ALIAS}'."
21+
)
2422

2523
requires_system_checks: list[str] = []
2624

@@ -30,7 +28,7 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
3028
metavar="alias",
3129
nargs="?",
3230
default=DEFAULT_DB_ALIAS,
33-
help="Specify the database connection alias to output " "parameters for.",
31+
help="Specify the database connection alias to output parameters for.",
3432
)
3533

3634
parser.add_argument(

src/django_mysql/management/commands/mysql_cache_migration.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
from typing import Any
55

66
from django.conf import settings
7-
from django.core.cache import InvalidCacheBackendError
8-
from django.core.cache import caches
9-
from django.core.management import BaseCommand
10-
from django.core.management import CommandError
7+
from django.core.cache import InvalidCacheBackendError, caches
8+
from django.core.management import BaseCommand, CommandError
119

1210
from django_mysql.cache import MySQLCache
1311
from django_mysql.utils import collapse_spaces

0 commit comments

Comments
 (0)