basedmypy
basedmypy copied to clipboard
make typeguards smarter about negation
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)