FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-46479: add typing.reveal_locals by JelleZijlstra · Pull Request #30839 · python/cpython · GitHub

/ cpython Public
Closed
19 changes: 19 additions & 0 deletions Doc/library/typing.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :func:`typing.reveal_locals`. Patch by Jelle Zijlstra.

Back | FazBrowse Home | New Git URL