python / python/mypy

Partial type that is "initialised" via assignment is only temporarily narrowed by `isinstance`

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

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

bug topic-type-narrowing
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

In the following example, the last revealed type could/should be Union[builtins.int, None]:

def f() -> bool: 
    return True

u: int | tuple[int]
x = None
if f():
    x = u
    if isinstance(x, tuple):
        x = x[0]
    reveal_type(x)  # note: "builtins.int"
reveal_type(x)  # note: Revealed type is "Union[builtins.int, Tuple[builtins.int], None]"

See this comment for background information.

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

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

はじめの一歩

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

調査の方向性

まず issue の Python 例を再現し、2 つの reveal_type の結果を期待される絞り込みと比較します。背景を把握するために PR #18180 にリンクされたコメントを読み、その後、代入に基づく絞り込みを担う型チェックの経路を追います。最終的に表示される型が int | None になり、示されている動作を退行させなければ完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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