aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2022-03-22 15:29:58 -0700
committerGitHub <noreply@github.com>2022-03-22 23:29:58 +0100
commitdb20497ae1be03d89e9cd083aa93cdff9c786831 (patch)
tree5598784731745cd541a3172c2c3a29270be1be0d
parent07fb800b29127a46a9c422d4dbc4e4d96df69d3f (diff)
downloadtyping-db20497ae1be03d89e9cd083aa93cdff9c786831.tar.gz
Add assert_type (#1103)
-rw-r--r--typing_extensions/CHANGELOG1
-rw-r--r--typing_extensions/README.rst1
-rw-r--r--typing_extensions/src/test_typing_extensions.py19
-rw-r--r--typing_extensions/src/typing_extensions.py21
4 files changed, 40 insertions, 2 deletions
diff --git a/typing_extensions/CHANGELOG b/typing_extensions/CHANGELOG
index f098464..a9a5980 100644
--- a/typing_extensions/CHANGELOG
+++ b/typing_extensions/CHANGELOG
@@ -1,5 +1,6 @@
# Unreleased
+- Add `typing.assert_type`. Backport from bpo-46480.
- Drop support for Python 3.6. Original patch by Adam Turner (@AA-Turner).
# Release 4.1.1 (February 13, 2022)
diff --git a/typing_extensions/README.rst b/typing_extensions/README.rst
index d29d9f5..9abed04 100644
--- a/typing_extensions/README.rst
+++ b/typing_extensions/README.rst
@@ -46,6 +46,7 @@ This module currently contains the following:
- In ``typing`` since Python 3.11
- ``assert_never``
+ - ``assert_type``
- ``LiteralString`` (see PEP 675)
- ``Never``
- ``NotRequired`` (see PEP 655)
diff --git a/typing_extensions/src/test_typing_extensions.py b/typing_extensions/src/test_typing_extensions.py
index 53a8343..b8fe5e3 100644
--- a/typing_extensions/src/test_typing_extensions.py
+++ b/typing_extensions/src/test_typing_extensions.py
@@ -12,7 +12,7 @@ import types
from unittest import TestCase, main, skipUnless, skipIf
from test import ann_module, ann_module2, ann_module3
import typing
-from typing import TypeVar, Optional, Union, Any
+from typing import TypeVar, Optional, Union, Any, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Tuple, List, Dict, Iterable, Iterator, Callable
from typing import Generic, NamedTuple
@@ -23,7 +23,7 @@ from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs,
from typing_extensions import Awaitable, AsyncIterator, AsyncContextManager, Required, NotRequired
from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, overload, final, is_typeddict
from typing_extensions import TypeVarTuple, Unpack, dataclass_transform, reveal_type, Never, assert_never, LiteralString
-from typing_extensions import get_type_hints, get_origin, get_args
+from typing_extensions import assert_type, get_type_hints, get_origin, get_args
# Flags used to mark tests that only apply after a specific
# version of the typing module.
@@ -425,6 +425,21 @@ class OverloadTests(BaseTestCase):
blah()
+class AssertTypeTests(BaseTestCase):
+
+ def test_basics(self):
+ arg = 42
+ self.assertIs(assert_type(arg, int), arg)
+ self.assertIs(assert_type(arg, Union[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)
+
T_a = TypeVar('T_a')
diff --git a/typing_extensions/src/typing_extensions.py b/typing_extensions/src/typing_extensions.py
index 5c43354..5698a76 100644
--- a/typing_extensions/src/typing_extensions.py
+++ b/typing_extensions/src/typing_extensions.py
@@ -717,6 +717,27 @@ else:
"""
return isinstance(tp, tuple(_TYPEDDICT_TYPES))
+
+if hasattr(typing, "assert_type"):
+ assert_type = typing.assert_type
+
+else:
+ def assert_type(__val, __typ):
+ """Assert (to the type checker) that the value is of the given type.
+
+ When the 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
+
+
if hasattr(typing, "Required"):
get_type_hints = typing.get_type_hints
else: