python / python/mypy

Inherited, overridden attribute type reverted to the top parent class type

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

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

bug false-positive priority-1-normal topic-inheritance
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

The issue type is probably quite bad, but example should quickly clear things up:

class A:
    attr = None


class B(A):
    attr: str  # type: ignore


class C1(B):
    attr = "test"


class C2(B):
    pass


class D(C2):
    attr = "test"

mypy test.py outputs:

test.py:18: error: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "None")

where line 18 is the last line of above example

As to why I do attr: str # type: ignore - in a nutshell, in my case A class comes from external source and they botched type hints there so I try to fix them in B. Not sure if it should have worked for C1, but if it did I see it breaking in D as a bug due mypy not being consistent, but then again maybe there is a good reason for it?.

Tested with mypy 0.720

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

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

はじめの一歩

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

調査の方向性

まず、提供された継承の例を mypy 0.720 で実行し、C1 と D の診断を比較します。継承された属性とオーバーライドされた属性の型解決を追跡し、そのうえで、得られた動作が issue に記載された意図する意味論と一貫していることを確認します。

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

評価

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

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

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