From ad6f6f59c2606af822c2e9d8b42210a3ed9e2d6d Mon Sep 17 00:00:00 2001 From: tarepan Date: Wed, 19 Jun 2024 06:58:57 +0000 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20`Setting`=20=E3=82=92=20`BaseMo?= =?UTF-8?q?del`=20=E3=81=8B=E3=82=89=20`dataclass`=20=E3=81=B8=E7=A7=BB?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/setting/test_setting.py | 16 ++++++++----- voicevox_engine/setting/setting_manager.py | 27 +++++++++++----------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/test/unit/setting/test_setting.py b/test/unit/setting/test_setting.py index 421a000b7..856db83a7 100644 --- a/test/unit/setting/test_setting.py +++ b/test/unit/setting/test_setting.py @@ -4,7 +4,11 @@ from pydantic import ValidationError from voicevox_engine.setting.model import CorsPolicyMode -from voicevox_engine.setting.setting_manager import Setting, SettingHandler +from voicevox_engine.setting.setting_manager import ( + Setting, + SettingHandler, + _setting_adapter, +) def test_setting_handler_load_not_exist_file() -> None: @@ -15,7 +19,7 @@ def test_setting_handler_load_not_exist_file() -> None: # Expects true_setting = {"allow_origin": None, "cors_policy_mode": CorsPolicyMode.localapps} # Outputs - setting = settings.model_dump() + setting = _setting_adapter.dump_python(settings) # Test assert true_setting == setting @@ -29,7 +33,7 @@ def test_setting_handler_load_exist_file_1() -> None: # Expects true_setting = {"allow_origin": None, "cors_policy_mode": CorsPolicyMode.localapps} # Outputs - setting = settings.model_dump() + setting = _setting_adapter.dump_python(settings) # Test assert true_setting == setting @@ -43,7 +47,7 @@ def test_setting_handler_load_exist_file_2() -> None: # Expects true_setting = {"allow_origin": None, "cors_policy_mode": "all"} # Outputs - setting = settings.model_dump() + setting = _setting_adapter.dump_python(settings) # Test assert true_setting == setting @@ -60,7 +64,7 @@ def test_setting_handler_load_exist_file_3() -> None: "cors_policy_mode": CorsPolicyMode.localapps, } # Outputs - setting = settings.model_dump() + setting = _setting_adapter.dump_python(settings) # Test assert true_setting == setting @@ -76,7 +80,7 @@ def test_setting_handler_save(tmp_path: Path) -> None: # Outputs setting_loader.save(new_setting) # NOTE: `.load()` の正常動作を前提とする - setting = setting_loader.load().model_dump() + setting = _setting_adapter.dump_python(setting_loader.load()) # Test assert true_setting == setting diff --git a/voicevox_engine/setting/setting_manager.py b/voicevox_engine/setting/setting_manager.py index ab8967956..1b0df2ecd 100644 --- a/voicevox_engine/setting/setting_manager.py +++ b/voicevox_engine/setting/setting_manager.py @@ -1,22 +1,26 @@ """エンジン設定関連の処理""" +from dataclasses import dataclass from enum import Enum from pathlib import Path +from typing import Any import yaml -from pydantic import BaseModel, Field +from pydantic import TypeAdapter from ..utility.path_utility import get_save_dir from .model import CorsPolicyMode -class Setting(BaseModel): - """ - エンジンの設定情報 - """ +@dataclass(frozen=True) +class Setting: + """エンジンの設定情報""" - cors_policy_mode: CorsPolicyMode = Field(title="リソース共有ポリシー") - allow_origin: str | None = Field(default=None, title="許可するオリジン") + cors_policy_mode: CorsPolicyMode # リソース共有ポリシー + allow_origin: str | None = None # 許可するオリジン + + +_setting_adapter = TypeAdapter(Setting) USER_SETTING_PATH: Path = get_save_dir() / "setting.yml" @@ -40,17 +44,14 @@ def load(self) -> Setting: setting = {"allow_origin": None, "cors_policy_mode": "localapps"} else: # 指定された設定ファイルから値を取得 - # FIXME: 型チェックと例外処理を追加する + # FIXME: 例外処理を追加する setting = yaml.safe_load(self.setting_file_path.read_text(encoding="utf-8")) - return Setting( - cors_policy_mode=setting["cors_policy_mode"], - allow_origin=setting["allow_origin"], - ) + return _setting_adapter.validate_python(setting) def save(self, settings: Setting) -> None: """設定値をファイルへ書き込む。""" - settings_dict = settings.model_dump() + settings_dict: dict[str, Any] = _setting_adapter.dump_python(settings) if isinstance(settings_dict["cors_policy_mode"], Enum): settings_dict["cors_policy_mode"] = settings_dict["cors_policy_mode"].value From f50aef8492c3d192a478b349dc85cf9a02de9cbc Mon Sep 17 00:00:00 2001 From: tarepan Date: Wed, 19 Jun 2024 07:01:00 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E3=83=90=E3=83=AA=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/setting/test_setting.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/test/unit/setting/test_setting.py b/test/unit/setting/test_setting.py index 856db83a7..a249c923b 100644 --- a/test/unit/setting/test_setting.py +++ b/test/unit/setting/test_setting.py @@ -1,8 +1,5 @@ from pathlib import Path -import pytest -from pydantic import ValidationError - from voicevox_engine.setting.model import CorsPolicyMode from voicevox_engine.setting.setting_manager import ( Setting, @@ -83,10 +80,3 @@ def test_setting_handler_save(tmp_path: Path) -> None: setting = _setting_adapter.dump_python(setting_loader.load()) # Test assert true_setting == setting - - -def test_setting_invalid_input() -> None: - """`Setting` は不正な入力に対してエラーを送出する。""" - # Test - with pytest.raises(ValidationError) as _: - Setting(cors_policy_mode="invalid_value", allow_origin="*")