python / python/mypy

Signatures of "__ior__" and "__or__" are incompatible when assigning __or__ with compatible method

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

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

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

説明

Bug Report

Dear mypy team,

I was implementing arithmetic operations on a custom class and ran into the following issue:
When I don't define __or__ in the class body, but assign it with a function with a compatible type, mypy complains about incompatible signatures of __ior__ and __or__. (This is also true for other arithmetic dunders, see example).

I wanted to only allow inplace operations on that class and throw a common error on non-inplace operations.

To Reproduce

# this works and does not throw an error
class A:

    def __or__(self, v: int) -> A:...

    def __ior__(self, v: int) -> None: ...

    reveal_type(__or__)  # note: Revealed type is "def (_error.A, builtins.int) -> _error.A"
    reveal_type(__ior__)  # note: Revealed type is "def (_error.A, builtins.int)"


# this errors
class B:

    def _no(self, v: int) -> B: ...

    __or__ = _no

    def __ior__(self, v: int) -> None: ...  # error: Signatures of "__ior__" and "__or__" are incompatible

    reveal_type(__or__)  # note: Revealed type is "def (self: _error.B, v: builtins.int) -> _error.B"
    reveal_type(__ior__)  # note: Revealed type is "def (_error.B, builtins.int)"

NOTE: the same is true for __and__, __xor__, __add__, __sub__, __mul__, __truediv__, __floordiv__. (I haven't tested others.

Your Environment

  • Mypy version used: 0.931
  • Python version used: python3.9

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

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

はじめの一歩

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

調査の方向性

まず、Python 3.9 の例と mypy 0.931 の動作を使って、クラス A で or を定義する場合と、クラス B で _no から or を代入する場合の報告された違いを再現します。次に、ioror のシグネチャを比較する型チェックの経路を追跡し、その後、報告に列挙されている他の算術 dunder メソッドでも結果を確認します。互換性のある代入メソッドでは誤った非互換エラーが発生しなくなり、実際に互換性のないシグネチャでは引き続き発生すれば完了です。

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

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
42/100

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

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