Enable strict_optional for aqt/notetypechooser, stats, switch (#3558)

* Enable strict_optional for notetypechooser

* Fix mypy errors

* Enable strict_optional for stats

* Fix mypy errors

* Enable strict_optional for switch

* Fix mypy errors

---------

Co-authored-by: Abdo <abdo@abdnh.net>
This commit is contained in:
Ben Nguyen 2024-11-08 02:42:42 -08:00 committed by GitHub
parent 7f5e081014
commit a150eda287
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 8 deletions

View file

@ -74,6 +74,12 @@ strict_optional = True
strict_optional = True strict_optional = True
[mypy-aqt.deckoptions] [mypy-aqt.deckoptions]
strict_optional = True strict_optional = True
[mypy-aqt.notetypechooser]
strict_optional = True
[mypy-aqt.stats]
strict_optional = True
[mypy-aqt.switch]
strict_optional = True
[mypy-anki.scheduler.base] [mypy-anki.scheduler.base]
strict_optional = True strict_optional = True
[mypy-anki._backend.rsbridge] [mypy-anki._backend.rsbridge]

View file

@ -111,6 +111,7 @@ class NotetypeChooser(QHBoxLayout):
if not ret.name: if not ret.name:
return return
notetype = self.mw.col.models.by_name(ret.name) notetype = self.mw.col.models.by_name(ret.name)
assert notetype is not None
if (id := notetype["id"]) != self._selected_notetype_id: if (id := notetype["id"]) != self._selected_notetype_id:
self.selected_notetype_id = id self.selected_notetype_id = id
@ -146,7 +147,9 @@ class NotetypeChooser(QHBoxLayout):
func(self._selected_notetype_id) func(self._selected_notetype_id)
def selected_notetype_name(self) -> str: def selected_notetype_name(self) -> str:
return self.mw.col.models.get(self.selected_notetype_id)["name"] selected_notetype = self.mw.col.models.get(self.selected_notetype_id)
assert selected_notetype is not None
return selected_notetype["name"]
def _ensure_selected_notetype_valid(self) -> None: def _ensure_selected_notetype_valid(self) -> None:
if not self.mw.col.models.get(self._selected_notetype_id): if not self.mw.col.models.get(self._selected_notetype_id):

View file

@ -61,9 +61,11 @@ class NewDeckStats(QDialog):
b = f.buttonBox.addButton( b = f.buttonBox.addButton(
tr.statistics_save_pdf(), QDialogButtonBox.ButtonRole.ActionRole tr.statistics_save_pdf(), QDialogButtonBox.ButtonRole.ActionRole
) )
assert b is not None
qconnect(b.clicked, self.saveImage) qconnect(b.clicked, self.saveImage)
b.setAutoDefault(False) b.setAutoDefault(False)
b = f.buttonBox.button(QDialogButtonBox.StandardButton.Close) b = f.buttonBox.button(QDialogButtonBox.StandardButton.Close)
assert b is not None
b.setAutoDefault(False) b.setAutoDefault(False)
maybeHideClose(self.form.buttonBox) maybeHideClose(self.form.buttonBox)
addCloseShortcut(self) addCloseShortcut(self)
@ -78,7 +80,7 @@ class NewDeckStats(QDialog):
def reject(self) -> None: def reject(self) -> None:
self.deck_chooser.cleanup() self.deck_chooser.cleanup()
self.form.web.cleanup() self.form.web.cleanup()
self.form.web = None self.form.web = None # type: ignore
saveGeom(self, self.name) saveGeom(self, self.name)
aqt.dialogs.markClosed("NewDeckStats") aqt.dialogs.markClosed("NewDeckStats")
QDialog.reject(self) QDialog.reject(self)
@ -115,7 +117,9 @@ class NewDeckStats(QDialog):
# unreadable. A simple fix for now is to scroll to the top of the # unreadable. A simple fix for now is to scroll to the top of the
# page first. # page first.
def after_scroll(arg: Any) -> None: def after_scroll(arg: Any) -> None:
self.form.web.page().printToPdf(path) form_web_page = self.form.web.page()
assert form_web_page is not None
form_web_page.printToPdf(path)
tooltip(tr.statistics_saved()) tooltip(tr.statistics_saved())
self.form.web.evalWithCallback("window.scrollTo(0, 0);", after_scroll) self.form.web.evalWithCallback("window.scrollTo(0, 0);", after_scroll)
@ -165,6 +169,7 @@ class DeckStats(QDialog):
b = f.buttonBox.addButton( b = f.buttonBox.addButton(
tr.statistics_save_pdf(), QDialogButtonBox.ButtonRole.ActionRole tr.statistics_save_pdf(), QDialogButtonBox.ButtonRole.ActionRole
) )
assert b is not None
qconnect(b.clicked, self.saveImage) qconnect(b.clicked, self.saveImage)
b.setAutoDefault(False) b.setAutoDefault(False)
qconnect(f.groups.clicked, lambda: self.changeScope("deck")) qconnect(f.groups.clicked, lambda: self.changeScope("deck"))
@ -182,7 +187,7 @@ class DeckStats(QDialog):
def reject(self) -> None: def reject(self) -> None:
self.form.web.cleanup() self.form.web.cleanup()
self.form.web = None self.form.web = None # type: ignore
saveGeom(self, self.name) saveGeom(self, self.name)
aqt.dialogs.markClosed("DeckStats") aqt.dialogs.markClosed("DeckStats")
QDialog.reject(self) QDialog.reject(self)
@ -208,7 +213,9 @@ class DeckStats(QDialog):
path = self._imagePath() path = self._imagePath()
if not path: if not path:
return return
self.form.web.page().printToPdf(path) form_web_page = self.form.web.page()
assert form_web_page is not None
form_web_page.printToPdf(path)
tooltip(tr.statistics_saved()) tooltip(tr.statistics_saved())
def changePeriod(self, n: int) -> None: def changePeriod(self, n: int) -> None:

View file

@ -103,7 +103,7 @@ class Switch(QAbstractButton):
self._position = self.end_position self._position = self.end_position
self.update() self.update()
def paintEvent(self, _event: QPaintEvent) -> None: def paintEvent(self, _event: QPaintEvent | None) -> None:
painter = QPainter(self) painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
painter.setPen(Qt.PenStyle.NoPen) painter.setPen(Qt.PenStyle.NoPen)
@ -162,12 +162,13 @@ class Switch(QAbstractButton):
self._current_label_rectangle(), Qt.AlignmentFlag.AlignCenter, self.label self._current_label_rectangle(), Qt.AlignmentFlag.AlignCenter, self.label
) )
def mouseReleaseEvent(self, event: QMouseEvent) -> None: def mouseReleaseEvent(self, event: QMouseEvent | None) -> None:
super().mouseReleaseEvent(event) super().mouseReleaseEvent(event)
assert event is not None
if event.button() == Qt.MouseButton.LeftButton: if event.button() == Qt.MouseButton.LeftButton:
self._animate_toggle() self._animate_toggle()
def enterEvent(self, event: QEnterEvent) -> None: def enterEvent(self, event: QEnterEvent | None) -> None:
self.setCursor(Qt.CursorShape.PointingHandCursor) self.setCursor(Qt.CursorShape.PointingHandCursor)
super().enterEvent(event) super().enterEvent(event)