From 8b84583433d6400e64359dccfe1e2edde3df3e34 Mon Sep 17 00:00:00 2001 From: Ben Nguyen <105088397+bpnguyen107@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:40:40 -0700 Subject: [PATCH] Enable strict_optional for aqt/clayout, changenotetype, fields (#3544) * Enable strict_optional for clayout * Fix mypy errors * Enable strict_optional for changenotetype * Fix mypy errors * Enable strict_optional for fields * Fix mypy errors --- .mypy.ini | 6 ++++++ qt/aqt/changenotetype.py | 2 +- qt/aqt/clayout.py | 26 ++++++++++++++++++++------ qt/aqt/fields.py | 24 +++++++++++++++++------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/.mypy.ini b/.mypy.ini index 96483e1cc..524a62168 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -62,6 +62,12 @@ strict_optional = True strict_optional = True [mypy-aqt.mediasrv] strict_optional = True +[mypy-aqt.changenotetype] +strict_optional = True +[mypy-aqt.clayout] +strict_optional = True +[mypy-aqt.fields] +strict_optional = True [mypy-anki.scheduler.base] strict_optional = True [mypy-anki._backend.rsbridge] diff --git a/qt/aqt/changenotetype.py b/qt/aqt/changenotetype.py index 0484c2ff8..3ab9a87e8 100644 --- a/qt/aqt/changenotetype.py +++ b/qt/aqt/changenotetype.py @@ -63,7 +63,7 @@ class ChangeNotetypeDialog(QDialog): def reject(self) -> None: self.web.cleanup() - self.web = None + self.web = None # type: ignore saveGeom(self, self.TITLE) QDialog.reject(self) diff --git a/qt/aqt/clayout.py b/qt/aqt/clayout.py index bdd7f02d3..8f467e379 100644 --- a/qt/aqt/clayout.py +++ b/qt/aqt/clayout.py @@ -61,7 +61,9 @@ class CardLayout(QDialog): self.ord = ord self.col = self.mw.col.weakref() self.mm = self.mw.col.models - self.model = note.note_type() + note_type = note.note_type() + assert note_type is not None + self.model = note_type self.templates = self.model["tmpls"] self.fill_empty_action_toggled = fill_empty self.night_mode_is_enabled = theme_manager.night_mode @@ -404,6 +406,7 @@ class CardLayout(QDialog): m = QMenu(self) a = m.addAction(tr.card_templates_fill_empty()) + assert a is not None a.setCheckable(True) a.setChecked(self.fill_empty_action_toggled) qconnect(a.triggered, self.on_fill_empty_action_toggled) @@ -411,11 +414,13 @@ class CardLayout(QDialog): a.setVisible(False) a = m.addAction(tr.card_templates_night_mode()) + assert a is not None a.setCheckable(True) a.setChecked(self.night_mode_is_enabled) qconnect(a.triggered, self.on_night_mode_action_toggled) a = m.addAction(tr.card_templates_add_mobile_class()) + assert a is not None a.setCheckable(True) a.setChecked(self.mobile_emulation_enabled) qconnect(a.toggled, self.on_mobile_class_action_toggled) @@ -754,6 +759,7 @@ class CardLayout(QDialog): a = m.addAction( tr.actions_with_ellipsis(action=tr.card_templates_restore_to_default()) ) + assert a is not None qconnect( a.triggered, lambda: self.on_restore_to_default(), # pylint: disable=unnecessary-lambda @@ -761,15 +767,19 @@ class CardLayout(QDialog): if not self._isCloze(): a = m.addAction(tr.card_templates_add_card_type()) + assert a is not None qconnect(a.triggered, self.onAddCard) a = m.addAction(tr.card_templates_remove_card_type()) + assert a is not None qconnect(a.triggered, self.onRemove) a = m.addAction(tr.card_templates_rename_card_type()) + assert a is not None qconnect(a.triggered, self.onRename) a = m.addAction(tr.card_templates_reposition_card_type()) + assert a is not None qconnect(a.triggered, self.onReorder) m.addSeparator() @@ -780,9 +790,11 @@ class CardLayout(QDialog): else: s = tr.card_templates_off() a = m.addAction(tr.card_templates_deck_override() + s) + assert a is not None qconnect(a.triggered, self.onTargetDeck) a = m.addAction(tr.card_templates_browser_appearance()) + assert a is not None qconnect(a.triggered, self.onBrowserDisplay) m.popup(self.topAreaForm.templateOptions.mapToGlobal(QPoint(0, 0))) @@ -834,7 +846,9 @@ class CardLayout(QDialog): te.setCol(self.col) l.addWidget(te) if t["did"]: - te.setText(self.col.decks.get(t["did"])["name"]) + deck = self.col.decks.get(t["did"]) + assert deck is not None + te.setText(deck["name"]) te.selectAll() bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Close) qconnect(bb.rejected, d.close) @@ -927,10 +941,10 @@ class CardLayout(QDialog): saveGeom(self, "CardLayout") saveSplitter(self.mainArea, "CardLayoutMainArea") self.preview_web.cleanup() - self.preview_web = None - self.model = None - self.rendered_card = None - self.mw = None + self.preview_web = None # type: ignore + self.model = None # type: ignore + self.rendered_card = None # type: ignore + self.mw = None # type: ignore def onHelp(self) -> None: openHelp(HelpPage.TEMPLATES) diff --git a/qt/aqt/fields.py b/qt/aqt/fields.py index da81c9ff7..0a570239e 100644 --- a/qt/aqt/fields.py +++ b/qt/aqt/fields.py @@ -51,15 +51,20 @@ class FieldDialog(QDialog): self.webview = None disable_help_button(self) - self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help).setAutoDefault( - False - ) - self.form.buttonBox.button( + help_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Help) + assert help_button is not None + help_button.setAutoDefault(False) + + cancel_button = self.form.buttonBox.button( QDialogButtonBox.StandardButton.Cancel - ).setAutoDefault(False) - self.form.buttonBox.button(QDialogButtonBox.StandardButton.Save).setAutoDefault( - False ) + assert cancel_button is not None + cancel_button.setAutoDefault(False) + + save_button = self.form.buttonBox.button(QDialogButtonBox.StandardButton.Save) + assert save_button is not None + save_button.setAutoDefault(False) + self.currentIdx: int | None = None self.fillFields() self.setupSignals() @@ -112,6 +117,7 @@ class FieldDialog(QDialog): # for pylint return # the item in idx is removed thus subtract 1. + assert idx is not None if idx < dropPos: movePos -= 1 self.moveField(movePos + 1) # convert to 1 based. @@ -144,6 +150,9 @@ class FieldDialog(QDialog): return txt def onRename(self) -> None: + if self.currentIdx is None: + return + idx = self.currentIdx f = self.model["flds"][idx] name = self._uniqueName(tr.actions_new_name(), self.currentIdx, f["name"]) @@ -195,6 +204,7 @@ class FieldDialog(QDialog): def onPosition(self, delta: int = -1) -> None: idx = self.currentIdx + assert idx is not None l = len(self.model["flds"]) txt = getOnlyText(tr.fields_new_position_1(val=l), default=str(idx + 1)) if not txt: