add typehint to deckchooser.py

every occurunce of self.widget is type ignored
because QHBoxLayout has a method named widget
renaming this property will allow type hints
This commit is contained in:
BlueGreenMagick 2020-06-11 14:19:36 +09:00
parent 552db2b94d
commit e3889d49c9
2 changed files with 21 additions and 15 deletions

View File

@ -2,14 +2,18 @@
# Copyright: Ankitects Pty Ltd and contributors # Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from typing import Any
from anki.lang import _ from anki.lang import _
from aqt import gui_hooks from aqt import AnkiQt, gui_hooks
from aqt.qt import * from aqt.qt import *
from aqt.utils import shortcut from aqt.utils import shortcut
class DeckChooser(QHBoxLayout): class DeckChooser(QHBoxLayout):
def __init__(self, mw, widget: QWidget, label=True, start=None) -> None: def __init__(
self, mw: AnkiQt, widget: QWidget, label: bool = True, start: Any = None
) -> None:
QHBoxLayout.__init__(self) QHBoxLayout.__init__(self)
self.widget = widget # type: ignore self.widget = widget # type: ignore
self.mw = mw self.mw = mw
@ -20,15 +24,15 @@ class DeckChooser(QHBoxLayout):
self.widget.setLayout(self) self.widget.setLayout(self)
gui_hooks.current_note_type_did_change.append(self.onModelChangeNew) gui_hooks.current_note_type_did_change.append(self.onModelChangeNew)
def setupDecks(self): def setupDecks(self) -> None:
if self.label: if self.label:
self.deckLabel = QLabel(_("Deck")) self.deckLabel = QLabel(_("Deck"))
self.addWidget(self.deckLabel) self.addWidget(self.deckLabel)
# decks box # decks box
self.deck = QPushButton(clicked=self.onDeckChange) self.deck = QPushButton(clicked=self.onDeckChange) # type: ignore
self.deck.setAutoDefault(False) self.deck.setAutoDefault(False)
self.deck.setToolTip(shortcut(_("Target Deck (Ctrl+D)"))) self.deck.setToolTip(shortcut(_("Target Deck (Ctrl+D)")))
s = QShortcut(QKeySequence("Ctrl+D"), self.widget, activated=self.onDeckChange) s = QShortcut(QKeySequence("Ctrl+D"), self.widget, activated=self.onDeckChange) # type: ignore
self.addWidget(self.deck) self.addWidget(self.deck)
# starting label # starting label
if self.mw.col.conf.get("addToCur", True): if self.mw.col.conf.get("addToCur", True):
@ -54,26 +58,26 @@ class DeckChooser(QHBoxLayout):
sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0)) sizePolicy = QSizePolicy(QSizePolicy.Policy(7), QSizePolicy.Policy(0))
self.deck.setSizePolicy(sizePolicy) self.deck.setSizePolicy(sizePolicy)
def show(self): def show(self) -> None:
self.widget.show() self.widget.show() # type: ignore
def hide(self): def hide(self) -> None:
self.widget.hide() self.widget.hide() # type: ignore
def cleanup(self) -> None: def cleanup(self) -> None:
gui_hooks.current_note_type_did_change.remove(self.onModelChangeNew) gui_hooks.current_note_type_did_change.remove(self.onModelChangeNew)
def onModelChangeNew(self, unused=None): def onModelChangeNew(self, unused: Any = None) -> None:
self.onModelChange() self.onModelChange()
def onModelChange(self): def onModelChange(self) -> None:
if not self.mw.col.conf.get("addToCur", True): if not self.mw.col.conf.get("addToCur", True):
self.setDeckName( self.setDeckName(
self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"]) self.mw.col.decks.nameOrNone(self.mw.col.models.current()["did"])
or _("Default") or _("Default")
) )
def onDeckChange(self): def onDeckChange(self) -> None:
from aqt.studydeck import StudyDeck from aqt.studydeck import StudyDeck
current = self.deckName() current = self.deckName()
@ -90,14 +94,14 @@ class DeckChooser(QHBoxLayout):
if ret.name: if ret.name:
self.setDeckName(ret.name) self.setDeckName(ret.name)
def setDeckName(self, name): def setDeckName(self, name: str) -> None:
self.deck.setText(name.replace("&", "&&")) self.deck.setText(name.replace("&", "&&"))
self._deckName = name self._deckName = name
def deckName(self): def deckName(self) -> str:
return self._deckName return self._deckName
def selectedId(self): def selectedId(self) -> int:
# save deck name # save deck name
name = self.deckName() name = self.deckName()
if not name.strip(): if not name.strip():

View File

@ -64,3 +64,5 @@ check_untyped_defs=true
check_untyped_defs=true check_untyped_defs=true
[mypy-aqt.deckbrowser] [mypy-aqt.deckbrowser]
check_untyped_defs=true check_untyped_defs=true
[mypy-aqt.deckchooser]
check_untyped_defs=true