diff options
author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2022-02-02 02:48:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-02 02:48:55 (GMT) |
commit | abcc3d75f6e570519cb37c62130a2295c6928bff (patch) | |
tree | 8298821cb060cd892de9354eb386ddcf89ae0379 /Doc | |
parent | b1288964e31069bdf81abe560c82874f6f620928 (diff) | |
download | cpython-abcc3d75f6e570519cb37c62130a2295c6928bff.zip cpython-abcc3d75f6e570519cb37c62130a2295c6928bff.tar.gz cpython-abcc3d75f6e570519cb37c62130a2295c6928bff.tar.bz2 |
bpo-46414: Add typing.reveal_type (#30646)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/typing.rst | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index cdfd403..9007c0d 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -1932,6 +1932,37 @@ Functions and decorators runtime we intentionally don't check anything (we want this to be as fast as possible). +.. function:: reveal_type(obj) + + Reveal the inferred static type of an expression. + + When a static type checker encounters a call to this function, + it emits a diagnostic with the type of the argument. For example:: + + x: int = 1 + reveal_type(x) # Revealed type is "builtins.int" + + This can be useful when you want to debug how your type checker + handles a particular piece of code. + + The function returns its argument unchanged, which allows using + it within an expression:: + + x = reveal_type(1) # Revealed type is "builtins.int" + + Most type checkers support ``reveal_type()`` anywhere, even if the + name is not imported from ``typing``. Importing the name from + ``typing`` allows your code to run without runtime errors and + communicates intent more clearly. + + At runtime, this function prints the runtime type of its argument to stderr + and returns it unchanged:: + + x = reveal_type(1) # prints "Runtime type is int" + print(x) # prints "1" + + .. versionadded:: 3.11 + .. decorator:: overload The ``@overload`` decorator allows describing functions and methods |