mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 17:26:36 -04:00
python: add missing type annotations for None values (#3364)
* refactor: explicitly add NoneType to type hints If variable can be `None`, don't be implicit. Be explicit.
This commit is contained in:
parent
922958b0ae
commit
a6d5c94997
18 changed files with 37 additions and 31 deletions
|
@ -897,7 +897,7 @@ class Collection(DeprecatedNamesMixin):
|
||||||
# Config
|
# Config
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def get_config(self, key: str, default: Any = None) -> Any:
|
def get_config(self, key: str, default: Any | None = None) -> Any:
|
||||||
try:
|
try:
|
||||||
return self.conf.get_immutable(key)
|
return self.conf.get_immutable(key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -939,7 +939,7 @@ class Collection(DeprecatedNamesMixin):
|
||||||
return self._backend.set_config_string(key=key, value=value, undoable=undoable)
|
return self._backend.set_config_string(key=key, value=value, undoable=undoable)
|
||||||
|
|
||||||
def get_aux_notetype_config(
|
def get_aux_notetype_config(
|
||||||
self, id: NotetypeId, key: str, default: Any = None
|
self, id: NotetypeId, key: str, default: Any | None = None
|
||||||
) -> Any:
|
) -> Any:
|
||||||
key = self._backend.get_aux_notetype_config_key(id=id, key=key)
|
key = self._backend.get_aux_notetype_config_key(id=id, key=key)
|
||||||
return self.get_config(key, default=default)
|
return self.get_config(key, default=default)
|
||||||
|
@ -951,7 +951,7 @@ class Collection(DeprecatedNamesMixin):
|
||||||
return self.set_config(key, value, undoable=undoable)
|
return self.set_config(key, value, undoable=undoable)
|
||||||
|
|
||||||
def get_aux_template_config(
|
def get_aux_template_config(
|
||||||
self, id: NotetypeId, card_ordinal: int, key: str, default: Any = None
|
self, id: NotetypeId, card_ordinal: int, key: str, default: Any | None = None
|
||||||
) -> Any:
|
) -> Any:
|
||||||
key = self._backend.get_aux_template_config_key(
|
key = self._backend.get_aux_template_config_key(
|
||||||
notetype_id=id, card_ordinal=card_ordinal, key=key
|
notetype_id=id, card_ordinal=card_ordinal, key=key
|
||||||
|
|
|
@ -76,7 +76,7 @@ class ConfigManager:
|
||||||
def __setitem__(self, key: str, value: Any) -> None:
|
def __setitem__(self, key: str, value: Any) -> None:
|
||||||
self.set(key, value)
|
self.set(key, value)
|
||||||
|
|
||||||
def get(self, key: str, default: Any = None) -> Any:
|
def get(self, key: str, default: Any | None = None) -> Any:
|
||||||
try:
|
try:
|
||||||
return self[key]
|
return self[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|
|
@ -85,7 +85,7 @@ class DeckManager(DeprecatedNamesMixin):
|
||||||
self.col = col.weakref()
|
self.col = col.weakref()
|
||||||
self.decks = DecksDictProxy(col)
|
self.decks = DecksDictProxy(col)
|
||||||
|
|
||||||
def save(self, deck_or_config: DeckDict | DeckConfigDict = None) -> None:
|
def save(self, deck_or_config: DeckDict | DeckConfigDict | None = None) -> None:
|
||||||
"Can be called with either a deck or a deck configuration."
|
"Can be called with either a deck or a deck configuration."
|
||||||
if not deck_or_config:
|
if not deck_or_config:
|
||||||
print("col.decks.save() should be passed the changed deck")
|
print("col.decks.save() should be passed the changed deck")
|
||||||
|
|
|
@ -57,7 +57,7 @@ class HttpClient(DeprecatedNamesMixin):
|
||||||
verify=self.verify,
|
verify=self.verify,
|
||||||
) # pytype: disable=wrong-arg-types
|
) # pytype: disable=wrong-arg-types
|
||||||
|
|
||||||
def get(self, url: str, headers: dict[str, str] = None) -> Response:
|
def get(self, url: str, headers: dict[str, str] | None = None) -> Response:
|
||||||
if headers is None:
|
if headers is None:
|
||||||
headers = {}
|
headers = {}
|
||||||
headers["User-Agent"] = self._agent_name()
|
headers["User-Agent"] = self._agent_name()
|
||||||
|
|
|
@ -563,7 +563,7 @@ and notes.mid = ? and cards.ord = ?""",
|
||||||
self._mutate_after_write(notetype)
|
self._mutate_after_write(notetype)
|
||||||
|
|
||||||
# @deprecated(replaced_by=update_dict)
|
# @deprecated(replaced_by=update_dict)
|
||||||
def save(self, notetype: NotetypeDict = None, **legacy_kwargs: bool) -> None:
|
def save(self, notetype: NotetypeDict | None = None, **legacy_kwargs: bool) -> None:
|
||||||
"Save changes made to provided note type."
|
"Save changes made to provided note type."
|
||||||
if not notetype:
|
if not notetype:
|
||||||
print_deprecation_warning(
|
print_deprecation_warning(
|
||||||
|
|
|
@ -147,7 +147,7 @@ class TemplateRenderContext:
|
||||||
card: anki.cards.Card,
|
card: anki.cards.Card,
|
||||||
note: anki.notes.Note,
|
note: anki.notes.Note,
|
||||||
browser: bool = False,
|
browser: bool = False,
|
||||||
notetype: NotetypeDict = None,
|
notetype: NotetypeDict | None = None,
|
||||||
template: dict | None = None,
|
template: dict | None = None,
|
||||||
fill_empty: bool = False,
|
fill_empty: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
|
@ -409,7 +409,9 @@ class AddonManager:
|
||||||
all_conflicts[other_dir].append(addon.dir_name)
|
all_conflicts[other_dir].append(addon.dir_name)
|
||||||
return all_conflicts
|
return all_conflicts
|
||||||
|
|
||||||
def _disableConflicting(self, module: str, conflicts: list[str] = None) -> set[str]:
|
def _disableConflicting(
|
||||||
|
self, module: str, conflicts: list[str] | None = None
|
||||||
|
) -> set[str]:
|
||||||
if not self.isEnabled(module):
|
if not self.isEnabled(module):
|
||||||
# disabled add-ons should not trigger conflict handling
|
# disabled add-ons should not trigger conflict handling
|
||||||
return set()
|
return set()
|
||||||
|
|
|
@ -62,7 +62,7 @@ class SidebarItem:
|
||||||
name: str,
|
name: str,
|
||||||
icon: str | ColoredIcon,
|
icon: str | ColoredIcon,
|
||||||
search_node: SearchNode | None = None,
|
search_node: SearchNode | None = None,
|
||||||
on_expanded: Callable[[bool], None] = None,
|
on_expanded: Callable[[bool], None] | None = None,
|
||||||
expanded: bool = False,
|
expanded: bool = False,
|
||||||
item_type: SidebarItemType = SidebarItemType.CUSTOM,
|
item_type: SidebarItemType = SidebarItemType.CUSTOM,
|
||||||
id: int = 0,
|
id: int = 0,
|
||||||
|
|
|
@ -159,7 +159,7 @@ class SidebarTreeView(QTreeView):
|
||||||
self.refresh()
|
self.refresh()
|
||||||
self._refresh_needed = False
|
self._refresh_needed = False
|
||||||
|
|
||||||
def refresh(self, new_current: SidebarItem = None) -> None:
|
def refresh(self, new_current: SidebarItem | None = None) -> None:
|
||||||
"Refresh list. No-op if sidebar is not visible."
|
"Refresh list. No-op if sidebar is not visible."
|
||||||
if not self.isVisible():
|
if not self.isVisible():
|
||||||
return
|
return
|
||||||
|
|
|
@ -600,7 +600,9 @@ class Table:
|
||||||
self._view.verticalScrollBar().setValue(vertical)
|
self._view.verticalScrollBar().setValue(vertical)
|
||||||
|
|
||||||
def _move_current(
|
def _move_current(
|
||||||
self, direction: QAbstractItemView.CursorAction, index: QModelIndex = None
|
self,
|
||||||
|
direction: QAbstractItemView.CursorAction,
|
||||||
|
index: QModelIndex | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if not self.has_current():
|
if not self.has_current():
|
||||||
return
|
return
|
||||||
|
|
|
@ -233,9 +233,9 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
|
||||||
func: Callable[[Editor], None],
|
func: Callable[[Editor], None],
|
||||||
tip: str = "",
|
tip: str = "",
|
||||||
label: str = "",
|
label: str = "",
|
||||||
id: str = None,
|
id: str | None = None,
|
||||||
toggleable: bool = False,
|
toggleable: bool = False,
|
||||||
keys: str = None,
|
keys: str | None = None,
|
||||||
disables: bool = True,
|
disables: bool = True,
|
||||||
rightside: bool = True,
|
rightside: bool = True,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
|
@ -137,7 +137,7 @@ class ExportDialog(QDialog):
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def options(self, out_path: str) -> ExportOptions:
|
def options(self, out_path: str) -> ExportOptions:
|
||||||
limit: ExportLimit = None
|
limit: ExportLimit | None = None
|
||||||
if self.nids:
|
if self.nids:
|
||||||
limit = NoteIdsLimit(self.nids)
|
limit = NoteIdsLimit(self.nids)
|
||||||
elif current_deck_id := self.current_deck_id():
|
elif current_deck_id := self.current_deck_id():
|
||||||
|
|
|
@ -120,7 +120,7 @@ class ImportDialog(QDialog):
|
||||||
)
|
)
|
||||||
self.deck = aqt.deckchooser.DeckChooser(self.mw, self.frm.deckArea, label=False)
|
self.deck = aqt.deckchooser.DeckChooser(self.mw, self.frm.deckArea, label=False)
|
||||||
|
|
||||||
def modelChanged(self, unused: Any = None) -> None:
|
def modelChanged(self, unused: Any | None = None) -> None:
|
||||||
self.importer.model = self.mw.col.models.current()
|
self.importer.model = self.mw.col.models.current()
|
||||||
self.importer.initMapping()
|
self.importer.initMapping()
|
||||||
self.showMapping()
|
self.showMapping()
|
||||||
|
|
|
@ -862,8 +862,8 @@ class AnkiQt(QMainWindow):
|
||||||
def requireReset(
|
def requireReset(
|
||||||
self,
|
self,
|
||||||
modal: bool = False,
|
modal: bool = False,
|
||||||
reason: Any = None,
|
reason: Any | None = None,
|
||||||
context: Any = None,
|
context: Any | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
traceback.print_stack(file=sys.stdout)
|
traceback.print_stack(file=sys.stdout)
|
||||||
print("requireReset() is obsolete; please use CollectionOp()")
|
print("requireReset() is obsolete; please use CollectionOp()")
|
||||||
|
|
|
@ -42,7 +42,7 @@ class ProgressManager:
|
||||||
repeat: bool,
|
repeat: bool,
|
||||||
requiresCollection: bool = True,
|
requiresCollection: bool = True,
|
||||||
*,
|
*,
|
||||||
parent: QObject = None,
|
parent: QObject | None = None,
|
||||||
) -> QTimer:
|
) -> QTimer:
|
||||||
"""Create and start a standard Anki timer. For an alternative see `single_shot()`.
|
"""Create and start a standard Anki timer. For an alternative see `single_shot()`.
|
||||||
|
|
||||||
|
|
|
@ -152,8 +152,8 @@ class Reviewer:
|
||||||
self.previous_card: Card | None = None
|
self.previous_card: Card | None = None
|
||||||
self._answeredIds: list[CardId] = []
|
self._answeredIds: list[CardId] = []
|
||||||
self._recordedAudio: str | None = None
|
self._recordedAudio: str | None = None
|
||||||
self.typeCorrect: str = None # web init happens before this is set
|
self.typeCorrect: str | None = None # web init happens before this is set
|
||||||
self.state: Literal["question", "answer", "transition", None] = None
|
self.state: Literal["question", "answer", "transition"] | None = None
|
||||||
self._refresh_needed: RefreshNeeded | None = None
|
self._refresh_needed: RefreshNeeded | None = None
|
||||||
self._v3: V3CardInfo | None = None
|
self._v3: V3CardInfo | None = None
|
||||||
self._state_mutation_key = str(random.randint(0, 2**64 - 1))
|
self._state_mutation_key = str(random.randint(0, 2**64 - 1))
|
||||||
|
@ -162,7 +162,7 @@ class Reviewer:
|
||||||
self._previous_card_info = PreviousReviewerCardInfo(self.mw)
|
self._previous_card_info = PreviousReviewerCardInfo(self.mw)
|
||||||
self._states_mutated = True
|
self._states_mutated = True
|
||||||
self._state_mutation_js = None
|
self._state_mutation_js = None
|
||||||
self._reps: int = None
|
self._reps: int | None = None
|
||||||
self._show_question_timer: QTimer | None = None
|
self._show_question_timer: QTimer | None = None
|
||||||
self._show_answer_timer: QTimer | None = None
|
self._show_answer_timer: QTimer | None = None
|
||||||
self.auto_advance_enabled = False
|
self.auto_advance_enabled = False
|
||||||
|
@ -369,7 +369,7 @@ class Reviewer:
|
||||||
def _showQuestion(self) -> None:
|
def _showQuestion(self) -> None:
|
||||||
self._reps += 1
|
self._reps += 1
|
||||||
self.state = "question"
|
self.state = "question"
|
||||||
self.typedAnswer: str = None
|
self.typedAnswer: str | None = None
|
||||||
c = self.card
|
c = self.card
|
||||||
# grab the question and play audio
|
# grab the question and play audio
|
||||||
q = c.question()
|
q = c.question()
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
# 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 __future__ import annotations
|
||||||
|
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from aqt import colors, props
|
from aqt import colors, props
|
||||||
|
@ -21,7 +23,7 @@ class Switch(QAbstractButton):
|
||||||
right_label: str = "",
|
right_label: str = "",
|
||||||
left_color: dict[str, str] = colors.ACCENT_CARD | {},
|
left_color: dict[str, str] = colors.ACCENT_CARD | {},
|
||||||
right_color: dict[str, str] = colors.ACCENT_NOTE | {},
|
right_color: dict[str, str] = colors.ACCENT_NOTE | {},
|
||||||
parent: QWidget = None,
|
parent: QWidget | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(parent=parent)
|
super().__init__(parent=parent)
|
||||||
self.setCheckable(True)
|
self.setCheckable(True)
|
||||||
|
|
|
@ -393,8 +393,8 @@ def showText(
|
||||||
|
|
||||||
def askUser(
|
def askUser(
|
||||||
text: str,
|
text: str,
|
||||||
parent: QWidget = None,
|
parent: QWidget | None = None,
|
||||||
help: HelpPageArgument = None,
|
help: HelpPageArgument | None = None,
|
||||||
defaultno: bool = False,
|
defaultno: bool = False,
|
||||||
msgfunc: Callable | None = None,
|
msgfunc: Callable | None = None,
|
||||||
title: str = "Anki",
|
title: str = "Anki",
|
||||||
|
@ -426,7 +426,7 @@ class ButtonedDialog(QMessageBox):
|
||||||
text: str,
|
text: str,
|
||||||
buttons: list[str],
|
buttons: list[str],
|
||||||
parent: QWidget | None = None,
|
parent: QWidget | None = None,
|
||||||
help: HelpPageArgument = None,
|
help: HelpPageArgument | None = None,
|
||||||
title: str = "Anki",
|
title: str = "Anki",
|
||||||
):
|
):
|
||||||
QMessageBox.__init__(self, parent)
|
QMessageBox.__init__(self, parent)
|
||||||
|
@ -459,7 +459,7 @@ def askUserDialog(
|
||||||
text: str,
|
text: str,
|
||||||
buttons: list[str],
|
buttons: list[str],
|
||||||
parent: QWidget | None = None,
|
parent: QWidget | None = None,
|
||||||
help: HelpPageArgument = None,
|
help: HelpPageArgument | None = None,
|
||||||
title: str = "Anki",
|
title: str = "Anki",
|
||||||
) -> ButtonedDialog:
|
) -> ButtonedDialog:
|
||||||
if not parent:
|
if not parent:
|
||||||
|
@ -473,7 +473,7 @@ class GetTextDialog(QDialog):
|
||||||
self,
|
self,
|
||||||
parent: QWidget | None,
|
parent: QWidget | None,
|
||||||
question: str,
|
question: str,
|
||||||
help: HelpPageArgument = None,
|
help: HelpPageArgument | None = None,
|
||||||
edit: QLineEdit | None = None,
|
edit: QLineEdit | None = None,
|
||||||
default: str = "",
|
default: str = "",
|
||||||
title: str = "Anki",
|
title: str = "Anki",
|
||||||
|
@ -525,7 +525,7 @@ class GetTextDialog(QDialog):
|
||||||
def getText(
|
def getText(
|
||||||
prompt: str,
|
prompt: str,
|
||||||
parent: QWidget | None = None,
|
parent: QWidget | None = None,
|
||||||
help: HelpPageArgument = None,
|
help: HelpPageArgument | None = None,
|
||||||
edit: QLineEdit | None = None,
|
edit: QLineEdit | None = None,
|
||||||
default: str = "",
|
default: str = "",
|
||||||
title: str = "Anki",
|
title: str = "Anki",
|
||||||
|
@ -558,7 +558,7 @@ def getOnlyText(*args: Any, **kwargs: Any) -> str:
|
||||||
# fixme: these utilities could be combined into a single base class
|
# fixme: these utilities could be combined into a single base class
|
||||||
# unused by Anki, but used by add-ons
|
# unused by Anki, but used by add-ons
|
||||||
def chooseList(
|
def chooseList(
|
||||||
prompt: str, choices: list[str], startrow: int = 0, parent: Any = None
|
prompt: str, choices: list[str], startrow: int = 0, parent: Any | None = None
|
||||||
) -> int:
|
) -> int:
|
||||||
if not parent:
|
if not parent:
|
||||||
parent = aqt.mw.app.activeWindow()
|
parent = aqt.mw.app.activeWindow()
|
||||||
|
|
Loading…
Reference in a new issue