Skip to content

bpo-46479: add typing.reveal_locals #30839

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

Closed
wants to merge 9 commits into from
Closed
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
19 changes: 19 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,25 @@ Functions and decorators

.. versionadded:: 3.11

.. function:: reveal_locals()

Reveal the inferred static types of all local variables.

When a static type checker encounters a call to this function,
it emits a diagnostic with the types of all variables in the current
scope. For example::

def greet(user_id: int) -> None:
message = f"Hello user #{user_id}"
reveal_locals() # Revealed local types are: user_id: int, message: str

Like :func:`reveal_type`, this function is useful for debugging how
the type checker understands a piece of code.

At runtime, this function does nothing.

.. versionadded:: 3.11

.. decorator:: overload

The ``@overload`` decorator allows describing functions and methods
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import get_type_hints
from typing import get_origin, get_args
from typing import is_typeddict
from typing import reveal_type
from typing import reveal_type, reveal_locals
from typing import no_type_check, no_type_check_decorator
from typing import Type
from typing import NamedTuple, TypedDict
Expand Down Expand Up @@ -5290,13 +5290,16 @@ def bar(self):
self.assertIn('baz', dir(Foo[int]))


class RevealTypeTests(BaseTestCase):
class RevealTests(BaseTestCase):
def test_reveal_type(self):
obj = object()
with captured_stderr() as stderr:
self.assertIs(obj, reveal_type(obj))
self.assertEqual(stderr.getvalue(), "Runtime type is 'object'\n")

def test_reveal_locals(self):
self.assertIsNone(reveal_locals())


class AllTests(BaseTestCase):
"""Tests for __all__."""
Expand Down
21 changes: 21 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def _idfunc(_, x):
'overload',
'ParamSpecArgs',
'ParamSpecKwargs',
'reveal_locals',
'reveal_type',
'runtime_checkable',
'Text',
Expand Down Expand Up @@ -2696,3 +2697,23 @@ def reveal_type(obj: T, /) -> T:
"""
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
return obj


def reveal_locals() -> None:
"""Reveal the inferred static types of all local variables.

When a static type checker encounters a call to this function,
it emits a diagnostic with the types of all variables in the current
scope. For example::

def greet(user_id: int) -> None:
message = f"Hello user #{user_id}"
reveal_locals() # Revealed local types are: user_id: int, message: str

Like :func:`reveal_type`, this function is useful for debugging how
the type checker understands a piece of code.

At runtime, this function does nothing.

"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :func:`typing.reveal_locals`. Patch by Jelle Zijlstra.