From 7356756868c6412bc0829b41782924b5ba3f4e74 Mon Sep 17 00:00:00 2001 From: Matt Krump <1036969+mkrump@users.noreply.github.com> Date: Sat, 25 Jul 2020 15:43:42 -0600 Subject: [PATCH 1/5] Turn on check_untyped_defs for aqt.taglimit * Add type hints taglimit * Turn on check_untyped_defs for aqt.taglimit --- qt/aqt/taglimit.py | 4 +++- qt/mypy.ini | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/qt/aqt/taglimit.py b/qt/aqt/taglimit.py index 0cb164683..971c41f49 100644 --- a/qt/aqt/taglimit.py +++ b/qt/aqt/taglimit.py @@ -1,5 +1,6 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/copyleft/agpl.html +from typing import List, Optional import aqt from aqt.qt import * @@ -9,8 +10,9 @@ from aqt.utils import restoreGeom, saveGeom class TagLimit(QDialog): def __init__(self, mw, parent): QDialog.__init__(self, parent, Qt.Window) + self.tags: Union[str, List] = "" self.mw = mw - self.parent = parent + self.parent: Optional[QWidget] = parent self.deck = self.parent.deck self.dialog = aqt.forms.taglimit.Ui_Dialog() self.dialog.setupUi(self) diff --git a/qt/mypy.ini b/qt/mypy.ini index 69352e25e..132d4cfea 100644 --- a/qt/mypy.ini +++ b/qt/mypy.ini @@ -76,3 +76,5 @@ check_untyped_defs=true check_untyped_defs=true [mypy-aqt.deckconf] check_untyped_defs=true +[mypy-aqt.taglimit] +check_untyped_defs=true From b2cbe10d7e6753ba9632c651d56c9ab17b588741 Mon Sep 17 00:00:00 2001 From: Matt Krump <1036969+mkrump@users.noreply.github.com> Date: Sat, 25 Jul 2020 16:22:05 -0600 Subject: [PATCH 2/5] Turn on check_untyped_defs for aqt.update * Turn on check_untyped_defs for aqt.update --- qt/aqt/update.py | 8 ++++---- qt/mypy.ini | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/qt/aqt/update.py b/qt/aqt/update.py index 5d3bbda0b..8b179f65f 100644 --- a/qt/aqt/update.py +++ b/qt/aqt/update.py @@ -47,18 +47,18 @@ class LatestVersionFinder(QThread): print("update check failed") return if resp["msg"]: - self.newMsg.emit(resp) + self.newMsg.emit(resp) # type: ignore if resp["ver"]: - self.newVerAvail.emit(resp["ver"]) + self.newVerAvail.emit(resp["ver"]) # type: ignore diff = resp["time"] - time.time() if abs(diff) > 300: - self.clockIsOff.emit(diff) + self.clockIsOff.emit(diff) # type: ignore def askAndUpdate(mw, ver): baseStr = _("""

Anki Updated

Anki %s has been released.

