basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

equality checks should narrow tuple types

Open AlexBlandin opened this issue 1 year ago • 2 comments

Version: BasedPyright v1.12.0

Cases within match statement do not exhaustively handle all values
  Unhandled type: "tuple[bool, bool]"
  If exhaustive handling is not intended, add "case _: pass"

basedpyright(reportMatchNotExhaustive)

As seen below, the snippet foo has this error on a, b, while bar passes.


def foo(a: bool, b: bool) -> None:
  match a, b:
    case (True, True):
      pass
    case (True, False):
      pass
    case (False, True):
      pass
    case (False, False):
      pass


def bar(a: bool) -> None:
  match (a,):
    case (True,):
      pass
    case (False,):
      pass

AlexBlandin avatar May 08 '24 14:05 AlexBlandin

it's not just match statements. the issue is that equality checks on tuples don't narrow their type:

from typing import assert_never

def bar(a: tuple[bool]) -> None:
    if a == (True,):
        ...
    elif a == (False,):
        ...
    else:
        assert_never(a) # error: Type "tuple[bool]" is incompatible with type "Never"

looks like this has been raised upstream but was rejected: https://github.com/microsoft/pyright/issues/6437

DetachHead avatar May 08 '24 14:05 DetachHead

looks like it was fixed upstream for match statements but not if statements

DetachHead avatar Jul 31 '24 10:07 DetachHead