python / python/mypy

Concrete class considered abstract when base class has additional python type

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

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

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

説明

Bug Report

When using a TypeVar from an abstract class, and the abstract class additionally inherits from some integrated types (I found dict, int, str, set; not float), mypy incorrectly alarms about instantiating an abstract class.

To Reproduce

from abc import ABC, abstractmethod
from typing import TypeVar, Type


class User(ABC, dict):  # removing dict makes mypy happy
    @abstractmethod
    def method(self):
        ...


class BasicUser(User):
    def method(self):
        print('basic')


U = TypeVar('U', bound=User)


def new_user(user_class: Type[U]) -> U:
  user = user_class()
  return user


joe = new_user(BasicUser)

Expected Behavior

It should not report an error, as it does not when inheriting from another class or just ABC.

Actual Behavior

test.py:20: error: Cannot instantiate abstract class "User" with abstract attribute "method" [abstract]

Your Environment

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

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

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

はじめの一歩

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

調査の方向性

まず、提供された test.py の再現コードを mypy 0.990 で実行し、BasicUser に対する抽象クラスエラーを確認します。TypeVar の境界、抽象メソッド、追加の組み込み基底クラスの処理を追跡し、その後、誤った診断なしで new_user(BasicUser) が受け入れられることを示す回帰テストを追加します。

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

評価

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

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

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