python / python/mypy

False positive for function that takes a `Callable` as a parameter and returns a `Tuple` of `Union`

Aperta
#10,281 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

The following code

from typing import (
    Callable,
    TypeVar,
    Union,
    Tuple,
)


T = TypeVar("T")


def test(pred: Callable[[T], bool]) -> Tuple[Union[T, int]]:
    return (1,)


def main(pred: Callable[[T], bool]) -> None:
    (val,) = test(pred)  # the error appears here


def f(x: int) -> bool:
    return x > 1


main(f)

triggers the mypy error

Argument 1 to "test" has incompatible type "Callable[[T], bool]"; expected "Callable[[Union[T, int]], bool]"

at the line that I marked with a comment. I believe this is a bug as this correction makes no sense to me (of couse, I could be wrong).
I'm aware that this code is meaningless, but it's the simplest sample I was able to produce that causes the error.
From the tests that I've done it seems to depend both on the type variable being used and on the function test returning a Tuple[Union[...]]. So if I replace T with a regular type of if I return either Union[T, int] or Tuple[int] instead of Tuple[Union[T, int]] the error goes away. It also disappears by not upacking the the result of test(pred).

To Reproduce

Take the code above and check it with mypy. That's it.

Expected Behavior

No error triggered.

Actual Behavior

Mypy detects the error reported above.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: --ignore-missing-imports --follow-imports=silent --show-column-numbers
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.2
  • Operating system and version: Linux x64

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 eseguendo lo snippet Python fornito con mypy 0.812 e conferma l’errore nella chiamata di unpacking. Traccia l’inferenza delle variabili di tipo per Callable, Tuple e Union; il lavoro è completato quando il riproduttore supera il controllo dei tipi senza il falso positivo segnalato e la copertura di regressione protegge questo comportamento.

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
Specificata chiaramente
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.