ID EN
Typing

assert_type

Python 3.11 🇮🇩 Bahasa Indonesia

Mintalah pemeriksa tipe statis untuk mengonfirmasi bahwa val memiliki tipe tipe yang disimpulkan.

Syntax

PYTHON
typing.assert_type(val, typ, /)

Contoh

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)