summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index dd68e71..6930f5d 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -118,6 +118,7 @@ __all__ = [
# One-off things.
'AnyStr',
+ 'assert_type',
'assert_never',
'cast',
'final',
@@ -2093,6 +2094,22 @@ def cast(typ, val):
return val
+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
+
+
_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
types.MethodType, types.ModuleType,
WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)