immediate numpy arguments not type checked (python 3.10)

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

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

評価

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

調査の方向性

まず、Python 3.10 上で mypy 0.991 を使い、最終的な直接の NumPy 呼び出しと、それと同等の変数ベースの呼び出しを再現します。即時呼び出しの引数がどのように推論され、注釈付きの NDArray dtype に対してチェックされるかを追跡します。完了条件は、直接の float32-to-uint8 呼び出しが、代入された変数の場合と一貫してエラーを報告することです。

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

説明

bug

Bug Report

Immediate numpy parameters are not type checked. Consider the following code:

import numpy as np


def do_something_uint8(val: np.typing.NDArray[np.uint8]):
    ...


def do_something_float(val: np.typing.NDArray[np.float32]):
    ...

def scalar_int(val: int):
    ...


scalar_int(3.0)  # correctly shows mypy error here

np_u8 = np.random.rand(3, 4).astype(np.uint8)
np_f = np.random.rand(3, 4).astype(np.float32)

do_something_uint8(np_f)  # correctly shows mypy error
do_something_float(np_f)

do_something_uint8(np_u8)
do_something_float(np_u8)  # correctly shows mypy error

do_something_uint8(np.random.rand(3, 4).astype(np.float32))  # does NOT show mypy error here

Look at the last line. This should show a mypy error (similar to the scalar_int case), since we are passing in a numpy with dtype float32. But it does not. If we first assign to a variable, then the type check works ok.

Expected Behavior

The last line of the code above should show a mypy error, because we are trying to pass in a numpy array with dtype float32, into a function that only accepts numpy array of dtype uint8.

Actual Behavior

The last line of the code above passes mypy checking.

Your Environment

$ python -V
Python 3.10.7
$ pip freeze |  grep mypy
mypy==0.991
mypy-extensions==0.4.3
$ uname -a
Darwin Hughs-MacBook-Air.local 21.6.0 Darwin Kernel Version 21.6.0: Thu Sep 29 20:11:33 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T8110 arm64
[mypy]
ignore_missing_imports = True

No mypy command-line flags.

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

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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