-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat!: ユーザー辞書データに改行やnull文字が入っていた場合にエラーとする #1522
Conversation
現在テストが落ちていますが、 #1523 がマージされれば通るはずです。 |
f4fbba7
to
7615029
Compare
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.
確かに CSV が破壊されると怖いですね!!
ほとんどLGTMです!
ちょっとpydanticでわからない点があり、把握しておくとミスを予防できるかもと思ったのでコメントしてみました 🙇
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.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
voicevox_engine/user_dict/model.py:66
- [nitpick] The method name 'remove_newlines_and_null' could be more descriptive. Consider renaming it to 'sanitize_surface'.
def remove_newlines_and_null(cls, surface: str) -> str:
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.
すみませんもう1点だけ!
検証がsurfaceだけなのって理由あったりされますか? 👀
こう、全str有り得そうだよな〜と・・・!!
pydanticに全str、あるいは複数のpropertyに対して同じvalidatorを噛ませる方法があればそういう実装も楽そうなのですが。。
通常のAPI経由の使用では、surface以外に改行やnull文字が混入しないため、今回はsurfaceのみ実装しました。
Annotated patternを使うことで実装できそうです。 ちなみにですが、CSVのエントリをダブルクォートで囲んでエスケープする方法は使えなさそうでした。 |
おーーーありがとうございます!! ちょっとメンテナンス性考えて整理してみました!
ということで、少なくとも今回はデータ変換ではなく検証のみにするのはどうでしょう・・・!! @takana-v さんが書いてくださったブランチのコードでほとんどできてると認識してます!(注文が多くてすみません。。。) 個人用メモ:
|
変換ではなく検証のみを行うように変更しました。
Pydanticは、「入力をチェックする」ものではなく、「最終的な出力の型を保証する」という考え方のようです。
入力チェックをModelの外に切り出すのであれば、Pydanticではなくdataclassを使うべきなんですかね...?
一方Pydanticに乗っかるのであれば、こんな感じにデータ変換をすることになるかなと思います。
ちょっとユーザー辞書周りのデータ変換フローがややこしい感じがしたので一旦整理してみました。 辞書登録時
辞書読み込み時
|
いったんコメントお答えまで!
自分もドキュメント読んでみた感じ、そういう雰囲気を感じました! だとしたら 検証をdataclassで行うかPydanticで行うか
すでに値として
仰ってる感じになると思います! CsvSafeStr型のようなものを作ってUserDictWord (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.
PR変更のレビューコメントです!かなり良さそう!!!
voicevox_engine/user_dict/model.py
Outdated
surface: Annotated[ | ||
str, | ||
AfterValidator(convert_to_zenkaku), | ||
AfterValidator(check_newlines_and_null), | ||
] = Field(description="表層形") |
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.
(ただのコメントです)
CsvSafeStr使ったほうが良さそう?
と思ったけど、これは,
とか使えるのが正しいのか〜〜〜。
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.
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Co-authored-by: Copilot <[email protected]>
なるほどです・・・! Pydanticの欠点を一言で表すと、コンストラクタに渡す型とフィールドの型を別々にできない点だなーと感じました! class UserDictWord:
def __init__(self, pronunciation: str, mora_count: int | "auto")
self.pronunciation = pronunciation
self.mora_count = mora_count if mora_count != "auto" else count(pronunciation) でも提案の通り |
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!!!!!
かなり整理できて課題点も見えてきて良かったです! ありがとうございました!!
add_user_dict_word
でCsvSafeStrを使いたい件、TODOコメントだけ足させていただこうと思います!!
内容
UserDictWord
モデルの作成時に、Surfaceに含まれる改行コードならびにnull文字を削除するようにします。これにより、不正な形式の辞書CSVファイルが生成されることを防ぎます。
関連 Issue
スクリーンショット・動画など
その他
コンマやダブルクオーテーション等の文字列に関しては、
UserDictWord
モデルの作成時に全角に変換されるため大丈夫そうです。また、
pronunciation
に関しては、カタカナ以外の文字を受け付けないので、こちらも大丈夫そうです。