From 3c804594214c7a3916d8183fa11dff1c77bae4a5 Mon Sep 17 00:00:00 2001 From: hauntsaninja <hauntsaninja@gmail.com> Date: Mon, 1 May 2023 21:30:18 -0700 Subject: [PATCH 1/2] Improve assert_type phrasing 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`. --- Doc/library/typing.rst | 7 ++++--- Lib/typing.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 409a95d528b5d3..c22fc0b28a50d0 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -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:: diff --git a/Lib/typing.py b/Lib/typing.py index 354bc80eb3abfa..40ac7e9132da81 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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. + + 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 From 9376fbaf7dd0c451b0241129747ff5ea330f070b Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 1 May 2023 21:44:04 -0700 Subject: [PATCH 2/2] Update Lib/typing.py Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> --- Lib/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/typing.py b/Lib/typing.py index 40ac7e9132da81..e743d8ab39d2ea 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2309,7 +2309,7 @@ def assert_type(val, typ, /): """Ask a static type checker to confirm that the value is of the given type. 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. + 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::