Skip to content

Consider an __eq__ that always raises as non-overlapping for equality purposes? #19595

@akx

Description

@akx

Feature

When a class defines __eq__, Mypy understandably bails out of ever considering that class as statically non-overlapping with another.

IOW,

class A:
   pass

class B:
   pass

A() == B()

goes error: Non-overlapping equality check (left operand type: "A", right operand type: "B") [comparison-overlap], as you'd expect, but

class A:
   def __eq__(self, other: object) -> bool:
       raise RuntimeError("Please do not directly compare A objects; use special method önnönnöö()")

   def önnönnöö(self, other: A | B) -> bool:
       return False  # some specific Explicit Is Better Than Implicit logic here

class B:
   pass

A() == B()

has no errors mypy could see.

It would be useful if mypy could recognize that an __eq__ function that always raises (or, I suppose, more broadly, can never return a value) makes a class never overlap with any other class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions