]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Improve assert_type phrasing (#104081)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Tue, 2 May 2023 06:05:25 +0000 (23:05 -0700)
committerGitHub <noreply@github.com>
Tue, 2 May 2023 06:05:25 +0000 (23:05 -0700)
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
Lib/typing.py

index 409a95d528b5d359aab2cc6a00a04eea3a1c992d..c22fc0b28a50d0a7b78a7e5f2ba12373e2f711fd 100644 (file)
@@ -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::
 
index 1a1c989dbaf37da97a75c9e8cff7ddfa07e978ab..0dacdd9031a77628bddb9642de55e61e5c5af02b 100644 (file)
@@ -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