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
run.py: ルーティング関数の戻り値に型ヒントを追加 #764
run.py: ルーティング関数の戻り値に型ヒントを追加 #764
Conversation
…lass of HTMLResponse)
…class of HTMLResponse)
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です!!
細かい部分のコメントとても助かります!!
引数が1つの関数を改行するかどうかが結構ランダムに決められているように感じました。
まあ別に良いのですが、とりあえず既存のに合わせてできる限り1行にしておくと、配慮ができてちょっとかっこいいかもです!
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!!!
調整ありがとうございます!
ず〜〜っと型ワーニングが出ていたの気になってたので嬉しいです!!!
内容
run.py
のルーティング関数(@app.get
などがついている関数)の戻り値に型ヒントを追加し、mypyのno-untyped-def
エラーをルーティング関数に限って解消します。誤ったオブジェクトを返すことによる不具合を型レベルで防げます。
変更量を少なくするため、ルーティング関数以外は変更していません。
合わせて変更した点
List[Type] -> list[Type]
、Tuple[Type] -> tuple[Type]
Optional[Type] -> Type | None
/core_versions
の戻り値の型が実際にはResponse
だがlist[str]
で型ヒントがついていたのをResponse
に修正list[str]
にする場合、Response
ではないオブジェクトを返すように修正が必要/installed_libraries
のdocstringの戻り値の型の誤りを修正setting_get()
,setting_post()
のresponse_class
をHTMLResponse
からResponse
に変更startlette.templating._TemplateResponse
型が返るが、この型はHTMLResponse
を継承しておらず、Response
を直接継承しているHTMLResponse
を継承する変更がマージされているが、まだリリースされていないResponse
を継承したオブジェクトを返すとresponse_class
は無視される(FastAPIのソースコード@c1adce4)ので、実は設定する必要がない。そのままでもいいかもしれないけれど、戻り値の型との不一致は混乱するので避けたい未修正な点(ルーティング関数内のmypy型エラー出力)
関連 Issue
スクリーンショット・動画など
その他
mypyを実行
poetry run mypy .