bpo-46480: add typing.assert_type by JelleZijlstra · Pull Request #30843 · python/cpython (original) (raw)

It occurs to me that users of "assert_type()" may be surprised that it doesn't actually perform an assertion at runtime.

Consider code like:

from typing import assert_type

def parse_int(value) -> int:
    # Hmm. A casual observer would likely read this incorrectly as `assert isinstance(value, str)`...
    assert_type(value, str)
    return int(value)

I wonder if we might consider a word other than "assert", or maybe add the word "static" somewhere. Suggestions: