Skip to content

Add typing_extensions.reveal_type #1055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions typing_extensions/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Release 4.x.x

- Add `reveal_type`. Backport from bpo-46414.
- Runtime support for PEP 681 and `typing_extensions.dataclass_transform`.
- `Annotated` can now wrap `ClassVar` and `Final`. Backport from
bpo-46491. Patch by Gregory Beauregard (@GBeauregard).
Expand Down
4 changes: 4 additions & 0 deletions typing_extensions/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ This module currently contains the following:
- ``@dataclass_transform()`` (see PEP 681)
- ``NotRequired`` (see PEP 655)
- ``Required`` (see PEP 655)

- In ``typing`` since Python 3.11

- ``reveal_type``
- ``Self`` (see PEP 673)

- In ``typing`` since Python 3.10
Expand Down
8 changes: 7 additions & 1 deletion typing_extensions/src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs, TypeGuard
from typing_extensions import Awaitable, AsyncIterator, AsyncContextManager, Required, NotRequired
from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, overload, final, is_typeddict
from typing_extensions import dataclass_transform
from typing_extensions import dataclass_transform, reveal_type
try:
from typing_extensions import get_type_hints
except ImportError:
Expand Down Expand Up @@ -2346,6 +2346,12 @@ def cached(self): ...
self.assertIs(True, Methods.cached.__final__)


class RevealTypeTests(BaseTestCase):
def test_reveal_type(self):
obj = object()
self.assertIs(obj, reveal_type(obj))


class DataclassTransformTests(BaseTestCase):
def test_decorator(self):
def create_model(*, frozen: bool = False, kw_only: bool = True):
Expand Down
24 changes: 24 additions & 0 deletions typing_extensions/src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _check_generic(cls, parameters):
'NewType',
'overload',
'Protocol',
'reveal_type',
'runtime',
'runtime_checkable',
'Text',
Expand Down Expand Up @@ -2343,6 +2344,29 @@ class Movie(TypedDict):
Required = _Required(_root=True)
NotRequired = _NotRequired(_root=True)

if hasattr(typing, "reveal_type"):
reveal_type = typing.reveal_type
else:
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


if hasattr(typing, 'dataclass_transform'):
dataclass_transform = typing.dataclass_transform
else:
Expand Down