Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 06d36da

Browse files
authored
Merge pull request #86 from hozan23/general-clean-up
WIP: major clean ups for the codebase
2 parents 909715a + ddfb382 commit 06d36da

6 files changed

Lines changed: 155 additions & 103 deletions

File tree

deta/__init__.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
import urllib.error
33
import urllib.request
44
import json
5+
from typing import Union
56

67
from .base import _Base
78
from .drive import _Drive
89
from .utils import _get_project_key_id
910

1011

1112
try:
12-
from detalib.app import App
13+
from detalib.app import App # pyright: ignore
1314

1415
app = App()
1516
except Exception:
1617
pass
1718

1819
try:
19-
from ._async.client import AsyncBase
20+
from ._async.client import AsyncBase # pyright: ignore
2021
except ImportError:
2122
pass
2223

2324
__version__ = "1.2.0"
2425

25-
2626
def Base(name: str):
2727
project_key, project_id = _get_project_key_id()
2828
return _Base(name, project_key, project_id)
@@ -34,20 +34,20 @@ def Drive(name: str):
3434

3535

3636
class Deta:
37-
def __init__(self, project_key: str = None, *, project_id: str = None):
37+
def __init__(self, project_key: Union[str, None] = None, *, project_id: Union[str, None] = None):
3838
project_key, project_id = _get_project_key_id(project_key, project_id)
3939
self.project_key = project_key
4040
self.project_id = project_id
4141

42-
def Base(self, name: str, host: str = None):
42+
def Base(self, name: str, host: Union[str, None] = None):
4343
return _Base(name, self.project_key, self.project_id, host)
4444

45-
def AsyncBase(self, name: str, host: str = None):
45+
def AsyncBase(self, name: str, host: Union[str, None] = None):
4646
from ._async.client import _AsyncBase
4747

4848
return _AsyncBase(name, self.project_key, self.project_id, host)
4949

50-
def Drive(self, name: str, host: str = None):
50+
def Drive(self, name: str, host: Union[str, None] = None):
5151
return _Drive(
5252
name=name,
5353
project_key=self.project_key,
@@ -73,9 +73,12 @@ def send_email(to, subject, message, charset="UTF-8"):
7373
"charset": charset,
7474
}
7575

76+
assert api_key
77+
7678
headers = {"X-API-Key": api_key}
7779

78-
req = urllib.request.Request(endpoint, json.dumps(data).encode("utf-8"), headers)
80+
req = urllib.request.Request(
81+
endpoint, json.dumps(data).encode("utf-8"), headers)
7982

8083
try:
8184
resp = urllib.request.urlopen(req)

deta/_async/client.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import typing
2-
1+
from typing import Union, List
32
import datetime
43
import os
5-
import aiohttp
64
from urllib.parse import quote
75

6+
import aiohttp
7+
88
from deta.utils import _get_project_key_id
99
from deta.base import FetchResponse, Util, insert_ttl, BASE_TTL_ATTTRIBUTE
1010

@@ -15,7 +15,7 @@ def AsyncBase(name: str):
1515

1616

1717
class _AsyncBase:
18-
def __init__(self, name: str, project_key: str, project_id: str, host: str = None):
18+
def __init__(self, name: str, project_key: str, project_id: str, host: Union[str, None] = None):
1919
if not project_key:
2020
raise AssertionError("No Base name provided")
2121

@@ -56,11 +56,11 @@ async def delete(self, key: str):
5656

5757
async def insert(
5858
self,
59-
data: typing.Union[dict, list, str, int, bool],
60-
key: str = None,
59+
data: Union[dict, list, str, int, bool],
60+
key: Union[str, None] = None,
6161
*,
62-
expire_in: int = None,
63-
expire_at: typing.Union[int, float, datetime.datetime] = None,
62+
expire_in: Union[int, None] = None,
63+
expire_at: Union[int, float, datetime.datetime, None] = None,
6464
):
6565
if not isinstance(data, dict):
6666
data = {"value": data}
@@ -70,19 +70,20 @@ async def insert(
7070
if key:
7171
data["key"] = key
7272

73-
insert_ttl(data, self.__ttl_attribute, expire_in=expire_in, expire_at=expire_at)
73+
insert_ttl(data, self.__ttl_attribute,
74+
expire_in=expire_in, expire_at=expire_at)
7475
async with self._session.post(
7576
f"{self._base_url}/items", json={"item": data}
7677
) as resp:
7778
return await resp.json()
7879

7980
async def put(
8081
self,
81-
data: typing.Union[dict, list, str, int, bool],
82-
key: str = None,
82+
data: Union[dict, list, str, int, bool],
83+
key: Union[str, None] = None,
8384
*,
84-
expire_in: int = None,
85-
expire_at: typing.Union[int, float, datetime.datetime] = None,
85+
expire_in: Union[int, None] = None,
86+
expire_at: Union[int, float, datetime.datetime, None] = None,
8687
):
8788
if not isinstance(data, dict):
8889
data = {"value": data}
@@ -92,8 +93,12 @@ async def put(
9293
if key:
9394
data["key"] = key
9495

95-
insert_ttl(data, self.__ttl_attribute, expire_in=expire_in, expire_at=expire_at)
96-
async with self._session.put(f"{self._base_url}/items", json={"items": [data]}) as resp:
96+
97+
insert_ttl(data, self.__ttl_attribute,
98+
expire_in=expire_in, expire_at=expire_at)
99+
async with self._session.put(
100+
f"{self._base_url}/items", json={"items": [data]}
101+
) as resp:
97102
if resp.status == 207:
98103
resp_json = await resp.json()
99104
if "processed" in resp_json:
@@ -102,10 +107,10 @@ async def put(
102107

103108
async def put_many(
104109
self,
105-
items: typing.List[typing.Union[dict, list, str, int, bool]],
110+
items: List[Union[dict, list, str, int, bool]],
106111
*,
107-
expire_in: int = None,
108-
expire_at: typing.Union[int, float, datetime.datetime] = None,
112+
expire_in: Union[int, None] = None,
113+
expire_at: Union[int, float, datetime.datetime, None] = None,
109114
):
110115
if len(items) > 25:
111116
raise AssertionError("We can't put more than 25 items at a time.")
@@ -126,10 +131,10 @@ async def put_many(
126131

127132
async def fetch(
128133
self,
129-
query: typing.Union[dict, list] = None,
134+
query: Union[dict, list, None] = None,
130135
*,
131136
limit: int = 1000,
132-
last: str = None,
137+
last: Union[str, None] = None,
133138
desc: bool = False,
134139
):
135140
payload = {}
@@ -154,8 +159,8 @@ async def update(
154159
updates: dict,
155160
key: str,
156161
*,
157-
expire_in: int = None,
158-
expire_at: typing.Union[int, float, datetime.datetime] = None,
162+
expire_in: Union[int, None] = None,
163+
expire_at: Union[int, float, datetime.datetime, None] = None,
159164
):
160165
if key == "":
161166
raise ValueError("Key is empty")

deta/base.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import datetime
3-
from re import I
4-
import typing
3+
from typing import Union, List
54
from urllib.parse import quote
65

76
from .service import _Service, JSON_MIME
@@ -62,18 +61,18 @@ def __init__(self, value):
6261
def trim(self):
6362
return self.Trim()
6463

65-
def increment(self, value: typing.Union[int, float] = None):
64+
def increment(self, value: Union[int, float, None] = None):
6665
return self.Increment(value)
6766

68-
def append(self, value: typing.Union[dict, list, str, int, float, bool]):
67+
def append(self, value: Union[dict, list, str, int, float, bool]):
6968
return self.Append(value)
7069

71-
def prepend(self, value: typing.Union[dict, list, str, int, float, bool]):
70+
def prepend(self, value: Union[dict, list, str, int, float, bool]):
7271
return self.Prepend(value)
7372

7473

7574
class _Base(_Service):
76-
def __init__(self, name: str, project_key: str, project_id: str, host: str = None):
75+
def __init__(self, name: str, project_key: str, project_id: str, host: Union[str, None] = None):
7776
assert name, "No Base name provided"
7877

7978
host = host or os.getenv("DETA_BASE_HOST") or "database.deta.sh"
@@ -110,11 +109,11 @@ def delete(self, key: str):
110109

111110
def insert(
112111
self,
113-
data: typing.Union[dict, list, str, int, bool],
114-
key: str = None,
112+
data: Union[dict, list, str, int, bool],
113+
key: Union[str, None] = None,
115114
*,
116-
expire_in: int = None,
117-
expire_at: typing.Union[int, float, datetime.datetime] = None,
115+
expire_in: Union[int, None] = None,
116+
expire_at: Union[int, float, datetime.datetime, None] = None,
118117
):
119118
if not isinstance(data, dict):
120119
data = {"value": data}
@@ -124,7 +123,8 @@ def insert(
124123
if key:
125124
data["key"] = key
126125

127-
insert_ttl(data, self.__ttl_attribute, expire_in=expire_in, expire_at=expire_at)
126+
insert_ttl(data, self.__ttl_attribute,
127+
expire_in=expire_in, expire_at=expire_at)
128128
code, res = self._request(
129129
"/items", "POST", {"item": data}, content_type=JSON_MIME
130130
)
@@ -135,11 +135,11 @@ def insert(
135135

136136
def put(
137137
self,
138-
data: typing.Union[dict, list, str, int, bool],
139-
key: str = None,
138+
data: Union[dict, list, str, int, bool],
139+
key: Union[str, None] = None,
140140
*,
141-
expire_in: int = None,
142-
expire_at: typing.Union[int, float, datetime.datetime] = None,
141+
expire_in: Union[int, None] = None,
142+
expire_at: Union[int, float, datetime.datetime, None] = None,
143143
):
144144
"""store (put) an item in the database. Overrides an item if key already exists.
145145
`key` could be provided as function argument or a field in the data dict.
@@ -154,22 +154,24 @@ def put(
154154
if key:
155155
data["key"] = key
156156

157-
insert_ttl(data, self.__ttl_attribute, expire_in=expire_in, expire_at=expire_at)
157+
insert_ttl(data, self.__ttl_attribute,
158+
expire_in=expire_in, expire_at=expire_at)
158159
code, res = self._request(
159160
"/items", "PUT", {"items": [data]}, content_type=JSON_MIME
160161
)
161162

163+
162164
if code == 207 and "processed" in res:
163165
return res["processed"]["items"][0]
164166
else:
165167
return None
166168

167169
def put_many(
168170
self,
169-
items: typing.List[typing.Union[dict, list, str, int, bool]],
171+
items: List[Union[dict, list, str, int, bool]],
170172
*,
171-
expire_in: int = None,
172-
expire_at: typing.Union[int, float, datetime.datetime] = None,
173+
expire_in: Union[int, None] = None,
174+
expire_at: Union[int, float, datetime.datetime, None] = None,
173175
):
174176
assert len(items) <= 25, "We can't put more than 25 items at a time."
175177
_items = []
@@ -189,9 +191,9 @@ def put_many(
189191

190192
def _fetch(
191193
self,
192-
query: typing.Union[dict, list] = None,
193-
buffer: int = None,
194-
last: str = None,
194+
query: Union[dict, list, None] = None,
195+
buffer: Union[int, None] = None,
196+
last: Union[str, None] = None,
195197
desc: bool = False,
196198
) -> typing.Optional[typing.Tuple[int, list]]:
197199
"""This is where actual fetch happens."""
@@ -204,34 +206,43 @@ def _fetch(
204206
if query:
205207
payload["query"] = query if isinstance(query, list) else [query]
206208

207-
code, res = self._request("/query", "POST", payload, content_type=JSON_MIME)
208-
return code, res
209+
_, res = self._request(
210+
"/query", "POST", payload, content_type=JSON_MIME)
211+
212+
assert res
213+
214+
return res
209215

210216
def fetch(
211217
self,
212-
query: typing.Union[dict, list] = None,
218+
query: Union[dict, list, None] = None,
213219
*,
214220
limit: int = 1000,
215-
last: str = None,
221+
last: Union[str, None] = None,
216222
desc: bool = False,
223+
217224
):
218225
"""
219226
fetch items from the database.
220227
`query` is an optional filter or list of filters. Without filter, it will return the whole db.
221228
"""
229+
222230
_, res = self._fetch(query, limit, last, desc)
223231

224-
paging = res.get("paging")
225232

226-
return FetchResponse(paging.get("size"), paging.get("last"), res.get("items"))
233+
paging = res.get("paging") # pyright: ignore
234+
235+
return FetchResponse(paging.get("size"),
236+
paging.get("last"),
237+
res.get("items")) # pyright: ignore
227238

228239
def update(
229240
self,
230241
updates: dict,
231242
key: str,
232243
*,
233-
expire_in: int = None,
234-
expire_at: typing.Union[int, float, datetime.datetime] = None,
244+
expire_in: Union[int, None] = None,
245+
expire_at: Union[int, float, datetime.datetime, None] = None,
235246
):
236247
"""
237248
update an item in the database

0 commit comments

Comments
 (0)