python / python/mypy

Mypy fails to correctly validate Type[T]

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

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

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

説明

Dear all,

Consider the example code below:

from typing import Type, TypeVar, Tuple

T = TypeVar('T')


def func(a: T, a_type: Type[T]) -> Tuple[T, Type[T]]:
    return a, a_type


a1, a1_type = func(1, int)
a2, a2_type = func(1, str)

reveal_type(a1)
reveal_type(a1_type)
reveal_type(a2)
reveal_type(a2_type)

The first call to func shouldn't give rise to any issues, while the second one should complain about an incompatible type in the a_type argument (as type(1) is not str).

Yet, whenever I run mypy (Python 3.7; mypy 0.770; MacOS 10.15.1) it fails to produce any errors, even going as far to infer the type of a2 as object instead of int.

>>> mypy test.py
test.py:13: note: Revealed type is 'builtins.int'
test.py:14: note: Revealed type is 'Type[builtins.int]'
test.py:15: note: Revealed type is 'builtins.object'
test.py:16: note: Revealed type is 'Type[builtins.object]'

Is anyone familiar with this issue?

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

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

はじめの一歩

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

調査の方向性

まず、提供された例を mypy で実行し、報告された型と診断を期待される結果と比較します。Type[T] の型推論と引数検証の経路を追跡します。完了条件は、func(1, str) を拒否し、func(1, int) を受け入れ、明らかになった型を正しく推論することです。

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

評価

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

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

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