python / python/mypy

Error when overriding a property created via decorators

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

Bug Report

When a parent class has a property, created via decorators with a getter and setter. If you attempt to override the fset of the parent by using decorators in the subclass, mypy throws an error.

To Reproduce

class A:
    def __init__(self, x: int):
        self._x = x

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, new_value: int):
        self._x = new_value


class B(A):
    def __init__(self):
        super().__init__(100)

    @A.x.setter
    def x(self, new_value: int):
        A.x.fset(self, new_value)

        print("Do something else ontop of the parent setter")

Expected Behavior

Mypy should produce no errors

Actual Behavior

Mypy produces the errors:

overloaded function has no attribute "setter" [attr-defined] on the decorator line in the subclass.
overloaded function has no attribute "fset" [attr-defined] on the call to fset in the subclass.

If instead of using a decorator, the property is created in the parent class via the property() function like below, then mypy does not produce any errors.

class A:
    def __init__(self, x: int):
        self._x = x

    def _get_x(self) -> int:
        return self._x

    def _set_x(self, new_value: int):
        self._x = new_value

    x = property(_get_x, _set_x)

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): Default configuration
  • Python version used: 3.10.5

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

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

はじめの一歩

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

調査の方向性

まず、提供された Python の例を mypy 1.0.0 とデフォルト設定で実行し、報告されたエラーを再現します。デコレートされたプロパティの型チェックのエントリーポイントを特定し、サブクラスでの setter のオーバーライドと親の fset へのアクセスを対象とするリグレッションテストを追加します。両方の例が attr-defined エラーなしで型チェックを通過すれば完了です。

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

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

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

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