python / python/mypy

Detect undefined attribute in child class (when defined in parent)

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

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

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

説明

mypy does not detect an undefined attribute in a child class if it is defined in a parent class.

Given the following code, where the __init__ of a child class forgets to call super().__init__:

class Base():
    def __init__(self, x: int) -> None:
        self.x = x
    def whatsx(self) -> int:
        return self.x

class Child(Base):
    def __init__(self, x: int) -> None:
        pass
        # Note: no call to super(), nor defining self.x.

c = Child(3)
print(c.whatsx())  # mypy does not catch undefined c.x

The real issue is obviously that the child class does not define the x attribute, failing Liskov.

Perhaps this is out-of-scope for mypy, but since programmers make stupid mistakes like this (/me raises hand), it would be awesome if mypy could warn about this error, making said programmers look less stupid ;)

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

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

はじめの一歩

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

調査の方向性

まず、提供された Base と Child の例に対して mypy を実行し、子クラスの init、継承された x 属性、欠落している super().init の呼び出しに焦点を当てます。mypy が継承された属性とコンストラクターのオーバーライドをどのように処理するかを追跡します。mypy が初期化の欠落を確実に警告し、正しいサブクラスを誤って拒否しなければ完了です。

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

評価

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

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

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