-
Notifications
You must be signed in to change notification settings - Fork 237
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
Recommend reveal_type in PEP 484 #277
Comments
I wish |
I use this hack: from typing import TYPE_CHECKING
if not TYPE_CHECKING:
globals()['reveal_type'] = print |
I support making it a builtin. File a bpo issue and write a PR and I will add it. |
One idea I have in addition to @ilevkivskyi's code above is to use the warnings module instead of just printing the output. This allows filtering, adds special handling by test runners etc. |
If Would you be open to Pyright currently implements This allows us to use it in test cases, like this: def func(x: int, y: str):
z = x if x > 3 else y
t1: Literal['int | str'] = reveal_type(z) We use this in thousands of places within pyright's test suites. It would be significant work to change this. |
Hm. In mypy IIRC reveal_type(x) returns x. This makes it possible to wrap arbitrary subexpressions in it without changing their meaning. I would regret losing this. |
You could introduce a new helper with the desired meaning in a pyright-specific header. |
Oh, that's clever. I hadn't considered that. OK, we can introduce a new helper. |
I am implementing this by adding |
This will be in Python 3.11 and the next release of typing-extensions. https://docs.python.org/3.11/library/typing.html#typing.reveal_type |
When #136 will be fixed, it will be nice to have something that could be used in place of
issubclass
for "experimentation" with types. Mypy already provides such a function --reveal_type
. I think it works very well and maybe it is a good idea to add a recommendation to PEP 484 that static type checkers should provide a function to reveal the inferred types? Or maybe even add a functionto
typing.py
so that it can be used for this purpose by other type checkers.The text was updated successfully, but these errors were encountered: