python / python/mypy

Disable comparison overlap checks in assert statements

Open
#17,896 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature needs discussion topic-overlap
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

In assert statements, allow comparisons that don't appear to overlap. These are common in test cases, and tend to generate false positives.

One way to implement this would be to filter out errors with the comparison-overlap error code in assert statements.

A potentially better way would be to not narrow down types in comparisons in assert statements, but this could be too complicated and ad hoc.

Example where we have a false positive:

# mypy: strict-equality
from enum import Enum

class MyEnum(Enum):
    X = 1
    Y = 2

class MyClass:
    attr: MyEnum = MyEnum.X

    def mutate(self) -> None:
        self.attr = MyEnum.Y

def test_foo() -> None:
    a = MyClass()
    assert a.attr == MyEnum.X
    a.mutate()
    assert a.attr == MyEnum.Y  # Error: Non-overlapping equality check

Pitch

These errors are often false positives, and they are somewhat frequent in test cases. The fix seems simple.

Hints

Here is an example of filtering errors adapted from mypy/plugins/default.py:

            with self.msg.filter_errors(
                filter_errors=lambda name, info: info.code != codes.TYPEDDICT_READONLY_MUTATED,
                save_filtered_errors=True,
            ):

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

Start by tracing how assert statements are checked and how the comparison-overlap error is produced; use the filtering example in mypy/plugins/default.py as a reference. Done means non-overlapping comparisons in assert statements no longer emit this error while ordinary comparisons retain their existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.