ID EN
Typing

assert_type

Python 3.11

Ask a static type checker to confirm that val has an inferred type of typ.

Syntax

PYTHON
typing.assert_type(val, typ, /)

Examples

Example 1
PYTHON
def greet(name: str) -> None:
    assert_type(name, str)   OK, inferred type of `name` is `str`
    assert_type(name, int)   type checker error
Example 2
PYTHON
def complex_function(arg: object):
     Do some complex type-narrowing logic,
     after which we hope the inferred type will be `int`
    ...
     Test whether the type checker correctly understands our function
    assert_type(arg, int)