python / python/mypy

immediate numpy arguments not type checked (python 3.10)

Abierto
#14,145 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo la llamada directa final a NumPy y las llamadas equivalentes basadas en variables con mypy 0.991 bajo Python 3.10. Rastrea cómo se infieren los argumentos de las llamadas inmediatas y cómo se comprueban frente al dtype anotado de NDArray; estará terminado cuando la llamada directa de float32-to-uint8 informe de un error de forma consistente con el caso de la variable asignada.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
numpy, python
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.