python / python/mypy

Union of `Never` can't be used

Offen
#18,779 3 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Functions which optionally take a value of Never type have impossible requirements placed on usages of that optional value.

To Reproduce

https://gist.github.com/mypy-play/a49d72bdacc179ffda5f69d8b66f41ff

from typing import Never


def blah(s: Never | str) -> None:
    print(s + "hi")

Expected Behavior

The code type-checks. Since it is impossible for any variable to take a value of type Never, the type signature of blah is equivalent to str -> None. The function's body is valid when s is annotated with type str, so it should be valid when annotated with Never | str.

Actual Behavior

main.py:5: error: Unsupported left operand type for + ("Never")  [operator]
main.py:5: note: Left operand is of type "Never | str"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Python 3.12
  • MyPy 1.15.0, invocation as performed in the Playground

Background

Pyright appears to do the right thing here: it accepts this code.

I ran into this when implementing this pattern, which is me placing types around something I found untyped in the wild:

T = TypeVar("T", contravariant=True)
class _MyProtocol(Protocol[T]):
  def _foo(self, arg: T | IO[str]) -> None: ...

try:
    import foo
    class _MyImpl(_MyProtocol[foo.Foo]):
      def _foo(self, arg: foo.Foo | IO[str]) -> None: 
        # do something here
        pass

    MyType = _MyImpl

except ImportError:
    class _MyImplNone(_MyProtocol[Never]):
      def _foo(self, arg: Never | IO[str]) -> None:
        # correctly assume arg is an IO[str] here
        pass

    MyType = _MyImplNone

That way, MyType has type _MyProtocol[foo.Foo] | _MyProtocol[Never], allowing a type-level discrimination of whether the foo functionality is present or not.

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 minimalen Python-Reproduzierer im Issue oder im verlinkten mypy-play-Beispiel und vergleiche den gemeldeten Operatorfehler mit dem erwarteten str-equivalenten Verhalten. Verfolge, wie Unions, die Never enthalten, während der Typprüfung behandelt werden, füge dann Abdeckung für die gezeigte Funktion hinzu und bestätige, dass mypy sie ohne den Never-Operandfehler akzeptiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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