Skip to content

Commit 86c6d71

Browse files
authored
Use Ruff for Python linting and formatting
This replaces the black, pyupgrade, reorder-python-imports and flake8 hooks in pre-commit with ruff.
1 parent 764e09a commit 86c6d71

File tree

10 files changed

+43
-86
lines changed

10 files changed

+43
-86
lines changed

.flake8

Lines changed: 0 additions & 16 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
repos:
2-
- repo: https://github.com/asottile/pyupgrade
3-
rev: v3.16.0
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.4.9
44
hooks:
5-
- id: pyupgrade
6-
args: [--py38-plus]
7-
- repo: https://github.com/asottile/reorder_python_imports
8-
rev: v3.13.0
9-
hooks:
10-
- id: reorder-python-imports
11-
args: [--py38-plus]
12-
- repo: https://github.com/psf/black
13-
rev: "23.12.1"
14-
hooks:
15-
- id: black
16-
args: [--safe, --quiet]
17-
- repo: https://github.com/pycqa/flake8
18-
rev: "7.1.0"
19-
hooks:
20-
- id: flake8
21-
additional_dependencies:
22-
- flake8-bugbear==24.4.26
23-
- flake8-comprehensions==3.14.0
5+
- id: ruff
6+
args: [--fix, --exit-non-zero-on-fix]
7+
- id: ruff-format
248
- repo: https://github.com/igorshubovych/markdownlint-cli
259
rev: v0.41.0
2610
hooks:

