python / python/mypy

Ior assignment operator is flagged while equivalent __ior__() method call is not

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

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

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

説明

Bug Report

A |= operation statement causes "Result type of | incompatible in assignment" while the equivalent method call is accepted without complaint. I haven't figured out to annotate the statement to prevent that complaint, and expect that the complaint is an error.

To Reproduce

I am composing an OrderedSet based on one of Ramond Hettinger's recipes (the weakref one), adding needed functionality and type annotations. As in the recipe, my OrderedSet class is based on collections.MutableSet. Adding an update method, this definition results in Result type of | incompatible in assignment on the "|=" statement:

def update(self, *others):
        # type: (*AbstractSet) -> None
        for o in others:
            self |= o   # Mypy Error: Result type of | incompatible in assignment

However, code for the method that is identical except for using the corresponding method call instead of the |= operator does not cause any complaints:

def update(self, *others):
        # type: (*AbstractSet) -> None
        for o in others:
            self.__ior__(o)   # No error.

I would prefer to use the |= statement, but have been unable to annotate the unaccepted statement in a way that prevents the complaint (short of using ignore). (There are other methods that suffer from similar obstacles, including the original __init__() included in Raymond's recipe. The example I'm using involves fewer incidental details.)

Expected Behavior

I believe the |= statement should be accepted without complaint.

Actual Behavior

Result type of | incompatible in assignment error on the "|=" statement.

Your Environment

  • Mypy version used: mypy 0.902
  • Python version used: Python 3.7.5
  • Operating system and version: Ubuntu Linux 18.04

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

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

はじめの一歩

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

調査の方向性

まず、提供された OrderedSet 更新スニペットに対して mypy を実行し、|= 文と同等の __ior__() 呼び出しを比較します。これら 2 つの形式がどのようにチェックされるかを追跡します。完了の条件は、|= 形式が ignore なしでメソッド呼び出しと一貫して受け入れられることです。

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

評価

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

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

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