python / python/mypy

`tuple` of different `type`s narrows to `object`

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

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

bug topic-join-v-union
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

mypy fails to infer the type variable in a arbitrary length tuple.

To Reproduce

from typing import TypeVar, reveal_type

T = TypeVar('T')

def get_type_hint(val_type: tuple[type[T]] | tuple[type[T], ...]) -> T:
    return None  # type: ignore

a: tuple[type[int | str], ...] = (str, int)

reveal_type(a)
reveal_type(get_type_hint((str,)))
reveal_type(get_type_hint((str, int)))
reveal_type(get_type_hint(a))

Expected Behavior

The reveal_type for calling (str, int) and a should be int | str.

Actual Behavior

Type is inferred to narrow type as object.

tuple_infer.py:10: note: Revealed type is "builtins.tuple[Union[Type[builtins.int], Type[builtins.str]], ...]"
tuple_infer.py:11: note: Revealed type is "builtins.str"   
tuple_infer.py:12: note: Revealed type is "builtins.object"
tuple_infer.py:13: note: Revealed type is "builtins.object"

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.1

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

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

はじめの一歩

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

調査の方向性

mypy 1.1.1 で tuple_infer.py の例を再現し、異種タプルの呼び出しに対する reveal_type の出力を調べます。get_type_hint の背後にある型推論の挙動を追跡します。(str, int) を使った呼び出しで object ではなく int | str が明らかになれば完了です。

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

評価

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

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

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