python / python/mypy

Misleading warning for generic argument type on wrong return type

Aperta
#12,156 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

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

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 con il riproduttore a.py fornito ed esegui mypy a.py usando la configurazione e la versione di Python indicate. Confronta la diagnosi corrente relativa al tipo dell’argomento con la diagnosi richiesta relativa al tipo restituito e usa questa differenza per identificare il comportamento di inferenza dei tipi da analizzare. Il lavoro è completato quando l’esempio segnala l’incompatibilità del tipo restituito invece di rifiutare None come argomento.

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

Valutazione

Stack tecnologico
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.