Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydanticをv2に上げる #695

Merged
merged 1 commit into from
Nov 27, 2023
Merged

Conversation

qryxip
Copy link
Member

@qryxip qryxip commented Nov 26, 2023

内容

Pydanticをv2に上げます。

マイグレーションガイド

関連 Issue

#545

その他

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

kana: Optional[str]
kana: Optional[str] # FIXME: requiredになっている(他の`Optional`も同様)
Copy link
Member Author

@qryxip qryxip Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: Optional[...]はrequiredになる(=Noneと指定する必要が出てくる)ようになる」とのことだったので対応しようとしたが、@pydantic.dataclasses.dataclassはそもそも元々この指定でオプショナルになるようにはなってなかったらしい。( = dataclasses.field(default=None)とちゃんと指定する必要がある)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kana: Optional[str] # FIXME: requiredになっている(他の`Optional`も同様)
kana: Optional[str] = None

でも良さそう?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにclass末尾にあるAudioQuery::kanaはそれで大丈夫ですね。ただclassの真ん中に挟まっている他のOptionalはそれだと駄目だったと思います(標準ライブラリのdataclassと同様)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あーー 確かにです。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ勘違いしていました。BaseModelとは異なり、 dataclasses.field(default=None)でもclassの末尾に持って来る必要があります (逆に末尾なら= NoneでもOK)。標準ライブラリのdataclassesの制約のようです。

python
Python 3.8.15 (default, Nov  6 2022, 22:50:15)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dataclasses
>>> from typing import Optional
>>> @dataclasses.dataclass
... class S:
...     a: Optional[int] = dataclasses.field(default=None)
...     b: int
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/ryo/.pyenv/versions/3.8.15/lib/python3.8/dataclasses.py", line 1019, in dataclass
    return wrap(cls)
  File "/home/ryo/.pyenv/versions/3.8.15/lib/python3.8/dataclasses.py", line 1011, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/home/ryo/.pyenv/versions/3.8.15/lib/python3.8/dataclasses.py", line 925, in _process_class
    _init_fn(flds,
  File "/home/ryo/.pyenv/versions/3.8.15/lib/python3.8/dataclasses.py", line 502, in _init_fn
    raise TypeError(f'non-default argument {f.name!r} '
TypeError: non-default argument 'b' follows default argument

Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

= Noneでちゃんと動くなら良さそう感。(別PRでもこのPRでも)

@qryxip
Copy link
Member Author

qryxip commented Nov 27, 2023

(別PRでもこのPRでも)

0.14→0.15のchangelog的なものに入れたいので、別PRにしようと思います。

@Hiroshiba Hiroshiba merged commit 667f157 into VOICEVOX:main Nov 27, 2023
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants