Skip to content

Commit

Permalink
feat!: [Python] UserDict.wordsUserDict.to_dict (#977)
Browse files Browse the repository at this point in the history
Java APIの`UserDict#toHashMap`に合わせる形。

懸念としてはデータとAPIの互換性があるが、v0.16を出してから考えることとす
る。
#977 (review)
  • Loading branch information
qryxip authored Feb 7, 2025
1 parent 50fd956 commit 0ec6ea6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async def test_user_dict_load() -> None:
)
)
assert isinstance(uuid_a, UUID)
assert dict_a.words()[uuid_a].surface == "hoge"
assert dict_a.words()[uuid_a].pronunciation == "ホゲ"
assert dict_a.to_dict()[uuid_a].surface == "hoge"
assert dict_a.to_dict()[uuid_a].pronunciation == "ホゲ"

# 単語の更新
dict_a.update_word(
Expand All @@ -39,8 +39,8 @@ async def test_user_dict_load() -> None:
),
)

assert dict_a.words()[uuid_a].surface == "fuga"
assert dict_a.words()[uuid_a].pronunciation == "フガ"
assert dict_a.to_dict()[uuid_a].surface == "fuga"
assert dict_a.to_dict()[uuid_a].pronunciation == "フガ"

# ユーザー辞書のインポート
dict_b = voicevox_core.asyncio.UserDict()
Expand All @@ -52,7 +52,7 @@ async def test_user_dict_load() -> None:
)

dict_a.import_dict(dict_b)
assert uuid_b in dict_a.words()
assert uuid_b in dict_a.to_dict()

# ユーザー辞書のエクスポート
dict_c = voicevox_core.asyncio.UserDict()
Expand All @@ -66,13 +66,13 @@ async def test_user_dict_load() -> None:
os.close(temp_path_fd)
await dict_c.save(temp_path)
await dict_a.load(temp_path)
assert uuid_a in dict_a.words()
assert uuid_c in dict_a.words()
assert uuid_a in dict_a.to_dict()
assert uuid_c in dict_a.to_dict()

# 単語の削除
dict_a.remove_word(uuid_a)
assert uuid_a not in dict_a.words()
assert uuid_c in dict_a.words()
assert uuid_a not in dict_a.to_dict()
assert uuid_c in dict_a.to_dict()

# 単語のバリデーション
with pytest.raises(pydantic.ValidationError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def test_user_dict_load() -> None:
)
)
assert isinstance(uuid_a, UUID)
assert dict_a.words()[uuid_a].surface == "hoge"
assert dict_a.words()[uuid_a].pronunciation == "ホゲ"
assert dict_a.to_dict()[uuid_a].surface == "hoge"
assert dict_a.to_dict()[uuid_a].pronunciation == "ホゲ"

# 単語の更新
dict_a.update_word(
Expand All @@ -38,8 +38,8 @@ def test_user_dict_load() -> None:
),
)

assert dict_a.words()[uuid_a].surface == "fuga"
assert dict_a.words()[uuid_a].pronunciation == "フガ"
assert dict_a.to_dict()[uuid_a].surface == "fuga"
assert dict_a.to_dict()[uuid_a].pronunciation == "フガ"

# ユーザー辞書のインポート
dict_b = voicevox_core.blocking.UserDict()
Expand All @@ -51,7 +51,7 @@ def test_user_dict_load() -> None:
)

dict_a.import_dict(dict_b)
assert uuid_b in dict_a.words()
assert uuid_b in dict_a.to_dict()

# ユーザー辞書のエクスポート
dict_c = voicevox_core.blocking.UserDict()
Expand All @@ -65,13 +65,13 @@ def test_user_dict_load() -> None:
os.close(temp_path_fd)
dict_c.save(temp_path)
dict_a.load(temp_path)
assert uuid_a in dict_a.words()
assert uuid_c in dict_a.words()
assert uuid_a in dict_a.to_dict()
assert uuid_c in dict_a.to_dict()

# 単語の削除
dict_a.remove_word(uuid_a)
assert uuid_a not in dict_a.words()
assert uuid_c in dict_a.words()
assert uuid_a not in dict_a.to_dict()
assert uuid_c in dict_a.to_dict()

# 単語のバリデーション
with pytest.raises(pydantic.ValidationError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ class Synthesizer:
class UserDict:
"""ユーザー辞書。"""

def words(self) -> dict[UUID, UserDictWord]:
"""このオプジェクトの :class:`dict` としての表現。"""
def to_dict(self) -> dict[UUID, UserDictWord]:
"""このオプジェクトを :class:`dict` に変換する。"""
...
def __init__(self) -> None: ...
async def load(self, path: str | PathLike[str]) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ class Synthesizer:
class UserDict:
"""ユーザー辞書。"""

def words(self) -> dict[UUID, UserDictWord]:
"""このオプジェクトの :class:`dict` としての表現。"""
def to_dict(self) -> dict[UUID, UserDictWord]:
"""このオプジェクトを :class:`dict` に変換する。"""
...
def __init__(self) -> None: ...
def load(self, path: str | PathLike[str]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ mod blocking {
Ok(())
}

fn words<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
let words = self.dict.with_words(|words| {
words
.iter()
Expand Down Expand Up @@ -1518,7 +1518,7 @@ mod asyncio {
Ok(())
}

fn words<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
fn to_dict<'py>(&self, py: Python<'py>) -> PyResult<&'py PyDict> {
let words = self.dict.with_words(|words| {
words
.iter()
Expand Down

0 comments on commit 0ec6ea6

Please sign in to comment.