From a5a39c930282b2479d7d63fab450dacc76c6d7ef Mon Sep 17 00:00:00 2001 From: David Culley <6276049+davidculley@users.noreply.github.com> Date: Sun, 4 Aug 2024 15:49:45 +0200 Subject: [PATCH] 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 --- qt/aqt/addons.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/qt/aqt/addons.py b/qt/aqt/addons.py index b05a4f50e..cb7a242cf 100644 --- a/qt/aqt/addons.py +++ b/qt/aqt/addons.py @@ -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