python / python/mypy

TypeForm: Recognize type variables in a consistent way

Offen
#20,124 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Prerequisite: This issue applies after PR https://github.com/python/mypy/pull/19596 is merged.

Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem because the behavior is inconsistent.

Specifically, mypy allows a type variable to be assigned to a TypeForm variable iff it is in a quoted type expression:

[case testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation]
...
E = TypeVar('E')
class Box(Generic[E]):
    def foo(self, e: E) -> None:
        list_of_typx: List[TypeForm] = [E]  # E: "E" is a type variable and only valid in type context
        typx1: TypeForm = E  # E: "E" is a type variable and only valid in type context
        typx2: TypeForm = 'E'

When a type variable is NOT quoted, mypy will not allow it to be assigned:

[case testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable]
SomeTypeVar = TypeVar('SomeTypeVar')
typx: TypeForm
...
# NOTE: Unbound TypeVar isn't currently accepted as a TypeForm. Is that OK?
typx = SomeTypeVar  # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "TypeForm[Any]")

I'd argue that TypeForm should always accept a type variable because it is a valid type expression and TypeForms are intended to accept all kinds of type expressions:

def cast[T](type_expr: TypeForm[T], value_expr: object) -> T: ...

cast(SomeTypeVar, some_expression)  # should not be an error

In summary:

  • mypy should allow a type variable to be assigned to a TypeForm variable, regardless of whether it is quoted or not. Update above-referenced tests to reflect this new behavior.

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

Prüfe zuerst den vorausgesetzten PR #19596 und suche dann die Tests mit den Namen testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation und testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable. Prüfe, wie in TypeForm-Zuweisungen Anführungszeichen verwendende und nicht verwendende Typvariablen behandelt werden. Als abgeschlossen gilt die Aufgabe, wenn beide Fälle Typvariablen konsistent zulassen und die Tests das neue Verhalten widerspiegeln.

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

Neue Issues direkt in Ihr Postfach

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