diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2023-05-02 06:05:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 06:05:25 (GMT) |
commit | 82ba6ce303d04a7b21034e38d220e23ca9f1dc0a (patch) | |
tree | d80b5e3bb6db8d8dc5e5e93c6fcbe17e081406c0 /Lib/typing.py | |
parent | f0ad4567319ee4ae878d570ab7709ab63df9123e (diff) | |
download | cpython-82ba6ce303d04a7b21034e38d220e23ca9f1dc0a.zip cpython-82ba6ce303d04a7b21034e38d220e23ca9f1dc0a.tar.gz cpython-82ba6ce303d04a7b21034e38d220e23ca9f1dc0a.tar.bz2 |
Improve assert_type phrasing (#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`.
Diffstat (limited to 'Lib/typing.py')
-rw-r--r-- | Lib/typing.py | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index 1a1c989..0dacdd9 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2319,15 +2319,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 |