summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2022-02-02 02:48:55 (GMT)
committerGitHub <noreply@github.com>2022-02-02 02:48:55 (GMT)
commitabcc3d75f6e570519cb37c62130a2295c6928bff (patch)
tree8298821cb060cd892de9354eb386ddcf89ae0379 /Lib/typing.py
parentb1288964e31069bdf81abe560c82874f6f620928 (diff)
downloadcpython-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 'Lib/typing.py')
-rw-r--r--Lib/typing.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index dac9c6c..0cf9755 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -130,6 +130,7 @@ __all__ = [
'overload',
'ParamSpecArgs',
'ParamSpecKwargs',
+ 'reveal_type',
'runtime_checkable',
'Text',
'TYPE_CHECKING',
@@ -2675,3 +2676,23 @@ class re(metaclass=_DeprecatedType):
re.__name__ = __name__ + '.re'
sys.modules[re.__name__] = re
+
+
+def reveal_type(obj: T, /) -> T:
+ """Reveal the inferred type of a variable.
+
+ When a static type checker encounters a call to ``reveal_type()``,
+ it will emit the inferred type of the argument::
+
+ x: int = 1
+ reveal_type(x)
+
+ Running a static type checker (e.g., ``mypy``) on this example
+ will produce output similar to 'Revealed type is "builtins.int"'.
+
+ At runtime, the function prints the runtime type of the
+ argument and returns it unchanged.
+
+ """
+ print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
+ return obj