File tree 4 files changed +63
-1
lines changed 4 files changed +63
-1
lines changed Original file line number Diff line number Diff line change @@ -1932,6 +1932,37 @@ Functions and decorators
1932
1932
runtime we intentionally don't check anything (we want this
1933
1933
to be as fast as possible).
1934
1934
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
+
1935
1966
.. decorator :: overload
1936
1967
1937
1968
The ``@overload `` decorator allows describing functions and methods
Original file line number Diff line number Diff line change 20
20
from typing import get_type_hints
21
21
from typing import get_origin , get_args
22
22
from typing import is_typeddict
23
+ from typing import reveal_type
23
24
from typing import no_type_check , no_type_check_decorator
24
25
from typing import Type
25
26
from typing import NamedTuple , TypedDict
34
35
import weakref
35
36
import types
36
37
37
- from test .support import import_helper
38
+ from test .support import import_helper , captured_stderr
38
39
from test import mod_generics_cache
39
40
from test import _typed_dict_helper
40
41
@@ -5289,6 +5290,14 @@ def bar(self):
5289
5290
self .assertIn ('baz' , dir (Foo [int ]))
5290
5291
5291
5292
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
+
5292
5301
class AllTests (BaseTestCase ):
5293
5302
"""Tests for __all__."""
5294
5303
Original file line number Diff line number Diff line change @@ -130,6 +130,7 @@ def _idfunc(_, x):
130
130
'overload' ,
131
131
'ParamSpecArgs' ,
132
132
'ParamSpecKwargs' ,
133
+ 'reveal_type' ,
133
134
'runtime_checkable' ,
134
135
'Text' ,
135
136
'TYPE_CHECKING' ,
@@ -2675,3 +2676,23 @@ class re(metaclass=_DeprecatedType):
2675
2676
2676
2677
re .__name__ = __name__ + '.re'
2677
2678
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
Original file line number Diff line number Diff line change
1
+ Add :func: `typing.reveal_type `. Patch by Jelle Zijlstra.
You can’t perform that action at this time.
0 commit comments