Skip to content

Commit

Permalink
add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Jul 2, 2024
1 parent a326386 commit 681700f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
9 changes: 1 addition & 8 deletions japanese/config_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,7 @@ def __init__(self, default: bool = False) -> None:

def iter_profiles(self) -> Iterable[Profile]:
for profile_dict in self["profiles"]:
# In case new options are added or removed in the future,
# load default settings first, then overwrite them.
default = get_default_profile(profile_dict["mode"])
common_keys = dataclasses.asdict(default).keys() & profile_dict.keys()
yield dataclasses.replace(
default,
**{key: profile_dict[key] for key in common_keys},
)
yield Profile.from_config_dict(profile_dict)

def iter_audio_sources(self) -> Iterable[AudioSourceConfig]:
for source_dict in self.audio_sources:
Expand Down
11 changes: 11 additions & 0 deletions japanese/helpers/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ def clone(cls, profile: "Profile"):
def as_config_dict(self):
return dataclasses.asdict(self)

@classmethod
def from_config_dict(cls, profile_dict: dict):
# In case new options are added or removed in the future,
# load default settings first, then overwrite them.
default = cls.get_default(profile_dict["mode"])
common_keys = dataclasses.asdict(default).keys() & profile_dict.keys()
return dataclasses.replace(
default,
**{key: profile_dict[key] for key in common_keys},
)


@functools.cache
def get_default_profile(mode: str) -> Profile:
Expand Down
2 changes: 1 addition & 1 deletion japanese/widgets/audio_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

from aqt.qt import *

from .table import CellContent, ExpandingTableWidget, TableRow
from ..ajt_common.utils import clamp
from ..audio_manager.abstract import AudioSourceManagerFactoryABC
from ..audio_manager.basic_types import AudioSourceConfig
from ..audio_manager.source_manager import normalize_filename
from ..helpers.sqlite3_buddy import sqlite3_buddy
from .table import CellContent, ExpandingTableWidget, TableRow


class SourceEnableCheckbox(QCheckBox):
Expand Down
25 changes: 20 additions & 5 deletions tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,30 @@ def audio_dict() -> dict[str, object]:
}


def test_read_profiles(no_anki_config):
def test_read_profiles(no_anki_config) -> None:
profiles = [*no_anki_config.iter_profiles()]
assert len(profiles) > 0


def test_create_profile(furigana_dict, pitch_dict, audio_dict):
profile = Profile(**furigana_dict)
def test_create_profile_furigana(furigana_dict) -> None:
profile = Profile.from_config_dict(furigana_dict)
assert isinstance(profile, ProfileFurigana)
profile = Profile(**pitch_dict)
assert profile.mode == "furigana"
assert profile.source == "SentKanji"
assert profile.destination == "SentFurigana"


def test_create_profile_pitch(pitch_dict) -> None:
profile = Profile.from_config_dict(pitch_dict)
assert isinstance(profile, ProfilePitch)
profile = Profile(**audio_dict)
assert profile.mode == "pitch"
assert profile.source == "VocabKanji"
assert profile.destination == "VocabPitchPattern"


def test_create_profile_audio(audio_dict) -> None:
profile = Profile.from_config_dict(audio_dict)
assert isinstance(profile, ProfileAudio)
assert profile.mode == "audio"
assert profile.source == "VocabKanji"
assert profile.destination == "VocabAudio"

0 comments on commit 681700f

Please sign in to comment.