Signatures of "__ior__" and "__or__" are incompatible when assigning __or__ with compatible method
まだ誰も着手していません。
- 主要言語
- 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、Python 3.9 の例と mypy 0.931 の動作を使って、クラス A で or を定義する場合と、クラス B で _no から or を代入する場合の報告された違いを再現します。次に、ior と or のシグネチャを比較する型チェックの経路を追跡し、その後、報告に列挙されている他の算術 dunder メソッドでも結果を確認します。互換性のある代入メソッドでは誤った非互換エラーが発生しなくなり、実際に互換性のないシグネチャでは引き続き発生すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 42/100