python / python/mypy

redundant-expr does not catch always-True `if`

Open
#19,728 0 comments 0 reactions 0 assignees View on GitHub

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.