This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add typing.reveal_type
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: JelleZijlstra Nosy List: AlexWaygood, JelleZijlstra, cdce8p, gvanrossum, kj, sobolevn, srittau
Priority: normal Keywords: patch

Created on 2022-01-17 15:02 by JelleZijlstra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30646 merged JelleZijlstra, 2022-01-17 18:03
Messages (2)
msg410794 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-01-17 15:02
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:

    def reveal_type(obj: _T, /) -> _T:
        print("Runtime type is", type(obj))
        return obj


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 for `typing.reveal_type`.
msg412333 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2022-02-02 02:49
New changeset abcc3d75f6e570519cb37c62130a2295c6928bff by Jelle Zijlstra in branch 'main':
bpo-46414: Add typing.reveal_type (#30646)
https://github.com/python/cpython/commit/abcc3d75f6e570519cb37c62130a2295c6928bff
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90572
2022-03-05 17:53:23JelleZijlstrasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-02-02 02:49:15gvanrossumsetmessages: + msg412333
2022-01-17 18:07:47srittausetnosy: + srittau
2022-01-17 18:03:16JelleZijlstrasetkeywords: + patch
stage: patch review
pull_requests: + pull_request28848
2022-01-17 16:47:27cdce8psetnosy: + cdce8p
2022-01-17 15:42:27sobolevnsetnosy: + sobolevn
2022-01-17 15:02:22JelleZijlstracreate