Skip to content

Commit 11ee912

Browse files
note35AlexWaygood
andauthored
gh-113255: Clarify docs for typing.reveal_type (#113286)
Co-authored-by: AlexWaygood <[email protected]>
1 parent de8a4e5 commit 11ee912

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

26052605
.. function:: reveal_type(obj, /)
26062606

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

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

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

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

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

2621-
x = reveal_type(1) # Revealed type is "builtins.int"
2622+
x = reveal_type(1) # prints "Runtime type is int"
2623+
print(x) # prints "1"
2624+
2625+
Note that the runtime type may be different from (more or less specific
2626+
than) the type statically inferred by a type checker.
26222627

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

2628-
At runtime, this function prints the runtime type of its argument to stderr
2629-
and returns it unchanged::
2630-
2631-
x = reveal_type(1) # prints "Runtime type is int"
2632-
print(x) # prints "1"
2633-
26342633
.. versionadded:: 3.11
26352634

26362635
.. 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
@@ -3301,7 +3301,7 @@ def __enter__(self) -> 'TextIO':
33013301

33023302

33033303
def reveal_type[T](obj: T, /) -> T:
3304-
"""Reveal the inferred type of a variable.
3304+
"""Ask a static type checker to reveal the inferred type of an expression.
33053305
33063306
When a static type checker encounters a call to ``reveal_type()``,
33073307
it will emit the inferred type of the argument::
@@ -3313,7 +3313,7 @@ def reveal_type[T](obj: T, /) -> T:
33133313
will produce output similar to 'Revealed type is "builtins.int"'.
33143314
33153315
At runtime, the function prints the runtime type of the
3316-
argument and returns it unchanged.
3316+
argument and returns the argument unchanged.
33173317
"""
33183318
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
33193319
return obj

0 commit comments

Comments
 (0)