Skip to content

--strict-equality too strict in iteratively visited code #19328

Closed

Description

@tyralla

The second error report is good, but, in line with the decision made in #6735, the first one is not:

# flags: --allow-redefinition-new --local-partial-types --strict-equality
x = 1
while True:
    if x == str():  # error: Non-overlapping equality check (left operand type: "A", right operand type: "C")
        break
    x = str()
    if x == int():  # error: Non-overlapping equality check (left operand type: "str", right operand type: "int")
        break

This could be easily fixed by letting the IterationErrorWatcher also handle COMPARISON_OVERLAP errors (which would already be an improvement over the current handling of functions with constrained type variables of just turning off --strict-equality temporarily). However, IterationErrorWatcher would need some adjustments to not accidentally filter out errors with slightly different texts, like in this case:

# flags: --allow-redefinition-new --local-partial-types --strict-equality

class A: ...
class B: ...
class C: ...

x = A()
while True:
    if x == C():  # error: Non-overlapping equality check (left operand type: "A", right operand type: "C")
                       # error: Non-overlapping equality check (left operand type: "A | B", right operand type: "C")
        break
    x = B()

Ideally, Mypy would only emit the last report (with A | B), which could eventually be implemented similarly to what is suggested in #19324 for reveal_type.

Activity

  1. self-assigned this
    on Jun 23, 2025
  2. gschaffner commented on Nov 24, 2025

    @gschaffner
    Contributor

    FYI: #20294 looks similar to this and is probably related, but I reported it separately since it seems to only occur in some specific conditions that aren't present in the reproducer above.

  3. added a commit that references this issue on Dec 15, 2025
    d06d3d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongtopic-overlapOverlapping equality check

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions