python / python/mypy

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

Open
#19,595 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

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.

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

The issue concerns mypy's comparison-overlap diagnostic and Python eq handling. Start by locating the implementation and tests for comparison-overlap, then investigate how an always-raising eq could be recognized. Done means the shown A and B example is handled as a non-overlapping equality check without weakening existing diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.