python / python/mypy

`reveal_type` should print out nice formatted types

Aperta
#6,526 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

priority-1-normal topic-reveal-type topic-tests topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Extracted from #6460

Say you have

lx: Optional[Tuple[str, int]] = None
lx[0]
reveal_type(lx)

mypy outputs

Value of type "Optional[Tuple[str, int]]" is not indexable
Revealed type is 'Union[Tuple[builtins.str, builtins.int], None]'

As you can see (and probably know) it shows two different representations for the inferred type. For user ergonomics the revealed type should be 'Optional[Tuple[int, str]]' as well, no?

The maybe too naive but works for me change is

@@ -1051,7 +1051,7 @@ class MessageBuilder:
         self.fail('Invalid signature "{}" for "{}"'.format(func_type, method_name), context)
 
     def reveal_type(self, typ: Type, context: Context) -> None:
-        self.fail('Revealed type is \'{}\''.format(typ), context)
+        self.fail('Revealed type is {}'.format(self.format(typ)), context)
 
     def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -> None:
         # To ensure that the output is predictable on Python < 3.6,

But then you get 1.000.000 failures which is expected because the tests rely on the verbose output.

E.g.

Expected:
  main:10: error: Revealed type is 'builtins.int' (diff)
  main:11: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
  main:13: error: Revealed type is 'Literal[1]' (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is 'Tuple[builtins.int, builtins.int]' (diff)
Actual:
  main:10: error: Revealed type is "int"        (diff)
  main:11: error: Revealed type is "Tuple[int, int]" (diff)
  main:13: error: Revealed type is "Literal[1]" (diff)
  main:14: error: Argument 1 to "force2" has incompatible type "Tuple[int, in...
  main:14: error: Revealed type is "Tuple[int, int]" (diff)

This makes it a non-trivial 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 da MessageBuilder.reveal_type, dove l’issue mostra l’attuale costruzione dell’output, e confrontala con l’output del tipo formattato esistente. Aggiorna le aspettative reveal_type interessate nell’intera suite di test in modo che i tipi rivelati usino la formattazione visibile all’utente, quindi esegui i test mypy per confermare le diagnostiche aggiornate.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
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.