Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable comparison overlap checks in assert statements #17896

Open
JukkaL opened this issue Oct 8, 2024 · 4 comments · May be fixed by #17984
Open

Disable comparison overlap checks in assert statements #17896

JukkaL opened this issue Oct 8, 2024 · 4 comments · May be fixed by #17984
Labels
false-positive mypy gave an error on correct code feature needs discussion topic-overlap Overlapping equality check

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 8, 2024

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,
            ):
@JukkaL JukkaL added feature false-positive mypy gave an error on correct code topic-overlap Overlapping equality check labels Oct 8, 2024
@Kaven668
Copy link

Could I be assigned this?

@JelleZijlstra
Copy link
Member

Feel free to send a PR if you're interested in working on this!

@Avasam
Copy link
Contributor

Avasam commented Oct 11, 2024

Even in non-test code, a redundant assert can get optimized out anyway. And in non-optimized mode, it's almost certainly a quick sanity check. So I'm in favor of such a change for less false-positives.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 11, 2024

Wonder if there's anything we can do about the reachability implications, since mypy will currently just not check any subsequent code in the example given. And if you do happen to have --warn-unreachable on, you'll get a similar error (granted it doesn't get used that much since it's not currently part of --strict)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature needs discussion topic-overlap Overlapping equality check
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants