Skip to content

Commit

Permalink
mypy: fix type checking error (#3365)
Browse files Browse the repository at this point in the history
* refactor: fix type checking error

error: Argument 1 to "_answerCard" of "Reviewer" has incompatible type "int"; expected "Literal[1, 2, 3, 4]"  [arg-type]

* refactor: remove check that `ease` is correct number

* refactor: rename variable

* refactor: add type hint for generator function

* refactor: revise import of `functools.partial`

* refactor: invert logic of if-construct

to avoid nesting.

* refactor: properly check for `None`

* Update qt/aqt/reviewer.py
  • Loading branch information
davidculley committed Aug 29, 2024
1 parent 83fe301 commit b35b69a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions qt/aqt/reviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from __future__ import annotations

import functools
import json
import random
import re
from collections.abc import Callable, Sequence
from collections.abc import Callable, Generator, Sequence
from dataclasses import dataclass
from enum import Enum, auto
from functools import partial
from typing import Any, Literal, Match, Union, cast

import aqt
Expand Down Expand Up @@ -591,6 +591,18 @@ def korean_shortcuts(
def _shortcutKeys(
self,
) -> Sequence[tuple[str, Callable] | tuple[Qt.Key, Callable]]:

def generate_default_answer_keys() -> (
Generator[tuple[str, partial], None, None]
):
for ease in aqt.mw.pm.default_answer_keys:
key = aqt.mw.pm.get_answer_key(ease)
if not key:
continue
ease = cast(Literal[1, 2, 3, 4], ease)
answer_card_according_to_pressed_key = partial(self._answerCard, ease)
yield (key, answer_card_according_to_pressed_key)

return [
("e", self.mw.onEditCurrent),
(" ", self.onEnterKey),
Expand All @@ -617,11 +629,7 @@ def _shortcutKeys(
("o", self.onOptions),
("i", self.on_card_info),
("Ctrl+Alt+i", self.on_previous_card_info),
*(
(key, functools.partial(self._answerCard, ease))
for ease in aqt.mw.pm.default_answer_keys
if (key := aqt.mw.pm.get_answer_key(ease))
),
*generate_default_answer_keys(),
("u", self.mw.undo),
("5", self.on_pause_audio),
("6", self.on_seek_backward),
Expand Down

0 comments on commit b35b69a

Please sign in to comment.