mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.11] Improve assert_type phrasing (GH-104081) (#104084)
Improve assert_type phrasing (GH-104081)
I'd like to make the fact that this does nothing at runtime
really obvious, since I suspect this is unintuitive for users who are
unfamiliar with static type checking.
I thought of this because of
https://discuss.python.org/t/add-arg-check-type-to-types/26384
wherein I'm skeptical that the user really did want `assert_type`.
(cherry picked from commit 82ba6ce303
)
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
This commit is contained in:
parent
0d40264325
commit
3e7e50e656
2 changed files with 8 additions and 6 deletions
|
@ -2250,15 +2250,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.
|
||||
|
||||
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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue