Skip to content

Commit 6b9e191

Browse files
authored
Use ruff instead of flake8 and black (#96)
* Use `ruff` instead of flake8 and black * Ignore ruff cache * Enable Pytest lint * Enable eradicable code lint * Enable Ruff lint
1 parent 4bad625 commit 6b9e191

File tree

15 files changed

+87
-86
lines changed

15 files changed

+87
-86
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ build
1010
dist
1111
*.pyc
1212
.pytest_cache/
13+
.ruff_cache/
1314
# dump.rdb is created by redis-server, needed to run tests
1415
dump.rdb

.isort.cfg

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

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
recursive-include docs *
22
recursive-include tests *
3-
include pytest.ini AUTHORS.rst CODE_OF_CONDUCT.md pyproject.toml LICENSE README.rst tox.ini .coveragerc .isort.cfg .readthedocs.yaml
3+
include pytest.ini AUTHORS.rst CODE_OF_CONDUCT.md pyproject.toml LICENSE README.rst tox.ini .coveragerc .readthedocs.yaml
44
prune docs/_build
55
global-exclude *.pyc
66
prune __pycache__

pyproject.toml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
[tool.black]
2-
line-length = 88
3-
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
4-
include = '\.pyi?$'
5-
exclude = '''
6-
/(
7-
\.eggs
8-
| \.git
9-
| \.hg
10-
| \.mypy_cache
11-
| \.tox
12-
| \.venv
13-
| _build
14-
| build
15-
| dist
16-
)/
17-
'''
1+
[tool.ruff]
2+
extend-exclude = [
3+
"__pycache__",
4+
]
5+
6+
[tool.ruff.lint]
7+
select = [
8+
# pycodestyle
9+
"E", "W",
10+
# flake8
11+
"F",
12+
# isort
13+
"I",
14+
# pytest style
15+
"PT",
16+
# eradicate commented code
17+
"ERA",
18+
# ruff lint
19+
"RUF",
20+
]
21+
ignore = [
22+
# `format` will wrap lines.
23+
"E501",
24+
]
25+
26+
[tool.ruff.lint.isort]
27+
known-first-party = ["dockerflow"]

src/dockerflow/django/middleware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import re
33
import time
4+
import typing
45
import urllib
56
import uuid
67

@@ -16,7 +17,7 @@ class DockerflowMiddleware(MiddlewareMixin):
1617
https://github.com/mozilla-services/Dockerflow/blob/main/docs/mozlog.md
1718
"""
1819

19-
viewpatterns = [
20+
viewpatterns: typing.ClassVar = [
2021
(re.compile(r"/__version__/?$"), views.version),
2122
(re.compile(r"/__heartbeat__/?$"), views.heartbeat),
2223
(re.compile(r"/__lbheartbeat__/?$"), views.lbheartbeat),

src/dockerflow/logging.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import socket
99
import sys
1010
import traceback
11+
import typing
1112

1213

1314
class SafeJSONEncoder(json.JSONEncoder):
@@ -32,7 +33,7 @@ class JsonLogFormatter(logging.Formatter):
3233
LOGGING_FORMAT_VERSION = "2.0"
3334

3435
# Map from Python logging to Syslog severity levels
35-
SYSLOG_LEVEL_MAP = {
36+
SYSLOG_LEVEL_MAP: typing.ClassVar = {
3637
50: 2, # CRITICAL
3738
40: 3, # ERROR
3839
30: 4, # WARNING
@@ -43,7 +44,7 @@ class JsonLogFormatter(logging.Formatter):
4344
# Syslog level to use when/if python level isn't found in map
4445
DEFAULT_SYSLOG_LEVEL = 7
4546

46-
EXCLUDED_LOGRECORD_ATTRS = set(
47+
EXCLUDED_LOGRECORD_ATTRS: typing.ClassVar = set(
4748
(
4849
"args",
4950
"asctime",

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import dockerflow.checks.registry
77

88

9-
@pytest.fixture
9+
@pytest.fixture()
1010
def version_content():
1111
"""
1212
as documented on https://github.com/mozilla-services/Dockerflow/blob/main/docs/version_object.md
@@ -20,6 +20,6 @@ def version_content():
2020

2121

2222
@pytest.fixture(autouse=True)
23-
def clear_checks():
23+
def _clear_checks():
2424
yield
2525
dockerflow.checks.registry.clear_checks()

tests/core/test_logging.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@pytest.fixture()
18-
def reset_logging():
18+
def _reset_logging():
1919
logging.shutdown()
2020
reload(logging)
2121

@@ -31,7 +31,8 @@ def assert_records(records):
3131
return details
3232

3333

34-
def test_initialization_from_ini(reset_logging, caplog, tmpdir):
34+
@pytest.mark.usefixtures("_reset_logging")
35+
def test_initialization_from_ini(caplog, tmpdir):
3536
ini_content = textwrap.dedent(
3637
"""
3738
[loggers]
@@ -229,7 +230,5 @@ def test_ignore_json_message(caplog):
229230
}
230231
}
231232
}
232-
""".replace(
233-
"\\", "\\\\"
234-
)
233+
""".replace("\\", "\\\\")
235234
) # HACK: Fix escaping for easy copy/paste

tests/django/test_django.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020

2121

2222
@pytest.fixture(autouse=True)
23-
def reset_checks():
23+
def _reset_checks():
2424
yield
2525
registry.registered_checks = set()
2626
registry.deployment_checks = set()
2727

2828

2929
@pytest.fixture(autouse=True)
30-
def setup_request_summary_logger(dockerflow_middleware):
30+
def _setup_request_summary_logger(dockerflow_middleware):
3131
dockerflow_middleware.summary_logger.addHandler(logging.NullHandler())
3232
dockerflow_middleware.summary_logger.setLevel(logging.INFO)
3333

3434

35-
@pytest.fixture
35+
@pytest.fixture()
3636
def dockerflow_middleware():
3737
return DockerflowMiddleware(get_response=HttpResponse())
3838

@@ -55,7 +55,7 @@ def test_version_missing(dockerflow_middleware, mocker, rf):
5555
assert response.status_code == 404
5656

5757

58-
@pytest.mark.django_db
58+
@pytest.mark.django_db()
5959
def test_heartbeat(client, settings):
6060
response = client.get("/__heartbeat__")
6161
assert response.status_code == 200
@@ -73,7 +73,7 @@ def test_heartbeat(client, settings):
7373
assert content.get("details") is None
7474

7575

76-
@pytest.mark.django_db
76+
@pytest.mark.django_db()
7777
def test_heartbeat_debug(client, settings):
7878
settings.DOCKERFLOW_CHECKS = [
7979
"tests.django.django_checks.warning",
@@ -89,7 +89,7 @@ def test_heartbeat_debug(client, settings):
8989
assert content["details"]
9090

9191

92-
@pytest.mark.django_db
92+
@pytest.mark.django_db()
9393
def test_heartbeat_silenced(client, settings):
9494
settings.DOCKERFLOW_CHECKS = [
9595
"tests.django.django_checks.warning",
@@ -107,8 +107,9 @@ def test_heartbeat_silenced(client, settings):
107107
assert "error" not in content["details"]
108108

109109

110-
@pytest.mark.django_db
111-
def test_heartbeat_logging(dockerflow_middleware, reset_checks, rf, settings, caplog):
110+
@pytest.mark.django_db()
111+
@pytest.mark.usefixtures("_reset_checks")
112+
def test_heartbeat_logging(dockerflow_middleware, rf, settings, caplog):
112113
request = rf.get("/__heartbeat__")
113114
settings.DOCKERFLOW_CHECKS = [
114115
"tests.django.django_checks.warning",
@@ -123,7 +124,7 @@ def test_heartbeat_logging(dockerflow_middleware, reset_checks, rf, settings, ca
123124
assert ("WARNING", "tests.checks.W001: some warning") in logged
124125

125126

126-
@pytest.mark.django_db
127+
@pytest.mark.django_db()
127128
def test_lbheartbeat_makes_no_db_queries(dockerflow_middleware, rf):
128129
queries = CaptureQueriesContext(connection)
129130
request = rf.get("/__lbheartbeat__")
@@ -133,7 +134,7 @@ def test_lbheartbeat_makes_no_db_queries(dockerflow_middleware, rf):
133134
assert len(queries) == 0
134135

135136

136-
@pytest.mark.django_db
137+
@pytest.mark.django_db()
137138
def test_redis_check(client, settings):
138139
settings.DOCKERFLOW_CHECKS = ["dockerflow.django.checks.check_redis_connected"]
139140
checks.register()
@@ -152,7 +153,7 @@ def assert_log_record(request, record, errno=0, level=logging.INFO):
152153
assert isinstance(record.t, int)
153154

154155

155-
@pytest.fixture
156+
@pytest.fixture()
156157
def dockerflow_request(rf):
157158
return rf.get("/", HTTP_USER_AGENT="dockerflow/tests", HTTP_ACCEPT_LANGUAGE="tlh")
158159

@@ -248,15 +249,15 @@ def test_check_database_connected_misconfigured(mocker):
248249
assert errors[0].id == health.ERROR_MISCONFIGURED_DATABASE
249250

250251

251-
@pytest.mark.django_db
252+
@pytest.mark.django_db()
252253
def test_check_database_connected_unsuable(mocker):
253254
mocker.patch("django.db.connection.is_usable", return_value=False)
254255
errors = checks.check_database_connected([])
255256
assert len(errors) == 1
256257
assert errors[0].id == health.ERROR_UNUSABLE_DATABASE
257258

258259

259-
@pytest.mark.django_db
260+
@pytest.mark.django_db()
260261
def test_check_database_connected_success(mocker):
261262
errors = checks.check_database_connected([])
262263
assert errors == []
@@ -272,7 +273,7 @@ def test_check_migrations_applied_cannot_check_migrations(exception, mocker):
272273
assert errors[0].id == health.INFO_CANT_CHECK_MIGRATIONS
273274

274275

275-
@pytest.mark.django_db
276+
@pytest.mark.django_db()
276277
def test_check_migrations_applied_unapplied_migrations(mocker):
277278
mock_loader = mocker.patch("django.db.migrations.loader.MigrationLoader")
278279
mock_loader.return_value.applied_migrations = ["spam", "eggs"]
@@ -306,7 +307,7 @@ def test_check_migrations_applied_unapplied_migrations(mocker):
306307

307308

308309
@pytest.mark.parametrize(
309-
"exception,error",
310+
("exception", "error"),
310311
[
311312
(redis.ConnectionError, health.ERROR_CANNOT_CONNECT_REDIS),
312313
(NotImplementedError, health.ERROR_MISSING_REDIS_CLIENT),

tests/fastapi/test_fastapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def create_app():
2525
return app
2626

2727

28-
@pytest.fixture
28+
@pytest.fixture()
2929
def app():
3030
return create_app()
3131

3232

33-
@pytest.fixture
33+
@pytest.fixture()
3434
def client(app):
3535
return TestClient(app)
3636

@@ -120,7 +120,7 @@ def test_mozlog_failure(client, mocker, caplog):
120120
"dockerflow.fastapi.views.get_version", side_effect=ValueError("crash")
121121
)
122122

123-
with pytest.raises(ValueError):
123+
with pytest.raises(expected_exception=ValueError):
124124
client.get("/__version__")
125125

126126
record = caplog.records[0]

0 commit comments

Comments
 (0)