python / python/mypy

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

オープン
#19,595 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

このIssueは、mypyのcomparison-overlap診断とPythonの__eq__の処理に関するものです。まずcomparison-overlapの実装とテストを見つけ、常に例外を発生させる__eq__をどのように認識できるかを調査してください。既存の診断を弱めることなく、示されているAとBの例が重ならない等価性チェックとして扱われれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。