basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

Support asymetric type-guards with any value in the false branch

Open KotlinIsland opened this issue 2 years ago • 2 comments

def guard(x: object) -> x is int if True else x is str: ...
def guard(x: object) -> True if True else x is str: ...

likely a subset of #235

KotlinIsland avatar Nov 16 '23 09:11 KotlinIsland

hmm, thinking about this more maybe it doesnt make sense to support it. i cant think of a situation where it'd be correct

def foo(value: object) -> value is str if True else value is int:
    """what code could possibly go here to make this typeguard correct in both cases?"""

DetachHead avatar Nov 16 '23 10:11 DetachHead

hmm, thinking about this more maybe it doesnt make sense to support it. i cant think of a situation where it'd be correct

def foo(value: object) -> value is str if True else value is int:
    """what code could possibly go here to make this typeguard correct in both cases?"""
def f(x: object):
    if isinstance(x, str):
        return True
    if isinstance(x, int):
        return False
    raise Exception

KotlinIsland avatar Nov 16 '23 23:11 KotlinIsland