Skip to content

Commit 5fe3ffc

Browse files
serhiyeccles
authored andcommitted
Fix type hinting to match Pylance "basic" type checking
Problem: The "Basic" type checking level from Pylance notified multiple errors Solution: Add the correct type hinting, and ignore type comments to deal with all Pylance errors Signed off: Serhiy <serhiy1@live.co.uk>
1 parent 3b8c756 commit 5fe3ffc

10 files changed

Lines changed: 130 additions & 89 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ htmlcov/
1212
.cache/
1313
.eggs/
1414
.task/
15+
*.pyi
16+
.vscode

Taskfile.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tasks:
2323
desc: Build a docker environment with the right dependencies and utilities
2424
cmds:
2525
- docker build --no-cache --build-arg VERSION=3.8 -f Dockerfile-builder -t jitsuin-archivist-python-builder .
26-
26+
2727
builder-3.9:
2828
desc: Build a docker environment with the right dependencies and utilities
2929
cmds:
@@ -46,23 +46,23 @@ tasks:
4646
docs:
4747
desc: Create sphinx documentation
4848
deps: [about]
49-
cmds:
49+
cmds:
5050
- ./scripts/builder.sh /bin/bash -c "cd docs && make html"
5151

5252
format:
5353
desc: Format code using black
5454
deps: [about]
55-
cmds:
55+
cmds:
5656
- ./scripts/builder.sh black archivist examples functests unittests
5757

5858
functests:
5959
desc: Run functests - requires an archivist instance and a authtoken
6060
deps: [about]
6161
cmds:
6262
- ./scripts/builder.sh ./scripts/functests.sh
63-
63+
6464
publish:
65-
desc: pubish wheel package (will require username and password)
65+
desc: publish wheel package (will require username and password)
6666
deps: [about]
6767
cmds:
6868
- ./scripts/builder.sh python3 -m twine upload --repository pypi dist/*
@@ -72,12 +72,12 @@ tasks:
7272
deps: [about]
7373
cmds:
7474
- ./scripts/builder.sh ./scripts/unittests.sh
75-
75+
7676
wheel:
7777
desc: Builds python wheel package
7878
deps: [about]
7979
cmds:
8080
- rm -rf *egg-info
81-
- rm -rf build
81+
- rm -rf build
8282
- rm -f dist/*
8383
- python3 setup.py bdist_wheel

archivist/access_policies.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
"""
2323

24-
from typing import List, Optional
24+
from typing import Dict, List, Optional
2525
import logging
2626
from copy import deepcopy
2727

@@ -65,7 +65,7 @@ def __init__(self, archivist: "type_helper.Archivist"):
6565
self._archivist = archivist
6666

6767
def create(
68-
self, props: dict, filters: List, access_permissions: List
68+
self, props: Dict, filters: List, access_permissions: List
6969
) -> AccessPolicy:
7070
"""Create access policy
7171
@@ -85,7 +85,7 @@ def create(
8585
self.__query(props, filters=filters, access_permissions=access_permissions),
8686
)
8787

88-
def create_from_data(self, data: dict) -> AccessPolicy:
88+
def create_from_data(self, data: Dict) -> AccessPolicy:
8989
"""Create access policy
9090
9191
Creates access policy with request body from data stream.
@@ -127,9 +127,9 @@ def read(self, identity: str) -> AccessPolicy:
127127
def update(
128128
self,
129129
identity,
130-
props: Optional[dict] = None,
131-
filters: Optional[list] = None,
132-
access_permissions: Optional[list] = None,
130+
props: Optional[Dict] = None,
131+
filters: Optional[List] = None,
132+
access_permissions: Optional[List] = None,
133133
) -> AccessPolicy:
134134
"""Update Access Policy
135135
@@ -155,7 +155,7 @@ def update(
155155
)
156156
)
157157

158-
def delete(self, identity: str) -> dict:
158+
def delete(self, identity: str) -> Dict:
159159
"""Delete Access Policy
160160
161161
Deletes access policy.
@@ -170,7 +170,12 @@ def delete(self, identity: str) -> dict:
170170
return self._archivist.delete(ACCESS_POLICIES_SUBPATH, identity)
171171

172172
@staticmethod
173-
def __query(props, *, filters=None, access_permissions=None):
173+
def __query(
174+
props: Optional[Dict],
175+
*,
176+
filters: Optional[List] = None,
177+
access_permissions: Optional[List] = None,
178+
) -> Dict:
174179
query = deepcopy(props) if props else {}
175180
if filters is not None:
176181
query["filters"] = filters

archivist/archivist.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import json
3535
from os.path import isfile as os_path_isfile
36-
from typing import IO, Optional
36+
from typing import BinaryIO, Dict, Optional
3737
from requests.models import Response
3838

3939
from flatten_dict import flatten
@@ -142,7 +142,7 @@ def __getattr__(self, value: str):
142142
return c
143143

144144
@property
145-
def headers(self) -> dict:
145+
def headers(self) -> Dict:
146146
"""dict: Headers REST headers from response"""
147147
return self._headers
148148

@@ -157,11 +157,11 @@ def verify(self) -> bool:
157157
return self._verify
158158

159159
@property
160-
def cert(self) -> str:
160+
def cert(self) -> Optional[str]:
161161
"""str: filepath containing authorisation certificate."""
162162
return self._cert
163163

164-
def __add_headers(self, headers):
164+
def __add_headers(self, headers: Optional[Dict]) -> Dict:
165165
"""docstring"""
166166
if headers is not None:
167167
newheaders = {**self.headers, **headers}
@@ -171,8 +171,8 @@ def __add_headers(self, headers):
171171
return newheaders
172172

173173
def get(
174-
self, subpath: str, identity: str, *, headers: Optional[dict] = None
175-
) -> dict:
174+
self, subpath: str, identity: str, *, headers: Optional[Dict] = None
175+
) -> Dict:
176176
"""GET method (REST)
177177
178178
Args:
@@ -198,7 +198,12 @@ def get(
198198
return response.json()
199199

200200
def get_file(
201-
self, subpath: str, identity: str, fd: IO, *, headers: Optional[dict] = None
201+
self,
202+
subpath: str,
203+
identity: str,
204+
fd: BinaryIO,
205+
*,
206+
headers: Optional[Dict] = None,
202207
) -> Response:
203208
"""GET method (REST) - chunked
204209
@@ -232,7 +237,7 @@ def get_file(
232237

233238
return response
234239

235-
def post(self, path: str, request: dict, *, headers: Optional[dict] = None) -> dict:
240+
def post(self, path: str, request: Dict, *, headers: Optional[Dict] = None) -> Dict:
236241
"""POST method (REST)
237242
238243
Creates an entity
@@ -261,7 +266,7 @@ def post(self, path: str, request: dict, *, headers: Optional[dict] = None) -> d
261266

262267
return response.json()
263268

264-
def post_file(self, path: str, fd: IO, mtype: str) -> dict:
269+
def post_file(self, path: str, fd: BinaryIO, mtype: str) -> Dict:
265270
"""POST method (REST) - upload binary
266271
267272
Uploads a file to an endpoint
@@ -286,7 +291,7 @@ def post_file(self, path: str, fd: IO, mtype: str) -> dict:
286291
}
287292
response = requests.post(
288293
SEP.join((self.url, ROOT, path)),
289-
data=multipart,
294+
data=multipart, # type: ignore https://github.com/requests/toolbelt/issues/312
290295
headers=self.__add_headers(headers),
291296
verify=self.verify,
292297
cert=self.cert,
@@ -299,8 +304,8 @@ def post_file(self, path: str, fd: IO, mtype: str) -> dict:
299304
return response.json()
300305

301306
def delete(
302-
self, subpath: str, identity: str, *, headers: Optional[dict] = None
303-
) -> dict:
307+
self, subpath: str, identity: str, *, headers: Optional[Dict] = None
308+
) -> Dict:
304309
"""DELETE method (REST)
305310
306311
Deletes an entity
@@ -331,10 +336,10 @@ def patch(
331336
self,
332337
subpath: str,
333338
identity: str,
334-
request: dict,
339+
request: Dict,
335340
*,
336-
headers: Optional[dict] = None,
337-
) -> dict:
341+
headers: Optional[Dict] = None,
342+
) -> Dict:
338343
"""PATCH method (REST)
339344
340345
Updates the specified entity.
@@ -364,7 +369,7 @@ def patch(
364369

365370
return response.json()
366371

367-
def __list(self, path, args, *, headers=None):
372+
def __list(self, path, args, *, headers=None) -> Response:
368373
if args:
369374
path = "?".join((path, args))
370375

@@ -381,14 +386,14 @@ def __list(self, path, args, *, headers=None):
381386
return response
382387

383388
@staticmethod
384-
def __query(query):
389+
def __query(query: Optional[Dict]):
385390
return query and "&".join(
386391
sorted(f"{k}={v}" for k, v in flatten(query, reducer="dot").items())
387392
)
388393

389394
def get_by_signature(
390-
self, path: str, field: str, query: dict, *, headers: Optional[dict] = None
391-
) -> dict:
395+
self, path: str, field: str, query: Dict, *, headers: Optional[Dict] = None
396+
) -> Dict:
392397
"""GET method (REST) with query string
393398
394399
Reads an entity indirectly by searching for its signature
@@ -417,7 +422,7 @@ def get_by_signature(
417422

418423
response = self.__list(
419424
path,
420-
"&".join((a for a in (paging, qry) if a)),
425+
"&".join((a for a in (paging, qry) if a)), # type: ignore
421426
headers=headers,
422427
)
423428

@@ -436,7 +441,7 @@ def get_by_signature(
436441

437442
return records[0]
438443

439-
def count(self, path: str, *, query: Optional[dict] = None) -> int:
444+
def count(self, path: str, *, query: Optional[Dict] = None) -> int:
440445
"""GET method (REST) with query string
441446
442447
Returns the count of objects that match query
@@ -456,7 +461,7 @@ def count(self, path: str, *, query: Optional[dict] = None) -> int:
456461

457462
response = self.__list(
458463
path,
459-
"&".join((a for a in (paging, qry) if a)),
464+
"&".join((a for a in (paging, qry) if a)), # type: ignore
460465
headers=headers,
461466
)
462467

@@ -467,9 +472,9 @@ def list(
467472
path: str,
468473
field: str,
469474
*,
470-
page_size: Optional[int] = None,
471-
query: Optional[dict] = None,
472-
headers: Optional[dict] = None,
475+
page_size: int = None,
476+
query: Optional[Dict] = None,
477+
headers: Optional[Dict] = None,
473478
):
474479
"""GET method (REST) with query string
475480
@@ -502,7 +507,7 @@ def list(
502507
while True:
503508
response = self.__list(
504509
path,
505-
"&".join((a for a in (paging, qry) if a)),
510+
"&".join((a for a in (paging, qry) if a)), # type: ignore
506511
headers=headers,
507512
)
508513
data = response.json()

0 commit comments

Comments
 (0)