Skip to content

Commit df68ee5

Browse files
committed
Fix type issues
1 parent 0ec22e5 commit df68ee5

4 files changed

Lines changed: 66 additions & 16 deletions

File tree

src/contiguity/_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ErrorResponse(BaseResponse):
2929

3030

3131
def decode_response(content: bytes, /, *, type: type[T]) -> T:
32-
raw = msgspec.json.decode(content, type=RawResponse[type])
32+
raw = msgspec.json.decode(content, type=RawResponse[type]) # ty: ignore[invalid-type-form]
3333
metadata = ResponseMetadata(
3434
id=raw.id,
3535
timestamp=raw.timestamp,

src/contiguity/base/async_base.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import Mapping, Sequence
33
from datetime import datetime, timedelta, timezone
44
from http import HTTPStatus
5-
from typing import Any, Generic, Literal, overload
5+
from typing import Any, Generic, Literal, cast, overload
66
from warnings import warn
77

88
import msgspec
@@ -34,13 +34,38 @@ class AsyncBase(Generic[ItemT]):
3434
EXPIRES_ATTRIBUTE = "__expires"
3535
PUT_LIMIT = 30
3636

37+
@overload
38+
def __init__(
39+
self: "AsyncBase[dict[str, Any]]",
40+
name: str,
41+
/,
42+
*,
43+
data_key: str | None = None,
44+
project_id: str | None = None,
45+
host: str | None = None,
46+
api_version: str = "v1",
47+
) -> None: ...
48+
49+
@overload
50+
@deprecated("The `project_key` parameter has been renamed to `data_key`.")
51+
def __init__(
52+
self: "AsyncBase[dict[str, Any]]",
53+
name: str,
54+
/,
55+
*,
56+
project_key: str | None = None,
57+
project_id: str | None = None,
58+
host: str | None = None,
59+
api_version: str = "v1",
60+
) -> None: ...
61+
3762
@overload
3863
def __init__(
3964
self,
4065
name: str,
4166
/,
4267
*,
43-
item_type: type[ItemT] = Mapping[str, Any],
68+
item_type: type[ItemT],
4469
data_key: str | None = None,
4570
project_id: str | None = None,
4671
host: str | None = None,
@@ -54,7 +79,7 @@ def __init__(
5479
name: str,
5580
/,
5681
*,
57-
item_type: type[ItemT] = Mapping[str, Any],
82+
item_type: type[ItemT],
5883
project_key: str | None = None,
5984
project_id: str | None = None,
6085
host: str | None = None,
@@ -66,7 +91,7 @@ def __init__( # noqa: PLR0913
6691
name: str,
6792
/,
6893
*,
69-
item_type: type[ItemT] = Mapping[str, Any],
94+
item_type: type[ItemT] | None = None,
7095
data_key: str | None = None,
7196
project_key: str | None = None, # Deprecated.
7297
project_id: str | None = None,
@@ -78,7 +103,7 @@ def __init__( # noqa: PLR0913
78103
raise ValueError(msg)
79104

80105
self.name = name
81-
self.item_type = item_type
106+
self.item_type = cast("type[ItemT]", item_type if item_type is not None else dict[str, Any])
82107
self.data_key = data_key or project_key or get_data_key()
83108
self.project_id = project_id or get_project_id()
84109
self.host = host or os.getenv("CONTIGUITY_BASE_HOST") or "api.base.contiguity.co"
@@ -118,7 +143,7 @@ def _response_as_item_type(
118143
response.raise_for_status()
119144
except HTTPStatusError as exc:
120145
raise ContiguityApiError(exc.response.text) from exc
121-
return msgspec.json.decode(response.content, type=Sequence[self.item_type] if sequence else self.item_type)
146+
return msgspec.json.decode(response.content, type=Sequence[self.item_type] if sequence else self.item_type) # ty: ignore[invalid-type-form]
122147

123148
def _insert_expires_attr(
124149
self,
@@ -288,7 +313,7 @@ async def query(
288313
response.raise_for_status()
289314
except HTTPStatusError as exc:
290315
raise ContiguityApiError(exc.response.text) from exc
291-
return msgspec.json.decode(response.content, type=QueryResponse[self.item_type])
316+
return msgspec.json.decode(response.content, type=QueryResponse[self.item_type]) # ty: ignore[invalid-type-form]
292317

293318
@deprecated("This method has been renamed to `query` and will be removed in a future release.")
294319
async def fetch(

src/contiguity/base/base.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections.abc import Mapping, Sequence
1212
from datetime import datetime, timedelta, timezone
1313
from http import HTTPStatus
14-
from typing import Any, Generic, Literal, overload
14+
from typing import Any, Generic, Literal, cast, overload
1515
from warnings import warn
1616

1717
import msgspec
@@ -43,13 +43,38 @@ class Base(Generic[ItemT]):
4343
EXPIRES_ATTRIBUTE = "__expires"
4444
PUT_LIMIT = 30
4545

46+
@overload
47+
def __init__(
48+
self: "Base[dict[str, Any]]",
49+
name: str,
50+
/,
51+
*,
52+
data_key: str | None = None,
53+
project_id: str | None = None,
54+
host: str | None = None,
55+
api_version: str = "v1",
56+
) -> None: ...
57+
58+
@overload
59+
@deprecated("The `project_key` parameter has been renamed to `data_key`.")
60+
def __init__(
61+
self: "Base[dict[str, Any]]",
62+
name: str,
63+
/,
64+
*,
65+
project_key: str | None = None,
66+
project_id: str | None = None,
67+
host: str | None = None,
68+
api_version: str = "v1",
69+
) -> None: ...
70+
4671
@overload
4772
def __init__(
4873
self,
4974
name: str,
5075
/,
5176
*,
52-
item_type: type[ItemT] = Mapping[str, Any],
77+
item_type: type[ItemT],
5378
data_key: str | None = None,
5479
project_id: str | None = None,
5580
host: str | None = None,
@@ -63,7 +88,7 @@ def __init__(
6388
name: str,
6489
/,
6590
*,
66-
item_type: type[ItemT] = Mapping[str, Any],
91+
item_type: type[ItemT],
6792
project_key: str | None = None,
6893
project_id: str | None = None,
6994
host: str | None = None,
@@ -75,7 +100,7 @@ def __init__( # noqa: PLR0913
75100
name: str,
76101
/,
77102
*,
78-
item_type: type[ItemT] = Mapping[str, Any],
103+
item_type: type[ItemT] | None = None,
79104
data_key: str | None = None,
80105
project_key: str | None = None, # Deprecated.
81106
project_id: str | None = None,
@@ -87,7 +112,7 @@ def __init__( # noqa: PLR0913
87112
raise ValueError(msg)
88113

89114
self.name = name
90-
self.item_type = item_type
115+
self.item_type = cast("type[ItemT]", item_type if item_type is not None else dict[str, Any])
91116
self.data_key = data_key or project_key or get_data_key()
92117
self.project_id = project_id or get_project_id()
93118
self.host = host or os.getenv("CONTIGUITY_BASE_HOST") or "api.base.contiguity.co"
@@ -127,7 +152,7 @@ def _response_as_item_type(
127152
response.raise_for_status()
128153
except HTTPStatusError as exc:
129154
raise ContiguityApiError(exc.response.text) from exc
130-
return msgspec.json.decode(response.content, type=Sequence[self.item_type] if sequence else self.item_type)
155+
return msgspec.json.decode(response.content, type=Sequence[self.item_type] if sequence else self.item_type) # ty: ignore[invalid-type-form]
131156

132157
def _insert_expires_attr(
133158
self,
@@ -297,7 +322,7 @@ def query(
297322
response.raise_for_status()
298323
except HTTPStatusError as exc:
299324
raise ContiguityApiError(exc.response.text) from exc
300-
return msgspec.json.decode(response.content, type=QueryResponse[self.item_type])
325+
return msgspec.json.decode(response.content, type=QueryResponse[self.item_type]) # ty: ignore[invalid-type-form]
301326

302327
@deprecated("This method has been renamed to `query` and will be removed in a future release.")
303328
def fetch(

src/contiguity/base/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, value: int = 1, /) -> None:
5050

5151
class Append(UpdateOperation):
5252
def __init__(self, value: DataType, /) -> None:
53-
if isinstance(value, list | tuple):
53+
if isinstance(value, list | tuple) and not isinstance(value, Mapping):
5454
self.value = value
5555
else:
5656
self.value = [value]

0 commit comments

Comments
 (0)