Skip to content

Commit 36b8103

Browse files
authored
Add ruff and fix lints (#1089)
* Add ruff and fix lints * Silence snyk on test infrastructure * Minor code clean-ups * Add some to-dos for 6.0 * Fix error message missing formatting parameter * Fix error message grammar
1 parent e232b6b commit 36b8103

File tree

262 files changed

+20779
-13739
lines changed

Some content is hidden

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

262 files changed

+20779
-13739
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ repos:
2727
rev: e83a1a42a73284d301c05baaffc176042ffbcf82
2828
hooks:
2929
- id: sphinx-lint
30+
- repo: https://github.com/astral-sh/ruff-pre-commit
31+
rev: v0.6.4
32+
hooks:
33+
- id: ruff-format
34+
- id: ruff
35+
args: [ --fix ]
36+
- id: ruff-format
3037
- repo: local
3138
hooks:
3239
- id: unasync

.snyk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
exclude:
44
code:
55
- tests/**
6+
- benchkit/**
67
- testkitbackend/**
78
- testkit/**

benchkit/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
117
from __future__ import annotations

benchkit/__main__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
117
from __future__ import annotations
218

319
from sanic import Sanic
@@ -7,7 +23,7 @@
723
from .env import env
824

925

10-
if __name__ == '__main__':
26+
if __name__ == "__main__":
1127
loader = AppLoader(factory=create_app)
1228
app = loader.load()
1329

benchkit/app.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
117
from __future__ import annotations
218

19+
import typing as t
320
from contextlib import contextmanager
421
from multiprocessing import Semaphore
522

623
import typing_extensions as te
724
from sanic import Sanic
8-
from sanic.config import Config
925
from sanic.exceptions import (
1026
BadRequest,
1127
NotFound,
1228
)
13-
from sanic.request import Request
1429
from sanic.response import (
1530
empty,
1631
HTTPResponse,
@@ -19,7 +34,13 @@
1934

2035
from .context import BenchKitContext
2136
from .env import env
22-
from .workloads import Workload
37+
38+
39+
if t.TYPE_CHECKING:
40+
from sanic.config import Config
41+
from sanic.request import Request
42+
43+
from .workloads import Workload
2344

2445

2546
T_App: te.TypeAlias = "Sanic[Config, BenchKitContext]"
@@ -29,13 +50,14 @@ def create_app() -> T_App:
2950
app: T_App = Sanic("Python_BenchKit", ctx=BenchKitContext())
3051

3152
@app.main_process_start
32-
async def main_process_start(app: T_App) -> None:
53+
def main_process_start(app: T_App) -> None:
3354
app.shared_ctx.running = Semaphore(1)
3455

3556
@app.before_server_start
36-
async def before_server_start(app: T_App) -> None:
57+
def before_server_start(app: T_App) -> None:
3758
if env.driver_debug:
3859
from neo4j.debug import watch
60+
3961
watch("neo4j")
4062

4163
running = app.shared_ctx.running
@@ -57,13 +79,13 @@ def _loading_workload():
5779
yield
5880
except (ValueError, TypeError) as e:
5981
print(e)
60-
raise BadRequest(str(e))
82+
raise BadRequest(str(e)) from None
6183

6284
def _get_workload(app: T_App, name: str) -> Workload:
6385
try:
6486
workload = app.ctx.workloads[name]
6587
except KeyError:
66-
raise NotFound(f"Workload {name} not found")
88+
raise NotFound(f"Workload {name} not found") from None
6789
return workload
6890

6991
@app.get("/ready")
@@ -72,14 +94,16 @@ async def ready(_: Request) -> HTTPResponse:
7294
return empty()
7395

7496
@app.post("/workload")
75-
async def post_workload(request: Request) -> HTTPResponse:
97+
def post_workload(request: Request) -> HTTPResponse:
7698
data = request.json
7799
with _loading_workload():
78100
name = app.ctx.workloads.store_workload(data)
79101
location = f"/workload/{name}"
80-
return text(f"created at {location}",
81-
status=204,
82-
headers={"location": location})
102+
return text(
103+
f"created at {location}",
104+
status=204,
105+
headers={"location": location},
106+
)
83107

84108
@app.put("/workload")
85109
async def put_workload(request: Request) -> HTTPResponse:
@@ -98,15 +122,15 @@ async def get_workload(_: Request, name: str) -> HTTPResponse:
98122
return empty()
99123

100124
@app.patch("/workload/<name>")
101-
async def patch_workload(request: Request, name: str) -> HTTPResponse:
125+
def patch_workload(request: Request, name: str) -> HTTPResponse:
102126
data = request.json
103127
workload = _get_workload(app, name)
104128
with _loading_workload():
105129
workload.patch(data)
106130
return empty()
107131

108132
@app.delete("/workload/<name>")
109-
async def delete_workload(_: Request, name: str) -> HTTPResponse:
133+
def delete_workload(_: Request, name: str) -> HTTPResponse:
110134
_get_workload(app, name)
111135
del app.ctx.workloads[name]
112136
return empty()

benchkit/context.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
from __future__ import annotations
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
216

3-
import typing as t
17+
from __future__ import annotations
418

5-
import neo4j
619
from neo4j import (
720
AsyncDriver,
821
AsyncGraphDatabase,
@@ -18,7 +31,7 @@
1831

1932

2033
class BenchKitContext:
21-
_db: t.Optional[AsyncDriver]
34+
_db: AsyncDriver | None
2235
workloads: Workloads
2336

2437
def __init__(self) -> None:

benchkit/env.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
# Copyright (c) "Neo4j"
2+
# Neo4j Sweden AB [https://neo4j.com]
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
117
from __future__ import annotations
218

319
import os
@@ -27,7 +43,8 @@ class Env(t.NamedTuple):
2743
neo4j_scheme=os.environ.get("TEST_NEO4J_SCHEME", "neo4j"),
2844
neo4j_user=os.environ.get("TEST_NEO4J_USER", "neo4j"),
2945
neo4j_pass=os.environ.get("TEST_NEO4J_PASS", "password"),
30-
driver_debug=os.environ.get("TEST_DRIVER_DEBUG", "").lower() in (
31-
"y", "yes", "true", "1", "on"
32-
)
46+
driver_debug=(
47+
os.environ.get("TEST_DRIVER_DEBUG", "").lower()
48+
in {"y", "yes", "true", "1", "on"}
49+
),
3350
)

0 commit comments

Comments
 (0)