From a25edecc884433f991e7bea02e97614a5a97f75a Mon Sep 17 00:00:00 2001 From: Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> Date: Tue, 29 Oct 2024 02:05:54 -0700 Subject: [PATCH] Enable strict_optional for aqt/studydeck, tts, mediasrv (#3542) * Enable strict_optional for studydeck * Fix mypy errors * Enable strict_optional for tts * Fix mypy errors in tts * Enable strict_optional for mediasrv * Fix mypy errors in mediasrv --- .mypy.ini | 6 ++++++ qt/aqt/mediasrv.py | 8 ++++++-- qt/aqt/studydeck.py | 3 ++- qt/aqt/tts.py | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.mypy.ini b/.mypy.ini index 818bcfb26..96483e1cc 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -56,6 +56,12 @@ strict_optional = True strict_optional = True [mypy-aqt.deckbrowser] strict_optional = True +[mypy-aqt.studydeck] +strict_optional = True +[mypy-aqt.tts] +strict_optional = True +[mypy-aqt.mediasrv] +strict_optional = True [mypy-anki.scheduler.base] strict_optional = True [mypy-anki._backend.rsbridge] diff --git a/qt/aqt/mediasrv.py b/qt/aqt/mediasrv.py index ca4f23503..15d9a0fd1 100644 --- a/qt/aqt/mediasrv.py +++ b/qt/aqt/mediasrv.py @@ -735,8 +735,12 @@ def _extract_page_context() -> PageContext: return PageContext.NON_LEGACY_PAGE elif referer.path == "/_anki/legacyPageData": query_params = parse_qs(referer.query) - id = int(query_params.get("id", [None])[0]) - return aqt.mw.mediaServer.get_page_context(id) + query_id = query_params.get("id") + if not query_id: + return PageContext.UNKNOWN + id = int(query_id[0]) + page_context = aqt.mw.mediaServer.get_page_context(id) + return page_context if page_context else PageContext.UNKNOWN else: return PageContext.UNKNOWN diff --git a/qt/aqt/studydeck.py b/qt/aqt/studydeck.py index 537c7d151..d1c0a5c47 100644 --- a/qt/aqt/studydeck.py +++ b/qt/aqt/studydeck.py @@ -101,7 +101,7 @@ class StudyDeck(QDialog): else: self.exec() - def eventFilter(self, obj: QObject, evt: QEvent) -> bool: + def eventFilter(self, obj: QObject | None, evt: QEvent | None) -> bool: if isinstance(evt, QKeyEvent) and evt.type() == QEvent.Type.KeyPress: new_row = current_row = self.form.list.currentRow() rows_count = self.form.list.count() @@ -178,6 +178,7 @@ class StudyDeck(QDialog): def success(out: OpChangesWithId) -> None: deck = self.mw.col.decks.get(DeckId(out.id)) + assert deck is not None self.name = deck["name"] self.accept_with_callback() diff --git a/qt/aqt/tts.py b/qt/aqt/tts.py index cd2884795..3cd0e5ddf 100644 --- a/qt/aqt/tts.py +++ b/qt/aqt/tts.py @@ -189,6 +189,7 @@ class MacTTSPlayer(TTSProcessPlayer): stderr=subprocess.DEVNULL, ) # write the input text to stdin + assert self._process.stdin is not None self._process.stdin.write(tag.field_text.encode("utf8")) self._process.stdin.close() self._wait_for_termination(tag) @@ -247,6 +248,7 @@ class MacTTSFilePlayer(MacTTSPlayer): stderr=subprocess.DEVNULL, ) # write the input text to stdin + assert self._process.stdin is not None self._process.stdin.write(tag.field_text.encode("utf8")) self._process.stdin.close() self._wait_for_termination(tag)