python / python/mypy

TypeForm: Recognize type variables in a consistent way

Aperta
#20,124 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic-type-form
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esamina prima il PR prerequisite #19596, quindi individua i test denominati testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation e testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable. Verifica come vengono gestite le variabili di tipo tra virgolette e non tra virgolette per le assegnazioni di TypeForm. Il lavoro è completato quando entrambi i casi consentono in modo coerente le variabili di tipo e i test riflettono il nuovo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.