Fix 'NoneType object is not subscriptable' error (#3286)

* fix: ensure none of the returned values is None

Fixes TypeError 'NoneType' object is not subscriptable

* refactor: reduce code duplication using a function

* refactor: prefer KeyError over None for dicts

If the key is not in the dictionary, we want to raise a KeyError rather
than returning None. That way, we can distinguish between whether the
value was None or the key was not found.

* chore: add myself to CONTRIBUTORS file

* refactor: simplify the code
This commit is contained in:
David Culley 2024-08-04 15:49:45 +02:00 committed by GitHub
parent 426790bfcb
commit a5a39c9302
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1114,11 +1114,14 @@ def extract_meta_from_download_url(url: str) -> ExtractedDownloadMeta:
urlobj = urlparse(url)
query = parse_qs(urlobj.query)
def get_first_element(elements: list[str]) -> int:
return int(elements[0])
meta = ExtractedDownloadMeta(
mod_time=int(query.get("t")[0]),
min_point_version=int(query.get("minpt")[0]),
max_point_version=int(query.get("maxpt")[0]),
branch_index=int(query.get("bidx")[0]),
mod_time=get_first_element(query["t"]),
min_point_version=get_first_element(query["minpt"]),
max_point_version=get_first_element(query["maxpt"]),
branch_index=get_first_element(query["bidx"]),
)
return meta