Skip to content

Commit b01caf1

Browse files
miss-islingtonnote35AlexWaygood
authored
[3.12] gh-113255: Clarify docs for typing.reveal_type (GH-113286) (#113323)
gh-113255: Clarify docs for `typing.reveal_type` (GH-113286) (cherry picked from commit 11ee912) Co-authored-by: Kir <[email protected]> Co-authored-by: AlexWaygood <[email protected]>
1 parent 44101e9 commit b01caf1

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
@@ -2581,33 +2581,32 @@ Functions and decorators
25812581

25822582
.. function:: reveal_type(obj, /)
25832583

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

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

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

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

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

2598-
x = reveal_type(1) # Revealed type is "builtins.int"
2599+
x = reveal_type(1) # prints "Runtime type is int"
2600+
print(x) # prints "1"
2601+
2602+
Note that the runtime type may be different from (more or less specific
2603+
than) the type statically inferred by a type checker.
25992604

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

2605-
At runtime, this function prints the runtime type of its argument to stderr
2606-
and returns it unchanged::
2607-
2608-
x = reveal_type(1) # prints "Runtime type is int"
2609-
print(x) # prints "1"
2610-
26112610
.. versionadded:: 3.11
26122611

26132612
.. 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
@@ -3280,7 +3280,7 @@ class re(metaclass=_DeprecatedType):
32803280

32813281

32823282
def reveal_type[T](obj: T, /) -> T:
3283-
"""Reveal the inferred type of a variable.
3283+
"""Ask a static type checker to reveal the inferred type of an expression.
32843284
32853285
When a static type checker encounters a call to ``reveal_type()``,
32863286
it will emit the inferred type of the argument::
@@ -3292,7 +3292,7 @@ def reveal_type[T](obj: T, /) -> T:
32923292
will produce output similar to 'Revealed type is "builtins.int"'.
32933293
32943294
At runtime, the function prints the runtime type of the
3295-
argument and returns it unchanged.
3295+
argument and returns the argument unchanged.
32963296
"""
32973297
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
32983298
return obj

0 commit comments

Comments
 (0)