Skip to content

Commit

Permalink
dataclassでオプショナルになっているべきフィールドをオプショナルにする (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Dec 1, 2023
1 parent 667f157 commit c8f5c3a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
29 changes: 29 additions & 0 deletions crates/voicevox_core/src/engine/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod tests {
use super::*;
use crate::*;
use pretty_assertions::assert_eq;
use serde_json::json;

#[rstest]
fn check_audio_query_model_json_field_snake_case() {
Expand Down Expand Up @@ -115,4 +116,32 @@ mod tests {
_ => {}
}
}

#[rstest]
fn it_accepts_json_without_optional_fields() -> anyhow::Result<()> {
serde_json::from_value::<AudioQueryModel>(json!({
"accent_phrases": [
{
"moras": [
{
"text": "ア",
"vowel": "a",
"vowel_length": 0.0,
"pitch": 0.0
}
],
"accent": 1
}
],
"speed_scale": 1.0,
"pitch_scale": 0.0,
"intonation_scale": 1.0,
"volume_scale": 1.0,
"pre_phoneme_length": 0.1,
"post_phoneme_length": 0.1,
"output_sampling_rate": 24000,
"output_stereo": false
}))?;
Ok(())
}
}
38 changes: 38 additions & 0 deletions crates/voicevox_core_python_api/python/test/test_audio_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import textwrap

from voicevox_core import AudioQuery


def test_accept_json_without_optional_fields() -> None:
AudioQuery(
**json.loads(
textwrap.dedent(
"""\
{
"accent_phrases": [
{
"moras": [
{
"text": "ア",
"vowel": "a",
"vowel_length": 0.0,
"pitch": 0.0
}
],
"accent": 1
}
],
"speed_scale": 1.0,
"pitch_scale": 0.0,
"intonation_scale": 1.0,
"volume_scale": 1.0,
"pre_phoneme_length": 0.1,
"post_phoneme_length": 0.1,
"output_sampling_rate": 24000,
"output_stereo": false
}
""",
)
)
)
18 changes: 9 additions & 9 deletions crates/voicevox_core_python_api/python/voicevox_core/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ class Mora:
text: str
"""文字。"""

consonant: Optional[str]
"""子音の音素。"""

consonant_length: Optional[float]
"""子音の音長。"""

vowel: str
"""母音の音素。"""

Expand All @@ -134,6 +128,12 @@ class Mora:
pitch: float
"""音高。"""

consonant: Optional[str] = None
"""子音の音素。"""

consonant_length: Optional[float] = None
"""子音の音長。"""


@pydantic.dataclasses.dataclass
class AccentPhrase:
Expand All @@ -145,10 +145,10 @@ class AccentPhrase:
accent: int
"""アクセント箇所。"""

pause_mora: Optional[Mora]
pause_mora: Optional[Mora] = None
"""後ろに無音を付けるかどうか。"""

is_interrogative: bool
is_interrogative: bool = False
"""疑問系かどうか。"""


Expand Down Expand Up @@ -183,7 +183,7 @@ class AudioQuery:
output_stereo: bool
"""音声データをステレオ出力するか否か。"""

kana: Optional[str] # FIXME: requiredになっている(他の`Optional`も同様)
kana: Optional[str] = None
"""
[読み取り専用] AquesTalk風記法。
Expand Down

0 comments on commit c8f5c3a

Please sign in to comment.