python / python/mypy

Mypy errors on code that's trivial syntactic transformation of code that it does not error on

Aperta
#17,715 0 commenti 0 reazioni 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

Mypy reports a type error for the eta expansion of code that it does not error on. See below for an example.

To Reproduce

from __future__ import annotations

from collections.abc import Callable
from typing import Generic, NamedTuple, TypeVar, TypeVarTuple

DType = TypeVar("DType")
DType2 = TypeVar("DType2")
Shape = TypeVarTuple("Shape")
Shape2 = TypeVarTuple("Shape2")
Dim1 = TypeVar("Dim1")
SeqLen = TypeVar("SeqLen")
BatchLen = TypeVar("BatchLen")


class ndarray(Generic[*Shape, DType]): ...  # noqa: N801


def vmap(
    fun: Callable[[ndarray[*Shape, DType]], ndarray[*Shape2, DType2]],
) -> Callable[[ndarray[Dim1, *Shape, DType]], ndarray[Dim1, *Shape2, DType2]]:
    raise NotImplementedError


def fn(tkns: ndarray[BatchLen, SeqLen, float]):
    _ = vmap(call)(tkns)
    _ = vmap(lambda x: call(x))(tkns)


def call(input_: ndarray[SeqLen, float]) -> ndarray[SeqLen, float]:
    raise NotImplementedError

Expected Behavior

Eta expansion (going from something like vmap(fn) to vmap(lambda x: fn(x)) should always be behavior preserving and also type preserving.

Actual Behavior

Instead Mypy approves of the first call but not the second.

main.py:26: error: Argument 1 to "call" has incompatible type "ndarray[*Shape, DType]"; expected "ndarray[Never, float]"  [arg-type]

Your Environment

This is using the default setting on https://mypy-play.net/?mypy=latest&python=3.12 with Mypy at 1.11.2.

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

Riproduci l’esempio da main.py usando le impostazioni predefinite di mypy con Python 3.12, come dimostrato su mypy-play con mypy 1.11.2. Traccia il modo in cui mypy inferisce la chiamata diretta rispetto alla lambda espansa eta, e aggiungi o aggiorna la copertura affinché entrambe le forme equivalenti ricevano risultati coerenti del controllo dei tipi.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.