Skip to content

Commit d65dfc8

Browse files
AlexWaygoodnote35
andauthored
[3.11] gh-113255: Clarify docs for typing.reveal_type (#113286) (#113326)
(cherry-picked from commit 11ee912) Co-authored-by: Kir <[email protected]>
1 parent 899f1c0 commit d65dfc8

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

Doc/library/typing.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,33 +2323,32 @@ Functions and decorators
23232323

23242324
.. function:: reveal_type(obj, /)
23252325

2326-
Reveal the inferred static type of an expression.
2326+
Ask a static type checker to reveal the inferred type of an expression.
23272327

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

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

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

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

2340-
x = reveal_type(1) # Revealed type is "builtins.int"
2341+
x = reveal_type(1) # prints "Runtime type is int"
2342+
print(x) # prints "1"
2343+
2344+
Note that the runtime type may be different from (more or less specific
2345+
than) the type statically inferred by a type checker.
23412346

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

2347-
At runtime, this function prints the runtime type of its argument to stderr
2348-
and returns it unchanged::
2349-
2350-
x = reveal_type(1) # prints "Runtime type is int"
2351-
print(x) # prints "1"
2352-
23532352
.. versionadded:: 3.11
23542353

23552354
.. decorator:: dataclass_transform(*, eq_default=True, order_default=False, \

Lib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,7 +3419,7 @@ class re(metaclass=_DeprecatedType):
34193419

34203420

34213421
def reveal_type(obj: T, /) -> T:
3422-
"""Reveal the inferred type of a variable.
3422+
"""Ask a static type checker to reveal the inferred type of an expression.
34233423
34243424
When a static type checker encounters a call to ``reveal_type()``,
34253425
it will emit the inferred type of the argument::
@@ -3431,7 +3431,7 @@ def reveal_type(obj: T, /) -> T:
34313431
will produce output similar to 'Revealed type is "builtins.int"'.
34323432
34333433
At runtime, the function prints the runtime type of the
3434-
argument and returns it unchanged.
3434+
argument and returns the argument unchanged.
34353435
"""
34363436
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
34373437
return obj

0 commit comments

Comments
 (0)