Repository navigation
Check __bool__ type for the left operand of and/or - #11823
Tomi Belan (TomiBelan) wants to merge 2 commits into
Conversation
pyright allows you to declare that a type can't be used in a conditional context, e.g. `if obj:`, `while obj:`. When it was originally added in 294a97d, pyright also checked the operand types of `and`/`or` operators. In microsoft#9225, it was pointed out that this leads to false positives - `None or obj` and `1 or obj` are valid Python code and shouldn't be a type error. PR microsoft#9234 fixed the false positive by removing the check on `and`/`or`. But that's not the best fix. It solved the false positive, but introduced a false negative. The Goldilocks fix is to check only the *left* operand, because the interpreter needs it to be convertible to bool, but not the *right* operand. This PR reintroduces half the check that was removed in PR microsoft#9234, for the left operand only. Updates microsoft#9225 (technically)
|
Compared candidate View the full release history charts with the base and PR results. Execution time historyPeak memory historyType checker benchmark🟢 No performance regressions detected. Regression threshold:
Pyright stats
|
🔴 Pyright CLI QA —
|
Yes. In your main.py example, That said: I find this example quite confusing. Where is it from - is it committed somewhere or did you write it? How did you come up with it? What was the idea? I'm also not really sure why pyright thinks |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
There's a duplicate diagnostic when the boolean expression is already in a conditional:
class BadInt:
def __bool__(self) -> int:
return 1
def f(c: BadInt):
if c or 1:
passBase reports one invalid conditional operand error on c; head reports two. The existing whole-condition check and the new left-operand check both catch it. A chain like if c or d or 1 reports three on head, and and chains can duplicate the error too.
Python rejects that __bool__ at runtime, so the new check is useful. Could we avoid reporting the same operand more than once while keeping detection for expressions used as values? A test for the conditional and a chain would help.
|
Rich Chiodo (@rchiodo) Thanks for taking a look!
$ git rev-parse HEAD
5f27db8dfeb3b9ae8ba80f9da6fd4f3fec04e67a
$ git status
On branch andor-a-star-wars-story
nothing to commit, working tree clean
$ cat ../badints.py
class BadInt:
def __bool__(self) -> int:
return 1
def f(c: BadInt):
if c or 1:
pass
def g(c: BadInt, d: BadInt):
if c or d or 1:
pass
$ pnpx pyright ../badints.py
/home/tomi/badints.py
/home/tomi/badints.py:6:8 - error: Invalid conditional operand of type "BadInt | Literal[1]"
Method __bool__ for type "BadInt" returns type "int" rather than "bool" (reportGeneralTypeIssues)
/home/tomi/badints.py:10:8 - error: Invalid conditional operand of type "BadInt | Literal[1]"
Method __bool__ for type "BadInt" returns type "int" rather than "bool" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations
EDIT: D'oh! I should've used |
|
Good catch Rich Chiodo (@rchiodo). If you don't mind, I could use some advice: I think the best way to prevent multiple diagnostics is that the bad My dilemma is that the expression's return type is decided by I suppose I could try to move (large parts of) Do you have any ideas? |
|
I'd avoid returning class Bad:
def __bool__(self) -> int:
return 1
def f(b: Bad) -> None:
result = b or "fallback"
reveal_type(result)One possible direction, not a tested patch, is to keep inference unchanged and deduplicate in the checker: the binary-operation visitor checks each left operand, while the surrounding conditional checks any remaining reachable result without reporting an already-checked operand again. Nested It would be worth testing the boundaries together: |
Check reachable Boolean operands once without widening expression types. Accept bool-bounded generic returns while retaining errors for invalid alternatives. Classification: B (an existing Pyright limitation newly exposed by and/or); type precision is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
AI-assisted follow-up on behalf of Bill Schnurr (@bschnurr) Pushed The original Python 3.13 QA reproduction has zero errors on base Local validation:
The dependencies used for these local checks satisfy the manifest ranges but are not frozen-lockfile installations. A synthetic Boolean-heavy timing had median reported analysis times of 0.774 s before and 0.797 s after over seven paired runs. This is not a representative performance or memory measurement; hosted benchmark and primer results for the new head are still needed. |
pyright allows you to declare that a type can't be used in a conditional context, e.g.
if obj:,while obj:.When it was originally added in 294a97d, pyright also checked the operand types of
and/oroperators.In #9225, it was pointed out that this leads to false positives -
None or objand1 or objare valid Python code and shouldn't be a type error.PR #9234 fixed the false positive by removing the check on
and/or.But that's not the best fix. It solved the false positive, but introduced a false negative. The Goldilocks fix is to check only the left operand, because the interpreter needs it to be convertible to bool, but not the right operand.
This PR reintroduces half the check that was removed in PR #9234, for the left operand only.
Updates #9225 (technically)