Skip to content

Commit 60bd355

Browse files
committed
Fix typing errors found by ty
1 parent 4896975 commit 60bd355

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ quote-style = "double"
116116

117117
# Like Black, indent with spaces, rather than tabs.
118118
indent-style = "space"
119+
120+
[tool.ty.rules]
121+
possibly-missing-attribute = "ignore"

src/pyzotero/zotero.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ def set_fulltext(self, itemkey, payload):
698698
f"/{self.library_type}/{self.library_id}/items/{itemkey}/fulltext",
699699
),
700700
headers=headers,
701-
data=json.dumps(payload),
701+
json=payload,
702702
)
703703

704704
def new_fulltext(self, since):
@@ -926,6 +926,9 @@ def iterfollow(self):
926926

927927
def makeiter(self, func):
928928
"""Return a generator of func's results"""
929+
if self.links is None or "self" not in self.links:
930+
msg = "makeiter() requires a previous API call with pagination links"
931+
raise RuntimeError(msg)
929932
# reset the link. This results in an extra API call, yes
930933
self.links["next"] = self.links["self"]
931934
return self.iterfollow()
@@ -1082,7 +1085,7 @@ def saved_search(self, name, conditions):
10821085
f"/{self.library_type}/{self.library_id}/searches",
10831086
),
10841087
headers=headers,
1085-
data=json.dumps(payload),
1088+
json=payload,
10861089
)
10871090
self.request = req
10881091
try:
@@ -1306,14 +1309,14 @@ def create_items(self, payload, parentid=None, last_modified=None):
13061309
"If-Unmodified-Since-Version": req.headers["last-modified-version"],
13071310
}
13081311
for value in resp["success"].values():
1309-
payload = json.dumps({"parentItem": parentid})
1312+
payload = {"parentItem": parentid}
13101313
self._check_backoff()
13111314
presp = self.client.patch(
13121315
url=build_url(
13131316
self.endpoint,
13141317
f"/{self.library_type}/{self.library_id}/items/{value}",
13151318
),
1316-
data=payload,
1319+
json=payload,
13171320
headers=dict(uheaders),
13181321
)
13191322
self.request = presp
@@ -1452,7 +1455,7 @@ def update_items(self, payload):
14521455
self.endpoint,
14531456
f"/{self.library_type}/{self.library_id}/items/",
14541457
),
1455-
data=json.dumps(chunk),
1458+
json=chunk,
14561459
)
14571460
self.request = req
14581461
try:
@@ -1478,7 +1481,7 @@ def update_collections(self, payload):
14781481
self.endpoint,
14791482
f"/{self.library_type}/{self.library_id}/collections/",
14801483
),
1481-
data=json.dumps(chunk),
1484+
json=chunk,
14821485
)
14831486
self.request = req
14841487
try:
@@ -1506,7 +1509,7 @@ def addto_collection(self, collection, payload):
15061509
self.endpoint,
15071510
f"/{self.library_type}/{self.library_id}/items/{ident}",
15081511
),
1509-
data=json.dumps({"collections": modified_collections}),
1512+
json={"collections": modified_collections},
15101513
headers=headers,
15111514
)
15121515

@@ -1528,7 +1531,7 @@ def deletefrom_collection(self, collection, payload):
15281531
self.endpoint,
15291532
f"/{self.library_type}/{self.library_id}/items/{ident}",
15301533
),
1531-
data=json.dumps({"collections": modified_collections}),
1534+
json={"collections": modified_collections},
15321535
headers=headers,
15331536
)
15341537

@@ -1646,14 +1649,14 @@ def err_msg(req):
16461649
delay = req.headers.get("backoff") or req.headers.get("retry-after")
16471650
if not delay:
16481651
msg = "You are being rate-limited and no backoff or retry duration has been received from the server. Try again later"
1649-
raise ze.TooManyRetriesError(
1650-
msg,
1651-
)
1652+
raise ze.TooManyRetriesError(msg)
16521653
zot._set_backoff(delay)
16531654
elif not exc:
1654-
raise error_codes.get(req.status_code)(err_msg(req))
1655+
raise error_codes[req.status_code](err_msg(req))
16551656
else:
1656-
raise error_codes.get(req.status_code)(err_msg(req)) from exc
1657+
raise error_codes[req.status_code](
1658+
err_msg(req)
1659+
) from exc # ← Direct indexing
16571660
elif not exc:
16581661
raise ze.HTTPError(err_msg(req))
16591662
else:
@@ -1816,9 +1819,14 @@ def _validate(self, conditions):
18161819
permitted_operators = self.conditions_operators.get(
18171820
condition.get("condition"),
18181821
)
1822+
if permitted_operators is None:
1823+
msg = f"Unknown condition: {condition.get('condition')}"
1824+
raise ze.ParamNotPassedError(msg)
18191825
# transform these into values
18201826
permitted_operators_list = {
1821-
self.operators.get(op) for op in permitted_operators
1827+
op_value
1828+
for op in permitted_operators
1829+
if (op_value := self.operators.get(op)) is not None
18221830
}
18231831
if condition.get("operator") not in permitted_operators_list:
18241832
msg = f"You may not use the '{condition.get('operator')}' operator when selecting the '{condition.get('condition')}' condition. \nAllowed operators: {', '.join(list(permitted_operators_list))}"
@@ -1979,7 +1987,7 @@ def _upload_file(self, authdata, attachment, reg_key):
19791987
files=upload_pairs,
19801988
headers={"User-Agent": f"Pyzotero/{pz.__version__}"},
19811989
)
1982-
except httpx.ConnectionError:
1990+
except httpx.ConnectError:
19831991
msg = "ConnectionError"
19841992
raise ze.UploadError(msg) from None
19851993
try:
@@ -2016,7 +2024,7 @@ def _register_upload(self, authdata, reg_key):
20162024
"retry-after",
20172025
)
20182026
if backoff:
2019-
self._set_backoff(backoff)
2027+
self.zinstance._set_backoff(backoff)
20202028

20212029
def upload(self):
20222030
"""File upload functionality

0 commit comments

Comments
 (0)