Skip to content

Commit

Permalink
Enable strict_optional for aqt/tagedit, utils, sync (#3578)
Browse files Browse the repository at this point in the history
* Enable strict_optional for tagedit

* Fix mypy errors

* Enable strict_optional for utils

* Fix mypy errors

* Enable strict_optional for sync

* Fix mypy errors

---------

Co-authored-by: Abdo <[email protected]>
  • Loading branch information
bpnguyen107 and abdnh authored Nov 15, 2024
1 parent 29f714d commit 9d09c32
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ strict_optional = True
strict_optional = True
[mypy-aqt.progress]
strict_optional = True
[mypy-aqt.tagedit]
strict_optional = True
[mypy-aqt.utils]
strict_optional = True
[mypy-aqt.sync]
strict_optional = True
[mypy-anki.scheduler.base]
strict_optional = True
[mypy-anki._backend.rsbridge]
Expand Down
2 changes: 1 addition & 1 deletion pylib/anki/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def abort_sync(self) -> None:
self._backend.abort_sync()

def full_upload_or_download(
self, *, auth: SyncAuth, server_usn: int | None, upload: bool
self, *, auth: SyncAuth | None, server_usn: int | None, upload: bool
) -> None:
self._backend.full_upload_or_download(
sync_pb2.FullUploadOrDownloadRequest(
Expand Down
4 changes: 2 additions & 2 deletions qt/aqt/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def on_deck_changed(self, deck_id: int) -> None:
lambda _: self.refresh()
).run_in_background()

def _imagePath(self) -> str:
def _imagePath(self) -> str | None:
name = time.strftime("-%Y-%m-%d@%H-%M-%S.pdf", time.localtime(time.time()))
name = f"anki-{tr.statistics_stats()}{name}"
file = getSaveFile(
Expand Down Expand Up @@ -196,7 +196,7 @@ def closeWithCallback(self, callback: Callable[[], None]) -> None:
self.reject()
callback()

def _imagePath(self) -> str:
def _imagePath(self) -> str | None:
name = time.strftime("-%Y-%m-%d@%H-%M-%S.pdf", time.localtime(time.time()))
name = f"anki-{tr.statistics_stats()}{name}"
file = getSaveFile(
Expand Down
10 changes: 6 additions & 4 deletions qt/aqt/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def callback(choice: int) -> None:


def confirm_full_download(
mw: aqt.main.AnkiQt, server_usn: int, on_done: Callable[[], None]
mw: aqt.main.AnkiQt, server_usn: int | None, on_done: Callable[[], None]
) -> None:
# confirmation step required, as some users customize their notetypes
# in an empty collection, then want to upload them
Expand All @@ -184,7 +184,7 @@ def callback(choice: int) -> None:


def confirm_full_upload(
mw: aqt.main.AnkiQt, server_usn: int, on_done: Callable[[], None]
mw: aqt.main.AnkiQt, server_usn: int | None, on_done: Callable[[], None]
) -> None:
# confirmation step required, as some users have reported an upload
# happening despite having their AnkiWeb collection not being empty
Expand Down Expand Up @@ -220,7 +220,7 @@ def on_full_sync_timer(mw: aqt.main.AnkiQt, label: str) -> None:


def full_download(
mw: aqt.main.AnkiQt, server_usn: int, on_done: Callable[[], None]
mw: aqt.main.AnkiQt, server_usn: int | None, on_done: Callable[[], None]
) -> None:
label = tr.sync_downloading_from_ankiweb()

Expand Down Expand Up @@ -372,7 +372,9 @@ def get_id_and_pass_from_user(
l2.setBuddy(passwd)
vbox.addLayout(g)
bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) # type: ignore
bb.button(QDialogButtonBox.StandardButton.Ok).setAutoDefault(True)
ok_button = bb.button(QDialogButtonBox.StandardButton.Ok)
assert ok_button is not None
ok_button.setAutoDefault(True)
qconnect(bb.accepted, diag.accept)
qconnect(bb.rejected, diag.reject)
vbox.addWidget(bb)
Expand Down
37 changes: 22 additions & 15 deletions qt/aqt/tagedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,39 @@ def setCol(self, col: Collection) -> None:
l = (d.name for d in self.col.decks.all_names_and_ids())
self.model.setStringList(l)

def focusInEvent(self, evt: QFocusEvent) -> None:
def focusInEvent(self, evt: QFocusEvent | None) -> None:
QLineEdit.focusInEvent(self, evt)

def keyPressEvent(self, evt: QKeyEvent) -> None:
def keyPressEvent(self, evt: QKeyEvent | None) -> None:
assert evt is not None
popup = self._completer.popup()
assert popup is not None

if evt.key() in (Qt.Key.Key_Up, Qt.Key.Key_Down):
# show completer on arrow key up/down
if not self._completer.popup().isVisible():
if not popup.isVisible():
self.showCompleter()
return
if (
evt.key() == Qt.Key.Key_Tab
and evt.modifiers() & Qt.KeyboardModifier.ControlModifier
):
# select next completion
if not self._completer.popup().isVisible():
if not popup.isVisible():
self.showCompleter()
index = self._completer.currentIndex()
self._completer.popup().setCurrentIndex(index)
popup.setCurrentIndex(index)
cur_row = index.row()
if not self._completer.setCurrentRow(cur_row + 1):
self._completer.setCurrentRow(0)
return
if (
evt.key() in (Qt.Key.Key_Enter, Qt.Key.Key_Return)
and self._completer.popup().isVisible()
):
if evt.key() in (Qt.Key.Key_Enter, Qt.Key.Key_Return) and popup.isVisible():
# apply first completion if no suggestion selected
selected_row = self._completer.popup().currentIndex().row()
selected_row = popup.currentIndex().row()
if selected_row == -1:
self._completer.setCurrentRow(0)
index = self._completer.currentIndex()
self._completer.popup().setCurrentIndex(index)
popup.setCurrentIndex(index)
self.hideCompleter()
QWidget.keyPressEvent(self, evt)
return
Expand All @@ -97,15 +98,19 @@ def showCompleter(self) -> None:
self._completer.setCompletionPrefix(self.text())
self._completer.complete()

def focusOutEvent(self, evt: QFocusEvent) -> None:
def focusOutEvent(self, evt: QFocusEvent | None) -> None:
QLineEdit.focusOutEvent(self, evt)
self.lostFocus.emit() # type: ignore
self._completer.popup().hide()
popup = self._completer.popup()
assert popup is not None
popup.hide()

def hideCompleter(self) -> None:
if sip.isdeleted(self._completer): # type: ignore
return
self._completer.popup().hide()
popup = self._completer.popup()
assert popup is not None
popup.hide()


class TagCompleter(QCompleter):
Expand All @@ -120,7 +125,9 @@ def __init__(
self.edit = edit
self.cursor: int | None = None

def splitPath(self, tags: str) -> list[str]:
def splitPath(self, tags: str | None) -> list[str]:
assert tags is not None
assert self.edit.col is not None
stripped_tags = tags.strip()
stripped_tags = re.sub(" +", " ", stripped_tags)
self.tags = self.edit.col.tags.split(stripped_tags)
Expand Down
Loading

0 comments on commit 9d09c32

Please sign in to comment.