python / python/mypy

Emit errors with `--disallow-any-generics` if `type[T]` or `TypeForm[T]` receives a generic `T` with missing type arguments

Aperta
#21,145 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

If --disallow-any-generics or --strict is on, I'd like mypy to emit E: Missing type parameters for generic type "list" [type-arg] when calling f(list) for the following function definitions:

  • def f[T](arg: type[T], /) -> T: ...
  • def f[T](arg: TypeForm[T], /) -> T: ...

Currently, there's no error report, and reveal_type(f(list)) shows Any.

Pitch

Due to the lack of error reporting in the above situation, implicit Anys currently easily leak when using type[T] or TypeForm[T], creating an implicit source of unsafety, which is surprising under --strict mode. Adapting the examples from PEP 747: Motivation:

# mypy: enable-incomplete-feature=TypeForm, disable-error-code=empty-body

from collections.abc import Callable
from typing_extensions import TypeForm, TypeIs

def trycast_list_item[T](typx: TypeForm[list[T]], value: object) -> T | None: ...
reveal_type(trycast_list_item(list, [1]))  # N: Revealed type is "Any | None"


def isassignable_list_item[T](value: object, typx: TypeForm[list[T]]) -> TypeIs[T]: ...

a: object
if isassignable_list_item(a, list):
    reveal_type(a)  # N: Revealed type is "Any"
else:
    reveal_type(a)  # N: Revealed type is "builtins.object"

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

Inizia riproducendo i due esempi di funzioni generiche con --disallow-any-generics o --strict, inclusi gli invocamenti di reveal_type e l’impostazione di TypeForm per le funzionalità incomplete. Traccia come vengono analizzate le chiamate che usano list per type[T] e TypeForm[T]; il lavoro è completato quando viene emesso l’errore relativo all’argomento di tipo mancante e l’Any implicito non si propaga più nei reveal mostrati.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.