microsoft / microsoft/pyright

False positive "Condition will always evaluate to False"

Open
#11,086 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Describe the bug**

A `==` comparison between a `bytearray` instance and a `bytes` instance, when checked with pyright at strict level, gets an error: "Condition will always evaluate to False since the types "bytearray" and "bytes" have no overlap".

This is incorrect, python overloads `==` for this case.

(It might actually be considered useful to be able to report this as a type error. But even in that case, the current pyright error message is not correct.)

**Code or Screenshots**

Example. The assert passes, even though pyright claims "always ... False'.

```python
def foo(x: bytes, y: bytes) -> bool:
return bytearray(x) == y

assert foo(b'1234', b'1234')
```

See https://pyright-play.net/?pyrightVersion=1.1.407&pythonVersion=3.14&strict=true&code=CYUwZgBGD20BQA8BcEBGBPALiAzgGgnRQ2xwEoIBaAPjVgBskAoCViAJxEwFd2A7NFhABDdu2HpEFALzTCTJsJw4Q7TFFhxUAcgCMAJgDMAFm0EdBk9rJMgA

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal example in the linked pyright-play reproduction and confirm the strict-mode diagnostic for the bytearray-to-bytes comparison. No repository file or test is named, so trace the diagnostic from this reproduction into the relevant type-checking code. Done means the comparison no longer produces a false-positive message, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.