You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The reveal_type() primitive is injected by type checkers into the builtins. When the type checker sees a call, it prints the inferred type of the argument.
This has been implemented across all type checkers, but adding an implementation to typing would help document the behavior and make it more discoverable for users. Also, it means code with reveal_type() calls can run without runtime errors, useful if you want to run your tests at the same time as you're debugging a typing issue.
The runtime implementation can be very simple:
defreveal_type(obj: _T, /) ->_T:
print("Runtime type is", type(obj))
returnobj
One area of divergence is the return value. Pyright returns the inferred type of the expression as a string (and uses that in its test suite for testing type inference). Mypy returns the argument, which has the advantage that you can insert reveal_type() in the middle of an expression without having to put it on its own line. Also, the Pyright behavior cannot sensibly be implemented at runtime. Therefore, I suggest using Mypy's behavior for typing.reveal_type.
Activity
JelleZijlstra commentedon Jan 17, 2022
The
reveal_type()primitive is injected by type checkers into the builtins. When the type checker sees a call, it prints the inferred type of the argument.This has been implemented across all type checkers, but adding an implementation to
typingwould help document the behavior and make it more discoverable for users. Also, it means code withreveal_type()calls can run without runtime errors, useful if you want to run your tests at the same time as you're debugging a typing issue.The runtime implementation can be very simple:
reveal_type() is supported by all type checkers that I'm aware of (docs include https://google.github.io/pytype/faq.html#can-i-find-out-what-pytype-thinks-the-type-of-my-expression-is for pytype and https://mypy.readthedocs.io/en/stable/common_issues.html#reveal-type for mypy).
One area of divergence is the return value. Pyright returns the inferred type of the expression as a string (and uses that in its test suite for testing type inference). Mypy returns the argument, which has the advantage that you can insert
reveal_type()in the middle of an expression without having to put it on its own line. Also, the Pyright behavior cannot sensibly be implemented at runtime. Therefore, I suggest using Mypy's behavior fortyping.reveal_type.gvanrossum commentedon Feb 2, 2022
New changeset abcc3d7 by Jelle Zijlstra in branch 'main':
bpo-46414: Add typing.reveal_type (bpo-30646)
abcc3d7