Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve assert_type phrasing #104081

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2484,15 +2484,16 @@ Functions and decorators

Ask a static type checker to confirm that *val* has an inferred type of *typ*.

When the type checker encounters a call to ``assert_type()``, it
At runtime this does nothing: it returns the first argument unchanged with no
checks or side effects, no matter the actual type of the argument.

When a static type checker encounters a call to ``assert_type()``, it
emits an error if the value is not of the specified type::

def greet(name: str) -> None:
assert_type(name, str) # OK, inferred type of `name` is `str`
assert_type(name, int) # type checker error

At runtime this returns the first argument unchanged with no side effects.

This function is useful for ensuring the type checker's understanding of a
script is in line with the developer's intentions::

Expand Down
7 changes: 4 additions & 3 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,15 +2308,16 @@ def cast(typ, val):
def assert_type(val, typ, /):
"""Ask a static type checker to confirm that the value is of the given type.

When the type checker encounters a call to assert_type(), it
At runtime this does nothing: it returns the first argument unchanged with no
checks or side effects, no matter the actual type of the argument.
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

When a static type checker encounters a call to assert_type(), it
emits an error if the value is not of the specified type::

def greet(name: str) -> None:
assert_type(name, str) # ok
assert_type(name, int) # type checker error

At runtime this returns the first argument unchanged and otherwise
does nothing.
"""
return val

Expand Down