python / python/mypy

Extend `redundant-expr` to type narrowing assertions

Open
#13,766 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature

Assert statements can be used to guide the type checker but are not checked for redundancy like casts.

Pitch

Consider this simple example, with the --enable-error-code=redundant-expr flag:

def f(x: int) -> None:
    assert x is not None           # no issue
    assert x is not None and True  # error: Left operand of "and" is always true

I think it would be useful to have mypy emit a warning for the first line.

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

No file or test is named. Start by locating the existing redundant-expr handling for casts and the assert type-narrowing path, then inspect the relevant tests. Done means redundant assertions such as assert x is not None are reported under --enable-error-code=redundant-expr without changing valid narrowing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
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.