python / python/typing

Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit `TypeAlias`es inside `if TYPE_CHECKING` blocks as stringized annotations.

Offen
#1,562 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: feature
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beschreibung

The Problem

At runtime, TYPE_CHECKING is always False, only type checkers assume it is True. Therefore, if TYPE_CHECKING blocks are only parsed, but never evaluated by the Python interpreter. Since A = int | str is a valid syntax for all still-supported Python versions at the moment (all versions ever since typing was introduced, actually), it would make sense if type checkers were allowed to accept usage of "new-semantics-old-syntax" features (like the alternative union syntax) in explicitly-annotated TypeAliases inside of if TYPE_CHECKING blocks as long as the Python version specified [project.requires-python] (or it's equivalent in tools) can successfully parse that syntax. This is essentially the same as treating explicitly-annotated TypeAliases as stringized annotations by-default.

Examples

# Run on 3.9

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    A = int | str  # Alternative syntax for unions requires Python 3.10 or newer # pyright
                   # Invalid type alias: expression is not a valid type          # mypy
                   # Unsupported left operand type for | ("type[int]")           # mypy

# With proposal
from typing import TYPE_CHECKING
from typing_extensions import TypeAlias

if TYPE_CHECKING:
    A: TypeAlias = int | str  # Treated as a stringized annotation because `: TypeAlias`
                              # is explicit, therefore valid.

pyright-play
mypy-play

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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 den verlinkten pyright-play- und mypy-play-Beispielen und konzentriere dich auf Python 3.9, TYPE_CHECKING und explizit annotierte TypeAliases. Es sind keine Repository-Dateien oder Tests identifiziert; abgeschlossen wäre die Arbeit erst mit einer festgelegten Spezifikation dafür, wie diese Fälle interpretiert werden sollen, sowie entsprechender Einigung oder Implementierungsarbeit in den relevanten Type-Checking-Tools.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
28/100

Neue Issues direkt in Ihr Postfach

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