On this page
typing.assert_type
Prev: typing.assert_never | Up: typing | Next: typing.AsyncContextManager
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.assert_type
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import assert_type
Signature & Overview
typing.assert_type(val, typ, /)
Ask a static type checker to confirm that the value is of the given type.
Usage
from typing import assert_type
x: int = 42
assert assert_type(x, int) == 42
Gotchas
- Static-only check (Python 3.11+): At runtime
assert_type(val, typ)simply returnsvalunchanged—it does not perform anisinstance()check.
REPL Inspect
from typing import assert_type
help(assert_type)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncIterable shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncIterator shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Awaitable shared tags: #stdlib, #typing