python / python/mypy

Mypy should warn about conditions whose value can be deduced

Open
#9,524 0 comments 0 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

Mypy can warn about left and right operands of conditions its value mypy can deduce. Unfortunately, it cannot do so for conditions consisting of one operand.

Pitch

val = 1337

if val is not None:
    pass

if val is not None and True:
    pass

Mypy can hint about the pointless left hand operand in the second if. Unfortunately, it does not do so for the first if. Supporting this use case would help the programmer to simplify her code by suggesting conditions to remove.

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

Begin with mypy's existing diagnostic for the pointless left-hand operand in the and True example, then locate the implementation and tests that produce it. Add coverage for the single-operand if val is not None case and verify mypy reports the removable condition without regressing the existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.