python / python/mypy

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

Offen
#10,281 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bereitgestellte Python-Snippet mit mypy 0.812 auszuführen, und bestätige den Fehler beim Entpackungsaufruf. Verfolge die Typvariablen-Inferenz für Callable, Tuple und Union; abgeschlossen ist die Aufgabe, wenn der Reproducer ohne den gemeldeten False-Positive-Fehler typgeprüft wird und eine Regressionstestabdeckung dieses Verhalten schützt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.