Sentinel and enum type merge issues with generic

Offen
#22,334 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

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

Rechercherichtung

Start by running the supplied examples through mypy and compare direct assignment with assigning the result to a temporary. Check both the sentinel and enum cases against the working int example. Done means the direct assignments are accepted and the revealed types match the expected results.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug
from typing_extensions import sentinel

Undefined = sentinel("Undefined")

def none_if_undefined[_T](value: _T | Undefined) -> _T | None:
    return None if value is Undefined else value

def func() -> None:
    device_id: str | Undefined | None = Undefined
    device_id = none_if_undefined(device_id)  # <-- error
    reveal_type(device_id)
test.py:10: error: Argument 1 to "none_if_undefined" has incompatible type "str | Undefined | None"; expected "str | Undefined"  [arg-type]
test.py:11: note: Revealed type is "str | Undefined | None"

However, assigning the result from none_if_undefined to a temporary variable first works as expected.

def func() -> None:
    device_id: str | Undefined | None = Undefined
    x = none_if_undefined(device_id)
    reveal_type(x)
    device_id = x
    reveal_type(device_id)
test.py:11: note: Revealed type is "str | None"
test.py:13: note: Revealed type is "str | None"

--
A similar issues exists for enums

from enum import Enum
from typing import Literal

class E(Enum):
    SPECIAL = 1

def none_if_special[_T](value: _T | Literal[E.SPECIAL]) -> _T | None:
    return None if value is E.SPECIAL else value

def func() -> None:
    device_id: str | Literal[E.SPECIAL] | None = E.SPECIAL
    device_id = none_if_special(device_id)  # <-- error
    reveal_type(device_id)
test.py:12: error: Argument 1 to "none_if_special" has incompatible type "str | Literal[E.SPECIAL] | None"; expected "str | Literal[E.SPECIAL]"  [arg-type]
test.py:13: note: Revealed type is "str | Literal[test7.E.SPECIAL] | None"

--
For completeness the related example with just int - that works fine -- no errors.

def none_if_int[_T](value: _T | int) -> _T | None:
    return None if isinstance(value, int) else value

def func() -> None:
    device_id: str | int | None = 42
    device_id = none_if_int(device_id)
    reveal_type(device_id)
Vorherrschende Sprache
Python
Sterne
20.7k
Forks
3.4k
Ø Merge
1 T. 15 Std.
Gemergte PRs (30 T.)
114

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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