python / python/mypy

Document type inference behavior in case type declaration has (not) initializer

オープン
#7,252 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

documentation priority-1-normal
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

In cases like this mypy doesn't narrow down the type in assignment:

x: Union[int, str] = 0
# type of x is Union[int, str] here!

Any assignment after the initial declaration will narrow the type down:

x: Union[int, str]
x = 0
# type of x is int here!

Document this behavior since it can be surprising. The motivation for this is that this behavior makes it easy to override the inferred type for a variable, which is sometimes useful.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue 内の 2 つの例をドキュメントの情報源として使用します。初期化子を伴う宣言と、後続の代入を比較し、narrowing の挙動の違いと、推論された型を上書きすることが有用な理由を説明してください。mypy ドキュメント内の適切なセクションを見つけ、そこに説明を追加し、両方の例と推論された型が正確であることを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。