Typing and Generics

typing.assert_type

Requests a static diagnostic if the inferred type of val is not the requested type. Returns val unchanged at runtime; it is not a runtime assertion. Added in 3.11.

Formal Syntax & Structure

python syntaxpython
typing.assert_type(val, typ, /)

Parameters & Attributes

NameTypeDescription
valobjectValue to check statically.
typtype expressionExpected inferred type.

Return Value

object

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import assert_type
value=7print(assert_type(value,int))
Python Reference