python / python/mypy

Type expression arguments aren't recognized as `TypeForm` when the parameter type is a union containing a `TypeForm`

Offen
#21,964 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-type-form
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

A type expression passed as a call argument is only recognized as a TypeForm when the parameter type is exactly TypeForm[...]. With a union such as TypeForm[T] | None, the argument is evaluated as a value expression instead.

from typing import Annotated, TypeVar
from typing_extensions import TypeForm

T = TypeVar('T')

def f(t: TypeForm[T]) -> T: ...
def g(t: TypeForm[T] | None) -> T: ...

reveal_type(f(Annotated[int, 'meta']))  # int
reveal_type(g(Annotated[int, 'meta']))  # Argument 1 to "g" has incompatible type "<typing special form>"; expected "TypeForm[Never] | None"  [arg-type]

The same happens with int | str, Literal[...], Optional[...] or a string annotation. Assigning the same expression to a variable annotated TypeForm[int] | None works, so this is specific to call arguments.

Playground: https://mypy-play.net/?mypy=latest&python=3.14&gist=9ccf3597617fe30ec3d718ee9cdb1d9d.

Your Environment

  • Mypy version used: 2.3.1
  • Python version used: 3.14

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 minimalen Reproduktion im Issue und vergleiche direkte TypeForm-Parameter mit TypeForm innerhalb von Unions, einschließlich Optional und String-Annotationen. Verfolge die Aufrufargumentanalyse von mypy und füge anschließend einen Regressionstest hinzu, der bestätigt, dass diese Argumente erkannt werden und ohne eine Inkompatibilitätsdiagnose den erwarteten Typ offenlegen.

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
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

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