python / python/mypy

(🎁) `redundant-expression` doesn't report in `else` branch when both sides are narrowed

Open
#12,082 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

def foo() -> bool:
    return None == None

1 == 1
True == False

These codes show no warning about an always true check.

mypy in real life 😳

class Thing:
    thing: str | None
    def something(self, that: Thing) -> bool:
        if self.thing is not None or that.thing is not None:
            return True
        else:
            return self.thing == that.thing  # no error, despite None == None

Would be nice if mypy could report on this code.

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

Use the supplied foo and Thing examples as minimal reproductions, then run mypy to confirm the missing diagnostic in the else branch. Trace the narrowing and redundant-expression checks involved; done means the shown always-true comparisons are reported without regressing valid narrowing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.