Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timhoffm opened this issue Jan 10, 2025 · 0 comments
Labels

Comments

@timhoffm
Copy link

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.

@brianschubert brianschubert added the topic-error-reporting How we report errors label Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants