python / python/mypy

Cannot deduce correct type for varargs and overloads involving unions

Offen
#14,189 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-join-v-union
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

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 der verknüpften MyPy Playground-Reproduktion und bestätige den gemeldeten Fehler für die varargs-Overloads mit Unions. Verfolge das für die Erzeugung von object verantwortliche Overload- und Typinferenzverhalten und füge anschließend einen fokussierten Regressionstest hinzu; fertig ist es, wenn das Beispiel für output str | bool ohne Zuweisungsfehler ableitet.

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

Neue Issues direkt in Ihr Postfach

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