python / python/mypy

Make it possible for reveal_type() to format types in the same way as error messages

Aperta
#18,437 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

Add an option to reveal_type() to format the type in the same way as error messages. Something like reveal_type(variable, format="short") or similar API.

Pitch

Example (based on a bug in numpy 2.2):

import numpy as np
from numpy.typing import NDArray

def multiply(image: NDArray[np.float64], factor: float) -> NDArray[np.float64]:
    reveal_type(image * factor)
    return image * factor

results in

test.py:7: note: Revealed type is "numpy.ndarray[builtins.tuple[builtins.int, ...], numpy.dtype[numpy.floating[Any]]]"
test.py:8: error: Incompatible return value type (got "ndarray[tuple[int, ...], dtype[floating[Any]]]", expected "ndarray[tuple[int, ...], dtype[float64]]")  [return-value]

The error is a numpy bug, and not the point of this request. My point is that the type formatting in reveal_type() is different than in the error message, which makes debugging harder. Compare

numpy.ndarray[builtins.tuple[builtins.int, ...], numpy.dtype[numpy.floating[Any]]]
ndarray[tuple[int, ...], dtype[floating[Any]]]

I would like to be able to get reveal_type() to display the type in the same way. Form a quick look, it seems that the error message uses format_type_distinctly() whereas reveal_type() uses the str representation. I assume the this is done intentionally and we don't want to change this by default (if that's not the case, could we just change it?). Therefore. I propose to make this configurable through an optional parameter.

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 tracciando il modo in cui reveal_type() usa attualmente la rappresentazione in forma di stringa e confrontalo con format_type_distinctly(), che l’issue identifica come il formatter dei messaggi di errore. Determina il comportamento dell’API opzionale e verifica che il nuovo formato produca un output dei tipi in stile errore, preservando al contempo il comportamento predefinito esistente.

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.