fixity/fixity.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ def scan(
159159
else:
160160
report_dict = {
161161
"success": "None",
162-
"message": "Exception encountered while scanning AIP {}: {} ({})".format(
163-
aip, type(e).__name__, str(e)
164-
),
162+
"message": f"Exception encountered while scanning AIP {aip}: {type(e).__name__} ({str(e)})",
165163
"traceback": traceback.format_exc(),
166164
"errors": None,
167165
}

fixity/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from sqlalchemy import Boolean
44
from sqlalchemy import Column
5-
from sqlalchemy import create_engine
65
from sqlalchemy import DateTime
76
from sqlalchemy import ForeignKey
87
from sqlalchemy import Integer
98
from sqlalchemy import String
9+
from sqlalchemy import create_engine
1010
from sqlalchemy.orm import backref
1111
from sqlalchemy.orm import declarative_base
1212
from sqlalchemy.orm import relationship

fixity/reporting.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,11 @@ def post_success_report(aip, report, report_url, report_auth=(), session_id=None
100100

101101
if response.status_code == 500:
102102
raise ReportServiceException(
103-
"Report service encountered an internal error when attempting to POST report for AIP {}".format(
104-
aip
105-
)
103+
f"Report service encountered an internal error when attempting to POST report for AIP {aip}"
106104
)
107105
elif response.status_code != 201:
108106
raise ReportServiceException(
109-
"Report service returned {} when attempting to POST report for AIP {}".format(
110-
response.status_code, aip
111-
)
107+
f"Report service returned {response.status_code} when attempting to POST report for AIP {aip}"
112108
)
113109

114110
return report.posted

fixity/storage_service.py

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from .models import AIP
1010
from .models import Report
1111

12-
1312
UNABLE_TO_CONNECT_ERROR = (
1413
"Unable to connect to storage service instance at {} (is it running?)"
1514
)
@@ -43,27 +42,19 @@ def _get_aips(ss_url, ss_user, ss_key, uri=None):
4342

4443
if response.status_code == 500:
4544
raise StorageServiceError(
46-
'Storage service at "{}" encountered an internal error while requesting AIPs'.format(
47-
ss_url
48-
)
45+
f'Storage service at "{ss_url}" encountered an internal error while requesting AIPs'
4946
)
5047
elif response.status_code == 504:
5148
raise StorageServiceError(
52-
'Storage service at "{}" encountered a gateway timeout while requesting AIPs'.format(
53-
ss_url
54-
)
49+
f'Storage service at "{ss_url}" encountered a gateway timeout while requesting AIPs'
5550
)
5651
elif response.status_code == 401:
5752
raise StorageServiceError(
58-
'Storage service at "{}" failed authentication while requesting AIPs'.format(
59-
ss_url
60-
)
53+
f'Storage service at "{ss_url}" failed authentication while requesting AIPs'
6154
)
6255
elif response.status_code != 200:
6356
raise StorageServiceError(
64-
'Storage service at "{}" returned {} while requesting AIPs'.format(
65-
ss_url, response.status_code
66-
)
57+
f'Storage service at "{ss_url}" returned {response.status_code} while requesting AIPs'
6758
)
6859

6960
results = response.json()
@@ -113,27 +104,19 @@ def get_single_aip(uuid, ss_url, ss_user, ss_key):
113104

114105
if response.status_code == 500:
115106
raise StorageServiceError(
116-
'Storage service at "{}" encountered an internal error while requesting AIP with UUID {}'.format(
117-
ss_url, uuid
118-
)
107+
f'Storage service at "{ss_url}" encountered an internal error while requesting AIP with UUID {uuid}'
119108
)
120109
elif response.status_code == 504:
121110
raise StorageServiceError(
122-
'Storage service at "{}" encounterd a gateway timeout while requesting AIP with UUID {}'.format(
123-
ss_url, uuid
124-
)
111+
f'Storage service at "{ss_url}" encounterd a gateway timeout while requesting AIP with UUID {uuid}'
125112
)
126113
elif response.status_code == 401:
127114
raise StorageServiceError(
128-
'Storage service at "{}" failed authentication while requesting AIP with UUID {}'.format(
129-
ss_url, uuid
130-
)
115+
f'Storage service at "{ss_url}" failed authentication while requesting AIP with UUID {uuid}'
131116
)
132117
if response.status_code != 200:
133118
raise StorageServiceError(
134-
'Storage service at "{}" returned {} while requesting AIP with UUID {}'.format(
135-
ss_url, response.status_code, uuid
136-
)
119+
f'Storage service at "{ss_url}" returned {response.status_code} while requesting AIP with UUID {uuid}'
137120
)
138121
return response.json()
139122

@@ -219,9 +202,7 @@ def scan_aip(
219202
}
220203
report = create_report(aip, None, begun, ended, json.dumps(json_report))
221204
raise StorageServiceError(
222-
'A fixity scan could not be started for the AIP with uuid "{}"'.format(
223-
aip.uuid
224-
),
205+
f'A fixity scan could not be started for the AIP with uuid "{aip.uuid}"',
225206
report=report,
226207
)
227208
if response.status_code == 500:
@@ -233,9 +214,7 @@ def scan_aip(
233214
}
234215
report = create_report(aip, None, begun, ended, json.dumps(json_report))
235216
raise StorageServiceError(
236-
'Storage service at "{}" encountered an internal error while scanning AIP {}'.format(
237-
ss_url, aip.uuid
238-
),
217+
f'Storage service at "{ss_url}" encountered an internal error while scanning AIP {aip.uuid}',
239218
report=report,
240219
)
241220
if response.status_code == 401:
@@ -247,9 +226,7 @@ def scan_aip(
247226
}
248227
report = create_report(aip, None, begun, ended, json.dumps(json_report))
249228
raise StorageServiceError(
250-
'Storage service at "{}" failed authentication while scanning AIP {}'.format(
251-
ss_url, aip.uuid
252-
),
229+
f'Storage service at "{ss_url}" failed authentication while scanning AIP {aip.uuid}',
253230
report=report,
254231
)
255232
if response.status_code != 200:
@@ -261,9 +238,7 @@ def scan_aip(
261238
}
262239
report = create_report(aip, None, begun, ended, json.dumps(json_report))
263240
raise StorageServiceError(
264-
'Storage service at "{}" returned {} while scanning AIP {}'.format(
265-
ss_url, response.status_code, aip.uuid
266-
),
241+
f'Storage service at "{ss_url}" returned {response.status_code} while scanning AIP {aip.uuid}',
267242
report=report,
268243
)
269244

pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,32 @@ dev = [
6161
"pip-tools",
6262
"pytest-cov",
6363
"pytest",
64+
"ruff",
6465
]
6566

6667
[tool.setuptools.dynamic]
6768
version = {attr = "fixity.__version__"}
6869
readme = {file = ["README.md"], content-type = "text/markdown"}
6970

71+
[tool.ruff.lint]
72+
# Rule reference: https://docs.astral.sh/ruff/rules/
73+
select = [
74+
"B",
75+
"C4",
76+
"E",
77+
"F",
78+
"I",
79+
"UP",
80+
"W",
81+
]
82+
ignore = [
83+
"B904",
84+
"E501",
85+
]
86+
87+
[tool.ruff.lint.isort]
88+
force-single-line = true
89+
7090
[tool.pytest.ini_options]
7191
python_files = [
7292
"test_*.py",

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pytest-cov==5.0.0
4646
# via fixity (pyproject.toml)
4747
requests==2.32.3
4848
# via fixity (pyproject.toml)
49+
ruff==0.4.9
50+
# via fixity (pyproject.toml)
4951
sqlalchemy==2.0.31
5052
# via fixity (pyproject.toml)
5153
tomli==2.0.1

tests/test_reporting.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from fixity.models import Report
1111
from fixity.utils import InvalidUUID
1212

13-
1413
REPORT_URL = "http://localhost:8003/"
1514

1615

tests/test_storage_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from fixity.models import Session
99
from fixity.utils import InvalidUUID
1010

11-
1211
SESSION = Session()
1312
STORAGE_SERVICE_URL = "http://localhost:8000/"
1413
STORAGE_SERVICE_USER = "test"

0 commit comments

Comments
 (0)