python / python/mypy

Cannot deduce correct type for varargs and overloads involving unions

Aperta
#14,189 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-join-v-union
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I'm not really sure if this is strictly a bug or not, but as I understand it now, it seems to be.

Mypy fails to deduce the correct type for overloads involving unions, but I'm not sure why.

Please refer to the code snippet. If I'm wrong in my understanding, or if I should type the coalesce function in a better/different way, please let me know!

To Reproduce

MyPy Playground link: https://mypy-play.net/?mypy=latest&python=3.11&gist=5efc4f884ef3a447bf050a2eab005f70

from typing_extensions import overload
from  typing import TypeVar


_T = TypeVar("_T")


@overload
def coalesce(
    *values: _T | None,
) -> _T | None:
    ...


@overload
def coalesce(
    *values: _T | None,
    default: _T,
) -> _T:
    ...


def coalesce(
    *values: _T | None,
    default: _T | None = None,
) -> _T | None:
    """Resolves to the first non-null value, or to `default` if no value different than `None` could be found.
    """
    for value in values:
        if value is not None:
            return value
    return default


source: str | bool | None = "a"
default: str | bool = False
output: str | bool = coalesce(source, default=default)

# main.py:35: error: Incompatible types in assignment (expression has type "object", variable has type "Union[str, bool]")  [assignment]
# Found 1 error in 1 file (checked 1 source file)

Expected Behavior

It should work.

Actual Behavior

Mypy emits the following error, for the line where output is assigned:

main.py:35: error: Incompatible types in assignment (expression has type "object", variable has type "Union[str, bool]") [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files): (all defaults)
  • Python version used: CPython 3.7

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 la riproduzione collegata di MyPy Playground e conferma l’errore segnalato per gli overload varargs che usano unions. Traccia il comportamento degli overload e dell’inferenza dei tipi responsabile della produzione di object, quindi aggiungi un test di regressione mirato; è completato quando l’esempio inferisce str | bool per output senza un errore di assegnazione.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.