Skip to content

Commit d76d6eb

Browse files
committed
Typing: strict mypy checking
Configures mypy to use strict checking, and makes our code compliant with that.
1 parent 78b4d7f commit d76d6eb

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- run: uv python install ${{ matrix.python }}
2222
- run: uv run ruff check src/obelisk/
2323
- run: uv run ruff format --check src/obelisk/
24-
- run: uv run mypy src/obelisk/
24+
- run: uv run mypy
2525
test:
2626
name: Run tests
2727
strategy:

hooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
exec 1>&2
1212

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

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ dev = [
5050

5151
[tool.hatch.build.targets.wheel]
5252
packages = ["src/obelisk"]
53+
54+
[tool.mypy]
55+
files = "src/obelisk"
56+
strict = true

src/obelisk/asynchronous/base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import httpx
77

8-
from obelisk.exceptions import AuthenticationError
8+
from obelisk.exceptions import AuthenticationError, ObeliskError
99
from obelisk.strategies.retry import RetryStrategy, NoRetryStrategy
1010
from obelisk.types import ObeliskKind
1111

@@ -45,7 +45,7 @@ def __init__(
4545

4646
self.log = logging.getLogger("obelisk")
4747

48-
async def _get_token(self):
48+
async def _get_token(self) -> None:
4949
auth_string = str(
5050
base64.b64encode(f"{self._client}:{self._secret}".encode("utf-8")), "utf-8"
5151
)
@@ -60,6 +60,7 @@ async def _get_token(self):
6060
payload = {"grant_type": "client_credentials"}
6161

6262
async with httpx.AsyncClient() as client:
63+
request: httpx.Response | None = None
6364
response = None
6465
last_error = None
6566
retry = self.retry_strategy.make()
@@ -81,6 +82,9 @@ async def _get_token(self):
8182
if response is None and last_error is not None:
8283
raise last_error
8384

85+
if request is None:
86+
raise last_error or ObeliskError("Could not create HTTP request")
87+
8488
if request.status_code != 200:
8589
if "error" in response:
8690
self.log.warning(f"Could not authenticate, {response['error']}")
@@ -91,10 +95,10 @@ async def _get_token(self):
9195
seconds=response["expires_in"]
9296
)
9397

94-
async def _verify_token(self):
95-
if self._token is None or self._token_expires < (
96-
datetime.now() - self.grace_period
97-
):
98+
async def _verify_token(self) -> None:
99+
if (self._token is None or
100+
self._token_expires is None or
101+
self._token_expires < (datetime.now() - self.grace_period)):
98102
retry = self.retry_strategy.make()
99103
first = True
100104
while first or await retry.should_retry():
@@ -107,7 +111,7 @@ async def _verify_token(self):
107111
continue
108112

109113
async def http_post(
110-
self, url: str, data: Any = None, params: Optional[dict] = None
114+
self, url: str, data: Any = None, params: Optional[dict[str, str]] = None
111115
) -> httpx.Response:
112116
"""
113117
Send an HTTP POST request to Obelisk,
@@ -155,7 +159,7 @@ async def http_post(
155159
raise last_error
156160
return response
157161

158-
async def http_get(self, url: str, params: Optional[dict] = None) -> httpx.Response:
162+
async def http_get(self, url: str, params: Optional[dict[str, str]] = None) -> httpx.Response:
159163
"""
160164
Send an HTTP GET request to Obelisk,
161165
with proper auth.

src/obelisk/asynchronous/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from datetime import datetime, timedelta
33
from math import floor
4-
from typing import AsyncGenerator, List, Literal, Optional
4+
from typing import Any, AsyncGenerator, List, Literal, Optional
55

66
import httpx
77
from pydantic import ValidationError
@@ -28,10 +28,10 @@ async def fetch_single_chunk(
2828
fields: Optional[List[str]] = None,
2929
from_timestamp: Optional[int] = None,
3030
to_timestamp: Optional[int] = None,
31-
order_by: Optional[dict] = None,
32-
filter_: Optional[dict] = None,
31+
order_by: Optional[dict[str, Any]] = None,
32+
filter_: Optional[dict[str, Any]] = None,
3333
limit: Optional[int] = None,
34-
limit_by: Optional[dict] = None,
34+
limit_by: Optional[dict[str, Any]] = None,
3535
cursor: Optional[str] = None,
3636
) -> QueryResult:
3737
"""
@@ -116,10 +116,10 @@ async def query(
116116
fields: Optional[List[str]] = None,
117117
from_timestamp: Optional[int] = None,
118118
to_timestamp: Optional[int] = None,
119-
order_by: Optional[dict] = None,
120-
filter_: Optional[dict] = None,
119+
order_by: Optional[dict[str, Any]] = None,
120+
filter_: Optional[dict[str, Any]] = None,
121121
limit: Optional[int] = None,
122-
limit_by: Optional[dict] = None,
122+
limit_by: Optional[dict[str, Any]] = None,
123123
) -> List[Datapoint]:
124124
"""
125125
Queries data from obelisk,
@@ -196,7 +196,7 @@ async def query_time_chunked(
196196
from_time: datetime,
197197
to_time: datetime,
198198
jump: timedelta,
199-
filter_: Optional[dict] = None,
199+
filter_: Optional[dict[str, Any]] = None,
200200
direction: Literal["asc", "desc"] = "asc",
201201
) -> AsyncGenerator[List[Datapoint], None]:
202202
"""
@@ -239,7 +239,7 @@ async def query_time_chunked(
239239
async def send(
240240
self,
241241
dataset: str,
242-
data: List[dict],
242+
data: List[dict[str, Any]],
243243
precision: TimestampPrecision = TimestampPrecision.MILLISECONDS,
244244
mode: IngestMode = IngestMode.DEFAULT,
245245
) -> httpx.Response:

src/obelisk/asynchronous/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def check_datatype_needed(self) -> Self:
159159

160160
return self
161161

162-
def to_dict(self) -> Dict:
162+
def to_dict(self) -> dict[str, Any]:
163163
return self.model_dump(exclude_none=True, by_alias=True)
164164

165165

src/obelisk/sync/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
from datetime import datetime, timedelta
33
from math import floor
4-
from typing import Generator, List, Literal, Optional
4+
from typing import Any, Generator, List, Literal, Optional
55

66
import httpx
77

@@ -49,10 +49,10 @@ def fetch_single_chunk(
4949
fields: Optional[List[str]] = None,
5050
from_timestamp: Optional[int] = None,
5151
to_timestamp: Optional[int] = None,
52-
order_by: Optional[dict] = None,
53-
filter_: Optional[dict] = None,
52+
order_by: Optional[dict[str, Any]] = None,
53+
filter_: Optional[dict[str, Any]] = None,
5454
limit: Optional[int] = None,
55-
limit_by: Optional[dict] = None,
55+
limit_by: Optional[dict[str, Any]] = None,
5656
cursor: Optional[str] = None,
5757
) -> QueryResult:
5858
"""
@@ -119,10 +119,10 @@ def query(
119119
fields: Optional[List[str]] = None,
120120
from_timestamp: Optional[int] = None,
121121
to_timestamp: Optional[int] = None,
122-
order_by: Optional[dict] = None,
123-
filter_: Optional[dict] = None,
122+
order_by: Optional[dict[str, Any]] = None,
123+
filter_: Optional[dict[str, Any]] = None,
124124
limit: Optional[int] = None,
125-
limit_by: Optional[dict] = None,
125+
limit_by: Optional[dict[str, Any]] = None,
126126
) -> List[Datapoint]:
127127
"""
128128
Queries data from obelisk,
@@ -182,7 +182,7 @@ def query_time_chunked(
182182
from_time: datetime,
183183
to_time: datetime,
184184
jump: timedelta,
185-
filter_: Optional[dict] = None,
185+
filter_: Optional[dict[str, Any]] = None,
186186
direction: Literal["asc", "desc"] = "asc",
187187
) -> Generator[List[Datapoint], None, None]:
188188
"""
@@ -225,7 +225,7 @@ def query_time_chunked(
225225
def send(
226226
self,
227227
dataset: str,
228-
data: List[dict],
228+
data: List[dict[str, Any]],
229229
precision: TimestampPrecision = TimestampPrecision.MILLISECONDS,
230230
mode: IngestMode = IngestMode.DEFAULT,
231231
) -> httpx.Response:

src/obelisk/types/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __str__(self) -> str:
6969
return f"'{self.left}'{self.op}{right}"
7070

7171
@staticmethod
72-
def _sstr(item: Any):
72+
def _sstr(item: Any) -> str:
7373
"""Smart string conversion"""
7474
if isinstance(item, datetime):
7575
return item.isoformat()

0 commit comments

Comments
 (0)