-
Notifications
You must be signed in to change notification settings - Fork 118
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
Pydanticをv2に上げる #695
Conversation
@@ -183,7 +183,7 @@ class AudioQuery: | |||
output_stereo: bool | |||
"""音声データをステレオ出力するか否か。""" | |||
|
|||
kana: Optional[str] | |||
kana: Optional[str] # FIXME: requiredになっている(他の`Optional`も同様) |
There was a problem hiding this comment.
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)
とちゃんと指定する必要がある)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kana: Optional[str] # FIXME: requiredになっている(他の`Optional`も同様) | |
kana: Optional[str] = None |
でも良さそう?
There was a problem hiding this comment.
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と同様)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あーー 確かにです。
There was a problem hiding this comment.
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
There was a problem hiding this 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でも)
0.14→0.15のchangelog的なものに入れたいので、別PRにしようと思います。 |
内容
Pydanticをv2に上げます。
マイグレーションガイド
関連 Issue
#545
その他