python / python/mypy

Misleading warning for generic argument type on wrong return type

Offen
#12,156 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-type-context
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beschreibung

Bug Report

It appears Mypy propagates types into the type arguments of a function call if the types on the left side are know, slightly obscuring the actual error.

To Reproduce

Run mypy a.py with

import typing as t

T = t.TypeVar('T')
U = t.TypeVar('U')

def coalesce(a: T | None, b: U) -> t.Union[T, U]:
  if a is None:
    return b
  return a

def foo() -> int:
  return coalesce(42, None)

Expected Behavior

Looking at this, I would expect Mypy to complain that the declared return value of foo() (being int) does not match what is actually returned (int | None).

Actual Behavior

It appears that Mypy propagates the return type into the coalesce() type arguments instead and expects them both to be int, thus complaining about the argument type instead.

a.py: note: In function "foo":
a.py:15: error: Argument 2 to "coalesce" has incompatible type "None"; expected "int"  [arg-type]
      return coalesce(42, None)
                          ^
Found 1 error in 1 file (checked 1 source file)

I find this rather unintuitive. My suggestion would be to make Mypy complain about the type of the returned value instead of the argument type.

Your Environment

  • Mypy version used: 0.931

  • Mypy command-line flags: /

  • Mypy configuration options from pyproject.toml (and other config files):

    [tool.mypy]
    pretty = true
    warn_redundant_casts = true
    #warn_unused_ignores = true
    warn_no_return = true
    warn_unreachable = true
    show_error_context = true
    show_error_codes = true
    
  • Python version used: 3.10.2

  • Operating system and version: WSL 2, Ubuntu 20

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 mit dem bereitgestellten Reproduktionsbeispiel a.py und führe mypy a.py mit der angegebenen Konfiguration und Python-Version aus. Vergleiche die aktuelle Diagnose zum Argumenttyp mit der angeforderten Diagnose zum Rückgabetyp und nutze diesen Unterschied, um das zu untersuchende Verhalten der Typinferenz zu identifizieren. Fertig ist die Aufgabe, wenn das Beispiel den Rückgabemismatch meldet, anstatt None als Argument abzulehnen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

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