""") % ver msg = QMessageBox(mw) - msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) + msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) # type: ignore msg.setIcon(QMessageBox.Information) msg.setText(baseStr + _("Would you like to download it now?")) button = QPushButton(_("Ignore this update")) diff --git a/qt/mypy.ini b/qt/mypy.ini index 132d4cfea..11e0b108c 100644 --- a/qt/mypy.ini +++ b/qt/mypy.ini @@ -78,3 +78,5 @@ check_untyped_defs=true check_untyped_defs=true [mypy-aqt.taglimit] check_untyped_defs=true +[mypy-aqt.update] +check_untyped_defs=true From b5222f935de8ffdd8346b48a8aae57173637e72d Mon Sep 17 00:00:00 2001 From: Matt Krump <1036969+mkrump@users.noreply.github.com> Date: Sat, 25 Jul 2020 17:25:39 -0600 Subject: [PATCH 3/5] Turn on check_untyped_defs for aqt.browser * Turn on check_untyped_defs for aqt.browser * Add type hints to browser --- qt/aqt/browser.py | 28 ++++++++++++++-------------- qt/mypy.ini | 2 ++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index a40c2064f..b2826b701 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -9,7 +9,7 @@ import time from dataclasses import dataclass from enum import Enum from operator import itemgetter -from typing import Callable, List, Optional, Sequence, Tuple, Union +from typing import Callable, List, Optional, Sequence, Tuple, Union, cast import anki import aqt @@ -26,7 +26,7 @@ from anki.utils import htmlToTextLine, ids2str, intTime, isMac, isWin from aqt import AnkiQt, gui_hooks from aqt.editor import Editor from aqt.exporting import ExportDialog -from aqt.previewer import BrowserPreviewer as PreviewDialog +from aqt.previewer import BrowserPreviewer as PreviewDialog, Previewer from aqt.qt import * from aqt.theme import theme_manager from aqt.utils import ( @@ -103,7 +103,7 @@ class DataModel(QAbstractTableModel): del self.cardObjs[c.id] refresh = True if refresh: - self.layoutChanged.emit() + self.layoutChanged.emit() # type: ignore # Model interface ###################################################################### @@ -130,12 +130,12 @@ class DataModel(QAbstractTableModel): if not t.get("bfont"): return f = QFont() - f.setFamily(t.get("bfont", "arial")) - f.setPixelSize(t.get("bsize", 12)) + f.setFamily(cast(str, t.get("bfont", "arial"))) + f.setPixelSize(cast(int, t.get("bsize", 12))) return f elif role == Qt.TextAlignmentRole: - align = Qt.AlignVCenter + align: Union[Qt.AlignmentFlag, int] = Qt.AlignVCenter if self.activeCols[index.column()] not in ( "question", "answer", @@ -299,7 +299,7 @@ class DataModel(QAbstractTableModel): elif type == "template": t = c.template()["name"] if c.model()["type"] == MODEL_CLOZE: - t += " %d" % (c.ord + 1) + t = f"{t} {c.ord + 1}" return t elif type == "cardDue": # catch invalid dates @@ -308,7 +308,7 @@ class DataModel(QAbstractTableModel): except: t = "" if c.queue < 0: - t = "(" + t + ")" + t = f"({t})" return t elif type == "noteCrt": return time.strftime(self.time_format(), time.localtime(c.note().id / 1000)) @@ -582,8 +582,8 @@ class Browser(QMainWindow): self.mw = mw self.col = self.mw.col self.lastFilter = "" - self.focusTo = None - self._previewer = None + self.focusTo: Optional[int] = None + self._previewer: Optional[Previewer] = None self._closeEventHasCleanedUp = False self.form = aqt.forms.browser.Ui_Dialog() self.form.setupUi(self) @@ -2225,16 +2225,16 @@ class ChangeModel(QDialog): old = self.oldModel["tmpls"] combos = self.tcombos new = self.targetModel["tmpls"] - map = {} + template_map: Dict[int, Optional[int]] = {} for i, f in enumerate(old): idx = combos[i].currentIndex() if idx == len(new): # ignore - map[f["ord"]] = None + template_map[f["ord"]] = None else: f2 = new[idx] - map[f["ord"]] = f2["ord"] - return map + template_map[f["ord"]] = f2["ord"] + return template_map def getFieldMap(self): return self.getTemplateMap( diff --git a/qt/mypy.ini b/qt/mypy.ini index 11e0b108c..4d4b6a50a 100644 --- a/qt/mypy.ini +++ b/qt/mypy.ini @@ -80,3 +80,5 @@ check_untyped_defs=true check_untyped_defs=true [mypy-aqt.update] check_untyped_defs=true +[mypy-aqt.browser] +check_untyped_defs=true From 4239c2dfdccb325430f50b57eb2ba3e1a5a5d81f Mon Sep 17 00:00:00 2001 From: Matt Krump <1036969+mkrump@users.noreply.github.com> Date: Sun, 26 Jul 2020 13:00:06 -0600 Subject: [PATCH 4/5] Cast set to list to fix mypy failure in browser.py --- qt/aqt/browser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index b2826b701..90ca565a1 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -2017,7 +2017,7 @@ update cards set usn=?, mod=?, did=? where id in """ nids = set() for s, nidlist in res: nids.update(nidlist) - self.col.tags.bulkAdd(nids, _("duplicate")) + self.col.tags.bulkAdd(list(nids), _("duplicate")) self.mw.progress.finish() self.model.endReset() self.mw.requireReset() From baa033bc78b40b7d24e05e4b5c858e23221719b9 Mon Sep 17 00:00:00 2001 From: Matt Krump <1036969+mkrump@users.noreply.github.com> Date: Sun, 26 Jul 2020 13:16:06 -0600 Subject: [PATCH 5/5] Fix browser.py import sorting --- qt/aqt/browser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qt/aqt/browser.py b/qt/aqt/browser.py index 90ca565a1..33469bc45 100644 --- a/qt/aqt/browser.py +++ b/qt/aqt/browser.py @@ -26,7 +26,8 @@ from anki.utils import htmlToTextLine, ids2str, intTime, isMac, isWin from aqt import AnkiQt, gui_hooks from aqt.editor import Editor from aqt.exporting import ExportDialog -from aqt.previewer import BrowserPreviewer as PreviewDialog, Previewer +from aqt.previewer import BrowserPreviewer as PreviewDialog +from aqt.previewer import Previewer from aqt.qt import * from aqt.theme import theme_manager from aqt.utils import (