result icon indicating copy to clipboard operation
result copied to clipboard

Changing the Err type E with and_then() passes type checking, but should not

Open JLessinger opened this issue 1 year ago • 0 comments

The following type checks (pyright, all checks enabled):

T_cov = TypeVar("T_cov", covariant=True)

StrResult = Result[T_cov, str]
ExceptionResult = Result[T_cov, Exception]


def foo() -> StrResult[int]:
    return Ok(1)


def baz(x: int) -> ExceptionResult[int]:
    return Ok(x)


r1 = foo()
x = r1.and_then(baz)

In particular, x is inferred to be a x: StrResult[int] | Err[Exception].

The expression r1.and_then(baz) should not type check: pyright should report that a StrResult is not compatible with a int -> ExceptionResult because the Err types differ.

JLessinger avatar Jul 13 '24 16:07 JLessinger