python / python/mypy

Explicit Any check for TypeGuard

Aperta
#12,049 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-disallow-any topic-typeguard-typeis
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Explicit Any check does not examine the type argument of a typeguard. An example is,

def foo(x: object) -> TypeGuard[Any]:
  ...

when run with mypy --disallow-explicit-any raises no error.

Expected Behavior

I would expect 1 type error on the function definition line.

Your Environment

Using master mypy.

I'm debugging this as the issue was brought up in this pr, that TypeGuardType currently does not implement accept. If it did check TypeGuard type for explicit Any it would end up crashing. So there's two fixes needed here, 1. Check typeguard argument, and 2. Add an accept.

The function signature is checked for Any, but it happens after normalization of typeguard to bool. By the time the Any check occurs on this line while the function signature looks like this,

def (_mypy_marker: Union[builtins.int, builtins.str]) -> TypeGuard[Any]

but if you check ret_type of typ object it shows bool.

My guess is a fix could be done around here, https://github.com/python/mypy/blob/619d9bdf7ae70ce953f1a10aebccfdc3526b7200/mypy/typeanal.py#L591 when typeguard argument get analyzed. Alternatively normalization to bool could be avoided, but removing normalization is probably a bigger change.

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 in mypy/typeanal.py, nell’area dell’analisi di TypeGuard vicino alle righe indicate, e controlla come viene normalizzato TypeGuardType e se implementa accept. Riproduci l’esempio con --disallow-explicit-any; il lavoro è completato quando TypeGuard[Any] produce un errore di tipo sulla definizione della funzione senza causare un arresto anomalo durante l’analisi.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.