Closed
Description
Mypy complains about this code, but perhaps it shouldn't (I'm assuming no --strict-optional
):
class A: pass
def f(a: A, b: bool) -> bool:
return a and b # Incompatible return value type (got "Union[A, bool]", expected "bool")
f
may return a bool
object (if a
is not None
) or None
(if a
is None
) but it can never return an A
instance (unless a subclass of A
defines __bool__
-- which we might not care about) so the inferred type for the return value may not be what we want.
I think I saw some real-world false positives caused by this, though I didn't look very carefully.
When using --strict-optional
, we'll probably require using bool(...)
around the return value since the type would otherwise be Optional[bool]
, which is not compatible with bool
.
(I remember having a similar earlier discussion but couldn't find an existing issue.)