Skip to content

Commit

Permalink
[3.11] gh-113255: Clarify docs for typing.reveal_type (#113286) (#1…
Browse files Browse the repository at this point in the history
…13326)

(cherry-picked from commit 11ee912)

Co-authored-by: Kir <note351@hotmail.com>
  • Loading branch information
AlexWaygood and note35 committed Dec 20, 2023
1 parent 899f1c0 commit d65dfc8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2323,33 +2323,32 @@ Functions and decorators

.. function:: reveal_type(obj, /)

Reveal the inferred static type of an expression.
Ask a static type checker to reveal the inferred type of an expression.

When a static type checker encounters a call to this function,
it emits a diagnostic with the type of the argument. For example::
it emits a diagnostic with the inferred type of the argument. For example::

x: int = 1
reveal_type(x) # Revealed type is "builtins.int"

This can be useful when you want to debug how your type checker
handles a particular piece of code.

The function returns its argument unchanged, which allows using
it within an expression::
At runtime, this function prints the runtime type of its argument to
:data:`sys.stderr` and returns the argument unchanged (allowing the call to
be used within an expression)::

x = reveal_type(1) # Revealed type is "builtins.int"
x = reveal_type(1) # prints "Runtime type is int"
print(x) # prints "1"

Note that the runtime type may be different from (more or less specific
than) the type statically inferred by a type checker.

Most type checkers support ``reveal_type()`` anywhere, even if the
name is not imported from ``typing``. Importing the name from
``typing`` allows your code to run without runtime errors and
``typing``, however, allows your code to run without runtime errors and
communicates intent more clearly.

At runtime, this function prints the runtime type of its argument to stderr
and returns it unchanged::

x = reveal_type(1) # prints "Runtime type is int"
print(x) # prints "1"

.. versionadded:: 3.11

.. decorator:: dataclass_transform(*, eq_default=True, order_default=False, \
Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3419,7 +3419,7 @@ class re(metaclass=_DeprecatedType):


def reveal_type(obj: T, /) -> T:
"""Reveal the inferred type of a variable.
"""Ask a static type checker to reveal the inferred type of an expression.
When a static type checker encounters a call to ``reveal_type()``,
it will emit the inferred type of the argument::
Expand All @@ -3431,7 +3431,7 @@ def reveal_type(obj: T, /) -> T:
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.
argument and returns the argument unchanged.
"""
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
return obj
Expand Down

0 comments on commit d65dfc8

Please sign in to comment.