basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

make typeguards smarter about negation

Open KotlinIsland opened this issue 2 years ago • 0 comments

If we could somehow inspect the implementation and determine if it is a 'full typeguard', or just a 'positive typegaurd'

def is_positive_int(val: int | str) -> TypeGuard[int]:
    return isinstance(val, int) and val > 0

def func(val: int | str):
    if is_positive_int(val):
        reveal_type(val)  # "int"
    else:
        # With the older behavior, the type of "val" is evaluated as
        # "int | str"; with the new behavior, the type is narrowed to
        # "str", which is perhaps not what was intended.
        reveal_type(val) 

KotlinIsland avatar Oct 20 '23 00:10 KotlinIsland