Skip to content

Commit eeb29ad

Browse files
Add typing_extensions.reveal_type (#1055)
1 parent 59e5918 commit eeb29ad

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

typing_extensions/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Release 4.x.x
22

3+
- Add `reveal_type`. Backport from bpo-46414.
34
- Runtime support for PEP 681 and `typing_extensions.dataclass_transform`.
45
- `Annotated` can now wrap `ClassVar` and `Final`. Backport from
56
bpo-46491. Patch by Gregory Beauregard (@GBeauregard).

typing_extensions/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ This module currently contains the following:
4040
- ``@dataclass_transform()`` (see PEP 681)
4141
- ``NotRequired`` (see PEP 655)
4242
- ``Required`` (see PEP 655)
43+
44+
- In ``typing`` since Python 3.11
45+
46+
- ``reveal_type``
4347
- ``Self`` (see PEP 673)
4448

4549
- In ``typing`` since Python 3.10

typing_extensions/src/test_typing_extensions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs, TypeGuard
2323
from typing_extensions import Awaitable, AsyncIterator, AsyncContextManager, Required, NotRequired
2424
from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, overload, final, is_typeddict
25-
from typing_extensions import dataclass_transform
25+
from typing_extensions import dataclass_transform, reveal_type
2626
try:
2727
from typing_extensions import get_type_hints
2828
except ImportError:
@@ -2346,6 +2346,12 @@ def cached(self): ...
23462346
self.assertIs(True, Methods.cached.__final__)
23472347

23482348

2349+
class RevealTypeTests(BaseTestCase):
2350+
def test_reveal_type(self):
2351+
obj = object()
2352+
self.assertIs(obj, reveal_type(obj))
2353+
2354+
23492355
class DataclassTransformTests(BaseTestCase):
23502356
def test_decorator(self):
23512357
def create_model(*, frozen: bool = False, kw_only: bool = True):

typing_extensions/src/typing_extensions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def _check_generic(cls, parameters):
7878
'NewType',
7979
'overload',
8080
'Protocol',
81+
'reveal_type',
8182
'runtime',
8283
'runtime_checkable',
8384
'Text',
@@ -2343,6 +2344,29 @@ class Movie(TypedDict):
23432344
Required = _Required(_root=True)
23442345
NotRequired = _NotRequired(_root=True)
23452346

2347+
if hasattr(typing, "reveal_type"):
2348+
reveal_type = typing.reveal_type
2349+
else:
2350+
def reveal_type(__obj: T) -> T:
2351+
"""Reveal the inferred type of a variable.
2352+
2353+
When a static type checker encounters a call to ``reveal_type()``,
2354+
it will emit the inferred type of the argument::
2355+
2356+
x: int = 1
2357+
reveal_type(x)
2358+
2359+
Running a static type checker (e.g., ``mypy``) on this example
2360+
will produce output similar to 'Revealed type is "builtins.int"'.
2361+
2362+
At runtime, the function prints the runtime type of the
2363+
argument and returns it unchanged.
2364+
2365+
"""
2366+
print(f"Runtime type is {type(__obj).__name__!r}", file=sys.stderr)
2367+
return __obj
2368+
2369+
23462370
if hasattr(typing, 'dataclass_transform'):
23472371
dataclass_transform = typing.dataclass_transform
23482372
else:

0 commit comments

Comments
 (0)