-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
type checking on boolean expressions #1871
Comments
and
expression
and
expression
However, 'line and True' does not always return bool. If line is '' it returns that. |
Also see #1477 for the first example. The remaining examples are all correct as @gvanrossum noted ( |
Ah, thanks. Removed simpler |
There's an interesting symmetry though -- in general both |
Here's another example I ran into: Lines 342 to 343 in c528638
The error is misguided: while |
It's odd that that doesn't work, because we actually have an explicit test for that behavior. |
The test has a context that may affect it. This definitely gives an error def f(s: str) -> Optional[str]: pass
x = ''
x = f(x) or x |
Oh, the difference is because the logic for this case goes through the type binder, which only stores information about the types of expressions like |
So what should we do?
|
Both these examples ( |
Fixed by #1989. |
I came across this while reading Daniel Moisset's impression of mypy. He mentions satisfying mypy by wrapping a boolean return expression in
bool()
, which I think is unnecessary. Below is a simple example that mypy fails on withIncompatible return value type (got "Union[str, bool]", expected "bool")
. Note that the return expression is of the form<expression> and <comparison> or <comparison>
.Tested on 64c0acd and 0.4.3 (from
pip
).The text was updated successfully, but these errors were encountered: