immediate numpy arguments not type checked (python 3.10)

Đang mở
#14,145 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
38/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
numpy, python
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện lệnh gọi NumPy trực tiếp cuối cùng và các lệnh gọi tương đương dựa trên biến bằng mypy 0.991 trên Python 3.10. Theo dõi cách các đối số của lệnh gọi tức thời được suy luận và kiểm tra so với dtype được chú thích của NDArray; hoàn tất khi lệnh gọi trực tiếp từ float32-to-uint8 báo lỗi nhất quán với trường hợp biến được gán.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.