python / python/mypy

immediate numpy arguments not type checked (python 3.10)

Aperta
#14,145 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la chiamata diretta finale a NumPy e le chiamate equivalenti basate su variabili con mypy 0.991 in Python 3.10. Traccia come vengono inferiti gli argomenti delle chiamate immediate e verificati rispetto al dtype annotato di NDArray; il lavoro è completato quando la chiamata diretta da float32 a uint8 segnala un errore in modo coerente con il caso della variabile assegnata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
numpy, python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.