Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abcc3d7

Browse files
bpo-46414: Add typing.reveal_type (#30646)
Co-authored-by: Nikita Sobolev <[email protected]>
1 parent b128896 commit abcc3d7

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

Doc/library/typing.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,37 @@ Functions and decorators
19321932
runtime we intentionally don't check anything (we want this
19331933
to be as fast as possible).
19341934

1935+
.. function:: reveal_type(obj)
1936+
1937+
Reveal the inferred static type of an expression.
1938+
1939+
When a static type checker encounters a call to this function,
1940+
it emits a diagnostic with the type of the argument. For example::
1941+
1942+
x: int = 1
1943+
reveal_type(x) # Revealed type is "builtins.int"
1944+
1945+
This can be useful when you want to debug how your type checker
1946+
handles a particular piece of code.
1947+
1948+
The function returns its argument unchanged, which allows using
1949+
it within an expression::
1950+
1951+
x = reveal_type(1) # Revealed type is "builtins.int"
1952+
1953+
Most type checkers support ``reveal_type()`` anywhere, even if the
1954+
name is not imported from ``typing``. Importing the name from
1955+
``typing`` allows your code to run without runtime errors and
1956+
communicates intent more clearly.
1957+
1958+
At runtime, this function prints the runtime type of its argument to stderr
1959+
and returns it unchanged::
1960+
1961+
x = reveal_type(1) # prints "Runtime type is int"
1962+
print(x) # prints "1"
1963+
1964+
.. versionadded:: 3.11
1965+
19351966
.. decorator:: overload
19361967

19371968
The ``@overload`` decorator allows describing functions and methods

Lib/test/test_typing.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import get_type_hints
2121
from typing import get_origin, get_args
2222
from typing import is_typeddict
23+
from typing import reveal_type
2324
from typing import no_type_check, no_type_check_decorator
2425
from typing import Type
2526
from typing import NamedTuple, TypedDict
@@ -34,7 +35,7 @@
3435
import weakref
3536
import types
3637

37-
from test.support import import_helper
38+
from test.support import import_helper, captured_stderr
3839
from test import mod_generics_cache
3940
from test import _typed_dict_helper
4041

@@ -5289,6 +5290,14 @@ def bar(self):
52895290
self.assertIn('baz', dir(Foo[int]))
52905291

52915292

5293+
class RevealTypeTests(BaseTestCase):
5294+
def test_reveal_type(self):
5295+
obj = object()
5296+
with captured_stderr() as stderr:
5297+
self.assertIs(obj, reveal_type(obj))
5298+
self.assertEqual(stderr.getvalue(), "Runtime type is 'object'\n")
5299+
5300+
52925301
class AllTests(BaseTestCase):
52935302
"""Tests for __all__."""
52945303

Lib/typing.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def _idfunc(_, x):
130130
'overload',
131131
'ParamSpecArgs',
132132
'ParamSpecKwargs',
133+
'reveal_type',
133134
'runtime_checkable',
134135
'Text',
135136
'TYPE_CHECKING',
@@ -2675,3 +2676,23 @@ class re(metaclass=_DeprecatedType):
26752676

26762677
re.__name__ = __name__ + '.re'
26772678
sys.modules[re.__name__] = re
2679+
2680+
2681+
def reveal_type(obj: T, /) -> T:
2682+
"""Reveal the inferred type of a variable.
2683+
2684+
When a static type checker encounters a call to ``reveal_type()``,
2685+
it will emit the inferred type of the argument::
2686+
2687+
x: int = 1
2688+
reveal_type(x)
2689+
2690+
Running a static type checker (e.g., ``mypy``) on this example
2691+
will produce output similar to 'Revealed type is "builtins.int"'.
2692+
2693+
At runtime, the function prints the runtime type of the
2694+
argument and returns it unchanged.
2695+
2696+
"""
2697+
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
2698+
return obj
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`typing.reveal_type`. Patch by Jelle Zijlstra.

0 commit comments

Comments
 (0)