aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_typing.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2022-03-16 20:02:26 -0700
committerGitHub <noreply@github.com>2022-03-16 20:02:26 -0700
commit96568e995d840c66edb25b6b9d85e4dcccf5a936 (patch)
tree907a903c2807fb7437b4e5a5779d034609320a9f /Lib/test/test_typing.py
parent7c353b7594545fb9403b3123a17ad06cadc2f73d (diff)
downloadcpython-96568e995d840c66edb25b6b9d85e4dcccf5a936.tar.gz
cpython-96568e995d840c66edb25b6b9d85e4dcccf5a936.zip
bpo-46480: add typing.assert_type (GH-30843)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: David Foster <david@dafoster.net>
Diffstat (limited to 'Lib/test/test_typing.py')
-rw-r--r--Lib/test/test_typing.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index b212b523048..e88f7322b2f 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -16,7 +16,7 @@ from typing import Union, Optional, Literal
from typing import Tuple, List, Dict, MutableMapping
from typing import Callable
from typing import Generic, ClassVar, Final, final, Protocol
-from typing import cast, runtime_checkable
+from typing import assert_type, cast, runtime_checkable
from typing import get_type_hints
from typing import get_origin, get_args
from typing import is_typeddict
@@ -3302,6 +3302,22 @@ class CastTests(BaseTestCase):
cast('hello', 42)
+class AssertTypeTests(BaseTestCase):
+
+ def test_basics(self):
+ arg = 42
+ self.assertIs(assert_type(arg, int), arg)
+ self.assertIs(assert_type(arg, str | float), arg)
+ self.assertIs(assert_type(arg, AnyStr), arg)
+ self.assertIs(assert_type(arg, None), arg)
+
+ def test_errors(self):
+ # Bogus calls are not expected to fail.
+ arg = 42
+ self.assertIs(assert_type(arg, 42), arg)
+ self.assertIs(assert_type(arg, 'hello'), arg)
+
+
# We need this to make sure that `@no_type_check` respects `__module__` attr:
from test import ann_module8