Skip to content

Check __bool__ type for the left operand of and/or - #11823

Open
Tomi Belan (TomiBelan) wants to merge 2 commits into
microsoft:mainfrom
TomiBelan:andor-a-star-wars-story
Open

Tomi Belan (TomiBelan) wants to merge 2 commits into
microsoft:mainfrom
TomiBelan:andor-a-star-wars-story

Conversation

@TomiBelan

Copy link
Copy Markdown

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 #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 #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)

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)
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Compared candidate 53b5b608dff492d3d8dcefdd090da1b1e318dff3 against its first parent 3e6e26db5b6a9e6ce363b62bfd4b7bd3cbd0587b.

View the full release history charts with the base and PR results.

Execution time history

Execution time history

Peak memory history

Peak memory history

Download the chart artifact.

Type checker benchmark

🟢 No performance regressions detected.

Regression threshold: 20.0%
Statistic: median
Variance guard: >1.0s time and >100.0 MB memory

Package Checker Files checked Time Time delta Peak memory Memory delta Status
ansible pyright 583 14.557s +0.9% 1109.2 MB -7.2% 🟢 Pass
click pyright 17 2.187s +0.2% 354.7 MB +0.1% 🟢 Pass
homeassistant pyright 9850 98.274s +0.1% 6150.3 MB +1.0% 🟢 Pass
numpy pyright 356 35.451s -1.3% 1316.8 MB +2.5% 🟢 Pass
pandas pyright 1459 130.110s -1.0% 3533.3 MB -0.4% 🟢 Pass
pytest pyright 243 12.274s +0.0% 859.2 MB +0.6% 🟢 Pass
requests pyright 19 1.797s +0.2% 343.9 MB -0.0% 🟢 Pass
torch pyright 2400 129.028s -0.2% 4933.3 MB -0.9% 🟢 Pass
transformers pyright 2901 114.828s +9.1% 5005.0 MB -4.7% 🟢 Pass

Pyright stats

Package Parsed/bound Checked Find Read Tokenize Parse Imports Bind Check Cycles
ansible 988 583 0.030s 0.067s 0.323s 0.530s 0.160s 0.707s 12.080s 0.000s
click 111 17 0.000s 0.013s 0.083s 0.153s 0.027s 0.197s 1.460s 0.000s
homeassistant 11146 9850 0.233s 0.563s 2.160s 3.403s 1.520s 5.060s 83.253s 0.000s
numpy 602 356 0.010s 0.063s 0.360s 0.747s 0.263s 0.900s 32.443s 0.000s
pandas 1894 1459 0.060s 0.200s 1.203s 2.513s 0.277s 2.610s 122.543s 0.000s
pytest 541 243 0.020s 0.060s 0.357s 0.530s 0.127s 0.717s 9.970s 0.000s
requests 167 19 0.000s 0.013s 0.087s 0.160s 0.037s 0.197s 1.060s 0.000s
torch 3078 2400 0.137s 0.377s 2.197s 3.613s 0.570s 4.620s 116.360s 0.000s
transformers 3534 2901 0.123s 0.410s 2.117s 3.660s 0.817s 8.500s 101.527s 0.000s

@heejaechang

Heejae Chang (heejaechang) commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

🔴 Pyright CLI QA — 5f27db8d — verdict: red

Automated CLI checks compared this change with its base revision.
14 focused check(s) completed.

Needs review: reportGeneralTypeIssues newly reported

The PR reports reportGeneralTypeIssues for this example; the base does not. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

reveal_type(0)
class Flag[T: bool]:
    def __init__(self, value: T): self.value = value
    def __bool__(self) -> T: return self.value

def choose[T: bool](x: Flag[T]) -> Flag[T]:
    return x or x

pyrightconfig.json

{"include": ["main.py"], "typeCheckingMode": "basic", "pythonVersion": "3.13"}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 3e6e26db5b6a9e6ce363b62bfd4b7bd3cbd0587b

{
  "exit_code": 0,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          0,
          12
        ],
        "end": [
          0,
          13
        ]
      }
    }
  ]
}

PR 5f27db8dfeb3b9ae8ba80f9da6fd4f3fec04e67a

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportGeneralTypeIssues",
      "message": "Invalid conditional operand of type \"Flag[T@choose]\"\n  Method __bool__ for type \"Flag[T@choose]\" returns type \"T@choose\" rather than \"bool\"",
      "range": {
        "start": [
          6,
          11
        ],
        "end": [
          6,
          12
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          0,
          12
        ],
        "end": [
          0,
          13
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

@TomiBelan

Copy link
Copy Markdown
Author

The PR reports reportGeneralTypeIssues for this example; the base does not. Is this change intended?

Yes. In your main.py example, BoolBox[B] is not considered a valid conditional type by pyright. You can verify this by appending the line if box: pass to main.py --> both base and PR will show this error: Invalid conditional operand of type "BoolBox[B@combine]". The goal of this PR is to extend the same checks that are already done on if statements also to the and, or operators, therefore, it is working as intended.

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 BoolBox[B] is not a valid conditional type - but I believe this to be out of scope for this PR.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
        pass

Base 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.

@TomiBelan

Tomi Belan (TomiBelan) commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

Rich Chiodo (@rchiodo) Thanks for taking a look!

Huh? Strange, I see only one diagnostic per line. Are we testing the same commit? Should I rebase it or something?

$ 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 node ./packages/pyright/index.js instead of pnpx pyright. I see the problem now.

@TomiBelan

Copy link
Copy Markdown
Author

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 and/or expression should return an Unknown type. For comparison, afaict the reason we don't get multiple diagnostics in a + b + c + d + e if they don't have __add__, is that the first a + b returns Unknown.

My dilemma is that the expression's return type is decided by Evaluator.getTypeResult, or rather, by getTypeOfBinaryOperation in operations.ts. But the _validateConditionalIsBool logic is implemented in Checker, not Evaluator. I don't see a way to change the expression type to Unknown in Checker.visitBinaryOperation (which might be a bad idea anyway). So I'm not sure how to fix this.

I suppose I could try to move (large parts of) _validateConditionalIsBool into operations.ts, but this sounds more complicated than I hoped. Not to mention that operations.ts doesn't quite sound like the right place either, since this logic should be shared between bool operators and bool statements.

Do you have any ideas?
(if not, that's ok, I'll try more stuff)

@bschnurr

Copy link
Copy Markdown
Member

I'd avoid returning Unknown: the expression still has useful type information. On 5f27db8d merged with main, this reports the intended bad-operand error but reveals Bad | Literal['fallback'] for result:

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 and/or expressions need the same treatment.

It would be worth testing the boundaries together: if False or b must still diagnose b; if True or b and if False and b must not diagnose the unreachable b; and value = None or b must not test b for truth at all. The same issue can occur with not, while, assert, ternaries, comprehensions, and match guards, not just if.

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>
@bschnurr

Copy link
Copy Markdown
Member

AI-assisted follow-up on behalf of Bill Schnurr (@bschnurr)

Pushed 4d374595e to address the duplicate diagnostics and the generic __bool__ case raised above. Truth-tested operands are checked once, including reachable ternary and assignment-expression results; the value type of b or "fallback" remains Bad | Literal['fallback']. A return type bounded or constrained to bool is accepted, while mixed int, unconstrained, and other non-bool returns remain diagnosed.

The original Python 3.13 QA reproduction has zero errors on base 3e6e26db, one reportGeneralTypeIssues error on return x or x at head 5f27db8d, and zero on this repair. The same reproduction passes on an isolated merge with main 7e29199f.

Local validation:

  • pnpm run build: passed.
  • pnpm exec jest 'typeEvaluator|checker' --maxWorkers=2 --forceExit (with local JSON output flags): 9 suites, 1,323 tests passed. After adding one further gradual-union test, pnpm exec jest typeEvaluator5.test -t Conditional --runInBand --forceExit passed all 3 selected tests.
  • Focused ESLint with ESLINT_USE_FLAT_CONFIG=false and Prettier checks passed.
  • An isolated merge with main built and passed 40 focused conditional, code-flow, operator, assert, and comprehension tests; the 3 conditional tests passed again after the test-only addition.

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.

This branch had an error being deployed

1 failed (outdated) deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants