redundant-expr does not catch always-True `if`
Open
Nobody has claimed this yet.
bug
topic-reachability
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
If the expression for an if statement is always true, then it is obviously redundant.
To Reproduce
from typing import Literal
class X:
def __bool__(self) -> Literal[True]:
return True
if X():
print("blah!")
Expected Behavior
Error on if X()
Actual Behavior
No error
Your Environment
- Mypy version used: 1.17.1
- Mypy command-line flags:
--enable-error-code=redundant-expr - Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.14
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides a Python reproducer and the --enable-error-code=redundant-expr flag; run it with mypy 1.17.1 first. Trace the redundant-expression diagnostic and add a regression test covering Literal[True] returned by bool; done when the example reports an error without changing unrelated cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100