Skip to content

Commit bbb66f8

Browse files
authored
Merge pull request #17 from predict-idlab/strict_linting
Strict linting
2 parents 183d2a5 + a5b11e7 commit bbb66f8

File tree

13 files changed

+149
-137
lines changed

13 files changed

+149
-137
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jobs:
1919
- uses: actions/checkout@v4
2020
- uses: astral-sh/setup-uv@v6
2121
- run: uv python install ${{ matrix.python }}
22-
- run: uv run ruff check src/obelisk/
23-
- run: uv run ruff format --check src/obelisk/
22+
- run: uv run ruff check
23+
- run: uv run ruff format
2424
- run: uv run mypy
2525
test:
2626
name: Run tests

hooks/pre-commit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
# Redirect output to stderr.
1111
exec 1>&2
1212

13-
uv run ruff format --check src/obelisk/
13+
uv run ruff format --check
14+
uv run ruff check
1415
uv run mypy

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ packages = ["src/obelisk"]
5454
[tool.mypy]
5555
files = "src/obelisk"
5656
strict = true
57+
58+
[tool.ruff]
59+
include = ["src/obelisk/**/*.py"]
60+
61+
[tool.ruff.lint]
62+
select = ["E4", "E7", "E9", "F", "ASYNC", "S", "B", "FIX", "SIM", "C90", "N", "PERF", "UP"]
63+
# Ignore N815, camelcase field names are usually for serialisation reasons
64+
ignore = ["N815"]

src/obelisk/asynchronous/base.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime, timedelta
22
import logging
33
import base64
4-
from typing import Any, Optional
4+
from typing import Any
55

66
import httpx
77

@@ -19,9 +19,9 @@ class BaseClient:
1919
_client: str = ""
2020
_secret: str = ""
2121

22-
_token: Optional[str] = None
22+
_token: str | None = None
2323
"""Current authentication token"""
24-
_token_expires: Optional[datetime] = None
24+
_token_expires: datetime | None = None
2525
"""Deadline after which token is no longer useable"""
2626

2727
grace_period: timedelta = timedelta(seconds=10)
@@ -35,7 +35,7 @@ def __init__(
3535
self,
3636
client: str,
3737
secret: str,
38-
retry_strategy: RetryStrategy = NoRetryStrategy(),
38+
retry_strategy: RetryStrategy = NoRetryStrategy(), # noqa: B008 # This is fine to bew shared
3939
kind: ObeliskKind = ObeliskKind.CLASSIC,
4040
) -> None:
4141
self._client = client
@@ -47,7 +47,7 @@ def __init__(
4747

4848
async def _get_token(self) -> None:
4949
auth_string = str(
50-
base64.b64encode(f"{self._client}:{self._secret}".encode("utf-8")), "utf-8"
50+
base64.b64encode(f"{self._client}:{self._secret}".encode()), "utf-8"
5151
)
5252
headers = {
5353
"Authorization": f"Basic {auth_string}",
@@ -74,7 +74,7 @@ async def _get_token(self) -> None:
7474
)
7575

7676
response = request.json()
77-
except Exception as e:
77+
except Exception as e: # noqa: PERF203 # retry strategy should add delay
7878
last_error = e
7979
self.log.error(e)
8080
continue
@@ -88,7 +88,7 @@ async def _get_token(self) -> None:
8888
if request.status_code != 200:
8989
if "error" in response:
9090
self.log.warning(f"Could not authenticate, {response['error']}")
91-
raise AuthenticationError
91+
raise AuthenticationError
9292

9393
self._token = response["access_token"]
9494
self._token_expires = datetime.now() + timedelta(
@@ -113,7 +113,7 @@ async def _verify_token(self) -> None:
113113
continue
114114

115115
async def http_post(
116-
self, url: str, data: Any = None, params: Optional[dict[str, str]] = None
116+
self, url: str, data: Any = None, params: dict[str, str] | None = None
117117
) -> httpx.Response:
118118
"""
119119
Send an HTTP POST request to Obelisk,
@@ -162,7 +162,7 @@ async def http_post(
162162
return response
163163

164164
async def http_get(
165-
self, url: str, params: Optional[dict[str, str]] = None
165+
self, url: str, params: dict[str, str] | None = None
166166
) -> httpx.Response:
167167
"""
168168
Send an HTTP GET request to Obelisk,

src/obelisk/asynchronous/client.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
22
from datetime import datetime, timedelta
33
from math import floor
4-
from typing import Any, AsyncGenerator, List, Literal, Optional
4+
from typing import Any, Literal
5+
from collections.abc import AsyncGenerator
56

67
import httpx
78
from pydantic import ValidationError
@@ -23,16 +24,16 @@ class Obelisk(BaseClient):
2324

2425
async def fetch_single_chunk(
2526
self,
26-
datasets: List[str],
27-
metrics: Optional[List[str]] = None,
28-
fields: Optional[List[str]] = None,
29-
from_timestamp: Optional[int] = None,
30-
to_timestamp: Optional[int] = None,
31-
order_by: Optional[dict[str, Any]] = None,
32-
filter_: Optional[dict[str, Any]] = None,
33-
limit: Optional[int] = None,
34-
limit_by: Optional[dict[str, Any]] = None,
35-
cursor: Optional[str] = None,
27+
datasets: list[str],
28+
metrics: list[str] | None = None,
29+
fields: list[str] | None = None,
30+
from_timestamp: int | None = None,
31+
to_timestamp: int | None = None,
32+
order_by: dict[str, Any] | None = None,
33+
filter_: dict[str, Any] | None = None,
34+
limit: int | None = None,
35+
limit_by: dict[str, Any] | None = None,
36+
cursor: str | None = None,
3637
) -> QueryResult:
3738
"""
3839
Queries one chunk of events from Obelisk for given parameters,
@@ -103,24 +104,24 @@ async def fetch_single_chunk(
103104
except json.JSONDecodeError as e:
104105
msg = f"Obelisk response is not a JSON object: {e}"
105106
self.log.warning(msg)
106-
raise ObeliskError(msg)
107+
raise ObeliskError(msg) from e
107108
except ValidationError as e:
108109
msg = f"Response cannot be validated: {e}"
109110
self.log.warning(msg)
110-
raise ObeliskError(msg)
111+
raise ObeliskError(msg) from e
111112

112113
async def query(
113114
self,
114-
datasets: List[str],
115-
metrics: Optional[List[str]] = None,
116-
fields: Optional[List[str]] = None,
117-
from_timestamp: Optional[int] = None,
118-
to_timestamp: Optional[int] = None,
119-
order_by: Optional[dict[str, Any]] = None,
120-
filter_: Optional[dict[str, Any]] = None,
121-
limit: Optional[int] = None,
122-
limit_by: Optional[dict[str, Any]] = None,
123-
) -> List[Datapoint]:
115+
datasets: list[str],
116+
metrics: list[str] | None = None,
117+
fields: list[str] | None = None,
118+
from_timestamp: int | None = None,
119+
to_timestamp: int | None = None,
120+
order_by: dict[str, Any] | None = None,
121+
filter_: dict[str, Any] | None = None,
122+
limit: int | None = None,
123+
limit_by: dict[str, Any] | None = None,
124+
) -> list[Datapoint]:
124125
"""
125126
Queries data from obelisk,
126127
automatically iterating when a cursor is returned.
@@ -157,8 +158,8 @@ async def query(
157158
to a specified maximum number.
158159
"""
159160

160-
cursor: Optional[str] | Literal[True] = True
161-
result_set: List[Datapoint] = []
161+
cursor: str | None | Literal[True] = True
162+
result_set: list[Datapoint] = []
162163

163164
while cursor:
164165
actual_cursor = cursor if cursor is not True else None
@@ -191,14 +192,14 @@ async def query(
191192

192193
async def query_time_chunked(
193194
self,
194-
datasets: List[str],
195-
metrics: List[str],
195+
datasets: list[str],
196+
metrics: list[str],
196197
from_time: datetime,
197198
to_time: datetime,
198199
jump: timedelta,
199-
filter_: Optional[dict[str, Any]] = None,
200+
filter_: dict[str, Any] | None = None,
200201
direction: Literal["asc", "desc"] = "asc",
201-
) -> AsyncGenerator[List[Datapoint], None]:
202+
) -> AsyncGenerator[list[Datapoint], None]:
202203
"""
203204
Fetches all data matching the provided filters,
204205
yielding one chunk at a time.
@@ -239,7 +240,7 @@ async def query_time_chunked(
239240
async def send(
240241
self,
241242
dataset: str,
242-
data: List[dict[str, Any]],
243+
data: list[dict[str, Any]],
243244
precision: TimestampPrecision = TimestampPrecision.MILLISECONDS,
244245
mode: IngestMode = IngestMode.DEFAULT,
245246
) -> httpx.Response:

0 commit comments

Comments
 (0)