Skip to content

Commit cd90f37

Browse files
authored
Migrate formatting and linting to Ruff (#1004)
1 parent 50d76eb commit cd90f37

File tree

9 files changed

+62
-67
lines changed

9 files changed

+62
-67
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,23 @@ repos:
3434
- id: rstcheck
3535
additional_dependencies:
3636
- tomli==2.0.1
37-
- repo: https://github.com/asottile/pyupgrade
38-
rev: ce40a160603ab0e7d9c627ae33d7ef3906e2d2b2 # frozen: v3.19.1
39-
hooks:
40-
- id: pyupgrade
41-
args: [--py39-plus]
4237
- repo: https://github.com/adamchainz/django-upgrade
4338
rev: 700530171ecf380bc829a64388f49d14ecd61c53 # frozen: 1.25.0
4439
hooks:
4540
- id: django-upgrade
4641
args: [--target-version, '4.2']
47-
- repo: https://github.com/psf/black-pre-commit-mirror
48-
rev: a4920527036bb9a3f3e6055d595849d67d0da066 # frozen: 25.1.0
49-
hooks:
50-
- id: black
5142
- repo: https://github.com/adamchainz/blacken-docs
5243
rev: 78a9dcbecf4f755f65d1f3dec556bc249d723600 # frozen: 1.19.1
5344
hooks:
5445
- id: blacken-docs
5546
additional_dependencies:
5647
- black==25.1.0
57-
- repo: https://github.com/pycqa/isort
58-
rev: c8ab4a5b21bac924d106e3103dd7c979fdd0f9bc # frozen: 6.0.1
48+
- repo: https://github.com/astral-sh/ruff-pre-commit
49+
rev: 12753357c00c3fb8615100354c9fdc6ab80b044d # frozen: v0.11.10
5950
hooks:
60-
- id: isort
61-
name: isort (python)
62-
- repo: https://github.com/PyCQA/flake8
63-
rev: 4b5e89b4b108a6c1a000c591d334a99a80d34c7b # frozen: 7.2.0
64-
hooks:
65-
- id: flake8
66-
additional_dependencies:
67-
- flake8-bugbear
68-
- flake8-comprehensions
69-
- flake8-logging
70-
- flake8-tidy-imports
51+
- id: ruff-check
52+
args: [ --fix ]
53+
- id: ruff-format
7154
- repo: https://github.com/pre-commit/mirrors-mypy
7255
rev: f40886d54c729f533f864ed6ce584e920feb0af7 # frozen: v1.15.0
7356
hooks:

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/corsheaders/apps.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from __future__ import annotations
22

33
from django.apps import AppConfig
4-
from django.core.checks import Tags
5-
from django.core.checks import register
4+
from django.core.checks import Tags, register
65

76
from corsheaders.checks import check_settings
87

src/corsheaders/checks.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from urllib.parse import urlsplit
77

88
from django.conf import settings
9-
from django.core.checks import CheckMessage
10-
from django.core.checks import Error
9+
from django.core.checks import CheckMessage, Error
1110

1211
from corsheaders.conf import conf
1312

@@ -99,9 +98,7 @@ def check_settings(**kwargs: Any) -> list[CheckMessage]:
9998
if parsed.scheme == "" or parsed.netloc == "":
10099
errors.append(
101100
Error(
102-
"Origin {} in {} is missing scheme or netloc".format(
103-
repr(origin), allowed_origins_alias
104-
),
101+
f"Origin {repr(origin)} in {allowed_origins_alias} is missing scheme or netloc",
105102
id="corsheaders.E013",
106103
hint=(
107104
"Add a scheme (e.g. https://) or netloc (e.g. "
@@ -116,9 +113,7 @@ def check_settings(**kwargs: Any) -> list[CheckMessage]:
116113
if getattr(parsed, part) != "":
117114
errors.append(
118115
Error(
119-
"Origin {} in {} should not have {}".format(
120-
repr(origin), allowed_origins_alias, part
121-
),
116+
f"Origin {repr(origin)} in {allowed_origins_alias} should not have {part}",
122117
id="corsheaders.E014",
123118
)
124119
)
@@ -130,9 +125,7 @@ def check_settings(**kwargs: Any) -> list[CheckMessage]:
130125
if not is_sequence(conf.CORS_ALLOWED_ORIGIN_REGEXES, (str, re_type)):
131126
errors.append(
132127
Error(
133-
"{} should be a sequence of strings and/or compiled regexes.".format(
134-
allowed_regexes_alias
135-
),
128+
f"{allowed_regexes_alias} should be a sequence of strings and/or compiled regexes.",
136129
id="corsheaders.E007",
137130
)
138131
)

src/corsheaders/conf.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
from collections.abc import Sequence
44
from re import Pattern
5-
from typing import Union
6-
from typing import cast
5+
from typing import Union, cast
76

87
from django.conf import settings
98

10-
from corsheaders.defaults import default_headers
11-
from corsheaders.defaults import default_methods
9+
from corsheaders.defaults import default_headers, default_methods
1210

1311

1412
class Settings:

src/corsheaders/middleware.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import re
44
from collections.abc import Awaitable
55
from typing import Callable
6-
from urllib.parse import SplitResult
7-
from urllib.parse import urlsplit
6+
from urllib.parse import SplitResult, urlsplit
87

9-
from asgiref.sync import iscoroutinefunction
10-
from asgiref.sync import markcoroutinefunction
11-
from django.http import HttpRequest
12-
from django.http import HttpResponse
8+
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
9+
from django.http import HttpRequest, HttpResponse
1310
from django.http.response import HttpResponseBase
1411
from django.utils.cache import patch_vary_headers
1512

tests/test_checks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import re
44

55
import pytest
6-
from django.core.checks import CheckMessage
7-
from django.core.checks import Error
8-
from django.core.management import base
9-
from django.core.management import call_command
6+
from django.core.checks import CheckMessage, Error
7+
from django.core.management import base, call_command
108
from django.test import SimpleTestCase
119
from django.test.utils import override_settings
1210

tests/test_middleware.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
from django.test.utils import override_settings
88
from django.utils.deprecation import MiddlewareMixin
99

10-
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_CREDENTIALS
11-
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_HEADERS
12-
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_METHODS
13-
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_ORIGIN
14-
from corsheaders.middleware import ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK
15-
from corsheaders.middleware import ACCESS_CONTROL_EXPOSE_HEADERS
16-
from corsheaders.middleware import ACCESS_CONTROL_MAX_AGE
17-
from tests.utils import prepend_middleware
18-
from tests.utils import temporary_check_request_handler
10+
from corsheaders.middleware import (
11+
ACCESS_CONTROL_ALLOW_CREDENTIALS,
12+
ACCESS_CONTROL_ALLOW_HEADERS,
13+
ACCESS_CONTROL_ALLOW_METHODS,
14+
ACCESS_CONTROL_ALLOW_ORIGIN,
15+
ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK,
16+
ACCESS_CONTROL_EXPOSE_HEADERS,
17+
ACCESS_CONTROL_MAX_AGE,
18+
)
19+
from tests.utils import prepend_middleware, temporary_check_request_handler
1920

2021

2122
class ShortCircuitMiddleware(MiddlewareMixin):

tox.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,3 @@ dependency_groups =
2727
django50: django50
2828
django51: django51
2929
django52: django52
30-
31-
[flake8]
32-
max-line-length = 88
33-
extend-ignore = E203,E501

0 commit comments

Comments
 (0)