File tree Expand file tree Collapse file tree 4 files changed +63
-1
lines changed Expand file tree Collapse file tree 4 files changed +63
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 2020from typing import get_type_hints
2121from typing import get_origin , get_args
2222from typing import is_typeddict
23+ from typing import reveal_type
2324from typing import no_type_check , no_type_check_decorator
2425from typing import Type
2526from typing import NamedTuple , TypedDict
3435import weakref
3536import types
3637
37- from test .support import import_helper
38+ from test .support import import_helper , captured_stderr
3839from test import mod_generics_cache
3940from 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+
52925301class AllTests (BaseTestCase ):
52935302 """Tests for __all__."""
52945303
Original file line number Diff line number Diff 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
26762677re .__name__ = __name__ + '.re'
26772678sys .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