mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Enable strict_optional for aqt/deckchooser, about, webview (#3573)
* Enable strict_optional for deckchooser * Fix mypy errors * Enable strict_optional for about * Fix mypy errors * Enable strict_optional for webview * Fix mypy errors * Fix merge error * Revert utils * Add early returns --------- Co-authored-by: Abdo <abdo@abdnh.net>
This commit is contained in:
parent
db7f128b83
commit
edf59c2bb2
4 changed files with 55 additions and 18 deletions
|
@ -92,6 +92,12 @@ strict_optional = True
|
||||||
strict_optional = True
|
strict_optional = True
|
||||||
[mypy-aqt.toolbar]
|
[mypy-aqt.toolbar]
|
||||||
strict_optional = True
|
strict_optional = True
|
||||||
|
[mypy-aqt.deckchooser]
|
||||||
|
strict_optional = True
|
||||||
|
[mypy-aqt.about]
|
||||||
|
strict_optional = True
|
||||||
|
[mypy-aqt.webview]
|
||||||
|
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]
|
||||||
|
|
|
@ -37,14 +37,21 @@ def show(mw: aqt.AnkiQt) -> QDialog:
|
||||||
txt = supportText()
|
txt = supportText()
|
||||||
if mw.addonManager.dirty:
|
if mw.addonManager.dirty:
|
||||||
txt += "\n" + addon_debug_info()
|
txt += "\n" + addon_debug_info()
|
||||||
QApplication.clipboard().setText(txt)
|
clipboard = QApplication.clipboard()
|
||||||
|
assert clipboard is not None
|
||||||
|
clipboard.setText(txt)
|
||||||
tooltip(tr.about_copied_to_clipboard(), parent=dialog)
|
tooltip(tr.about_copied_to_clipboard(), parent=dialog)
|
||||||
|
|
||||||
btn = QPushButton(tr.about_copy_debug_info())
|
btn = QPushButton(tr.about_copy_debug_info())
|
||||||
qconnect(btn.clicked, on_copy)
|
qconnect(btn.clicked, on_copy)
|
||||||
abt.buttonBox.addButton(btn, QDialogButtonBox.ButtonRole.ActionRole)
|
abt.buttonBox.addButton(btn, QDialogButtonBox.ButtonRole.ActionRole)
|
||||||
abt.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setFocus()
|
|
||||||
|
ok_button = abt.buttonBox.button(QDialogButtonBox.StandardButton.Ok)
|
||||||
|
assert ok_button is not None
|
||||||
|
ok_button.setFocus()
|
||||||
|
|
||||||
btnLayout = abt.buttonBox.layout()
|
btnLayout = abt.buttonBox.layout()
|
||||||
|
assert btnLayout is not None
|
||||||
btnLayout.setContentsMargins(12, 12, 12, 12)
|
btnLayout.setContentsMargins(12, 12, 12, 12)
|
||||||
|
|
||||||
# WebView cleanup
|
# WebView cleanup
|
||||||
|
@ -52,7 +59,7 @@ def show(mw: aqt.AnkiQt) -> QDialog:
|
||||||
|
|
||||||
def on_dialog_destroyed() -> None:
|
def on_dialog_destroyed() -> None:
|
||||||
abt.label.cleanup()
|
abt.label.cleanup()
|
||||||
abt.label = None
|
abt.label = None # type: ignore
|
||||||
|
|
||||||
qconnect(dialog.destroyed, on_dialog_destroyed)
|
qconnect(dialog.destroyed, on_dialog_destroyed)
|
||||||
|
|
||||||
|
|
|
@ -99,7 +99,9 @@ class DeckChooser(QHBoxLayout):
|
||||||
def callback(ret: StudyDeck) -> None:
|
def callback(ret: StudyDeck) -> None:
|
||||||
if not ret.name:
|
if not ret.name:
|
||||||
return
|
return
|
||||||
new_selected_deck_id = self.mw.col.decks.by_name(ret.name)["id"]
|
deck = self.mw.col.decks.by_name(ret.name)
|
||||||
|
assert deck is not None
|
||||||
|
new_selected_deck_id = deck["id"]
|
||||||
if self.selected_deck_id != new_selected_deck_id:
|
if self.selected_deck_id != new_selected_deck_id:
|
||||||
self.selected_deck_id = new_selected_deck_id
|
self.selected_deck_id = new_selected_deck_id
|
||||||
if func := self.on_deck_changed:
|
if func := self.on_deck_changed:
|
||||||
|
|
|
@ -89,17 +89,23 @@ class AnkiWebPage(QWebEnginePage):
|
||||||
script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
|
script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
|
||||||
script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentReady)
|
script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentReady)
|
||||||
script.setRunsOnSubFrames(False)
|
script.setRunsOnSubFrames(False)
|
||||||
self.profile().scripts().insert(script)
|
|
||||||
|
profile = self.profile()
|
||||||
|
assert profile is not None
|
||||||
|
scripts = profile.scripts()
|
||||||
|
assert scripts is not None
|
||||||
|
scripts.insert(script)
|
||||||
|
|
||||||
def javaScriptConsoleMessage(
|
def javaScriptConsoleMessage(
|
||||||
self,
|
self,
|
||||||
level: QWebEnginePage.JavaScriptConsoleMessageLevel,
|
level: QWebEnginePage.JavaScriptConsoleMessageLevel,
|
||||||
msg: str,
|
msg: str | None,
|
||||||
line: int,
|
line: int,
|
||||||
srcID: str,
|
srcID: str | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
# not translated because console usually not visible,
|
# not translated because console usually not visible,
|
||||||
# and may only accept ascii text
|
# and may only accept ascii text
|
||||||
|
assert srcID is not None
|
||||||
if srcID.startswith("data"):
|
if srcID.startswith("data"):
|
||||||
srcID = ""
|
srcID = ""
|
||||||
else:
|
else:
|
||||||
|
@ -162,10 +168,16 @@ class AnkiWebPage(QWebEnginePage):
|
||||||
def _onCmd(self, str: str) -> Any:
|
def _onCmd(self, str: str) -> Any:
|
||||||
return self._onBridgeCmd(str)
|
return self._onBridgeCmd(str)
|
||||||
|
|
||||||
def javaScriptAlert(self, frame: Any, text: str) -> None:
|
def javaScriptAlert(self, frame: Any, text: str | None) -> None:
|
||||||
|
if text is None:
|
||||||
|
return
|
||||||
|
|
||||||
showInfo(text)
|
showInfo(text)
|
||||||
|
|
||||||
def javaScriptConfirm(self, frame: Any, text: str) -> bool:
|
def javaScriptConfirm(self, frame: Any, text: str | None) -> bool:
|
||||||
|
if text is None:
|
||||||
|
return False
|
||||||
|
|
||||||
return askUser(text)
|
return askUser(text)
|
||||||
|
|
||||||
|
|
||||||
|
@ -328,7 +340,9 @@ class AnkiWebView(QWebEngineView):
|
||||||
# with target="_blank") and return view
|
# with target="_blank") and return view
|
||||||
return AnkiWebView()
|
return AnkiWebView()
|
||||||
|
|
||||||
def eventFilter(self, obj: QObject, evt: QEvent) -> bool:
|
def eventFilter(self, obj: QObject | None, evt: QEvent | None) -> bool:
|
||||||
|
if evt is None:
|
||||||
|
return False
|
||||||
if self._disable_zoom and is_gesture_or_zoom_event(evt):
|
if self._disable_zoom and is_gesture_or_zoom_event(evt):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -377,7 +391,7 @@ class AnkiWebView(QWebEngineView):
|
||||||
def onSelectAll(self) -> None:
|
def onSelectAll(self) -> None:
|
||||||
self.triggerPageAction(QWebEnginePage.WebAction.SelectAll)
|
self.triggerPageAction(QWebEnginePage.WebAction.SelectAll)
|
||||||
|
|
||||||
def contextMenuEvent(self, evt: QContextMenuEvent) -> None:
|
def contextMenuEvent(self, evt: QContextMenuEvent | None) -> None:
|
||||||
m = QMenu(self)
|
m = QMenu(self)
|
||||||
self._maybe_add_copy_action(m)
|
self._maybe_add_copy_action(m)
|
||||||
gui_hooks.webview_will_show_context_menu(self, m)
|
gui_hooks.webview_will_show_context_menu(self, m)
|
||||||
|
@ -387,9 +401,10 @@ class AnkiWebView(QWebEngineView):
|
||||||
def _maybe_add_copy_action(self, menu: QMenu) -> None:
|
def _maybe_add_copy_action(self, menu: QMenu) -> None:
|
||||||
if self.hasSelection():
|
if self.hasSelection():
|
||||||
a = menu.addAction(tr.actions_copy())
|
a = menu.addAction(tr.actions_copy())
|
||||||
|
assert a is not None
|
||||||
qconnect(a.triggered, self.onCopy)
|
qconnect(a.triggered, self.onCopy)
|
||||||
|
|
||||||
def dropEvent(self, evt: QDropEvent) -> None:
|
def dropEvent(self, evt: QDropEvent | None) -> None:
|
||||||
if self.allow_drops:
|
if self.allow_drops:
|
||||||
super().dropEvent(evt)
|
super().dropEvent(evt)
|
||||||
|
|
||||||
|
@ -455,7 +470,9 @@ class AnkiWebView(QWebEngineView):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def setPlaybackRequiresGesture(self, value: bool) -> None:
|
def setPlaybackRequiresGesture(self, value: bool) -> None:
|
||||||
self.settings().setAttribute(
|
settings = self.settings()
|
||||||
|
assert settings is not None
|
||||||
|
settings.setAttribute(
|
||||||
QWebEngineSettings.WebAttribute.PlaybackRequiresUserGesture, value
|
QWebEngineSettings.WebAttribute.PlaybackRequiresUserGesture, value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -632,10 +649,13 @@ html {{ {font} }}
|
||||||
def eval(self, js: str) -> None:
|
def eval(self, js: str) -> None:
|
||||||
self.evalWithCallback(js, None)
|
self.evalWithCallback(js, None)
|
||||||
|
|
||||||
def evalWithCallback(self, js: str, cb: Callable) -> None:
|
def evalWithCallback(self, js: str, cb: Callable | None) -> None:
|
||||||
self._queueAction("eval", js, cb)
|
self._queueAction("eval", js, cb)
|
||||||
|
|
||||||
def _evalWithCallback(self, js: str, cb: Callable[[Any], Any]) -> None:
|
def _evalWithCallback(self, js: str, cb: Callable[[Any], Any] | None) -> None:
|
||||||
|
page = self.page()
|
||||||
|
assert page is not None
|
||||||
|
|
||||||
if cb:
|
if cb:
|
||||||
|
|
||||||
def handler(val: Any) -> None:
|
def handler(val: Any) -> None:
|
||||||
|
@ -644,9 +664,9 @@ html {{ {font} }}
|
||||||
return
|
return
|
||||||
cb(val)
|
cb(val)
|
||||||
|
|
||||||
self.page().runJavaScript(js, handler)
|
page.runJavaScript(js, handler)
|
||||||
else:
|
else:
|
||||||
self.page().runJavaScript(js)
|
page.runJavaScript(js)
|
||||||
|
|
||||||
def _queueAction(self, name: str, *args: Any) -> None:
|
def _queueAction(self, name: str, *args: Any) -> None:
|
||||||
self._pendingActions.append((name, args))
|
self._pendingActions.append((name, args))
|
||||||
|
@ -685,7 +705,9 @@ html {{ {font} }}
|
||||||
return
|
return
|
||||||
|
|
||||||
if not self._filterSet:
|
if not self._filterSet:
|
||||||
self.focusProxy().installEventFilter(self)
|
focus_proxy = self.focusProxy()
|
||||||
|
assert focus_proxy is not None
|
||||||
|
focus_proxy.installEventFilter(self)
|
||||||
self._filterSet = True
|
self._filterSet = True
|
||||||
|
|
||||||
if cmd == "domDone":
|
if cmd == "domDone":
|
||||||
|
|
Loading…
Reference in a new issue