python / python/mypy

More problems with abstract class properties

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

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

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

説明

This is probably related to #8993

  • Are you reporting a bug, or opening a feature request?
    I believe this is a bug

  • Please insert below the code you are checking with mypy,

from abc import ABC, abstractmethod
from typing import List, Type, Dict


class BaseModel(ABC):
    @property
    @classmethod
    @abstractmethod
    def model_code(cls) -> str:
        pass

    # model_code: str


class M1(BaseModel):
    model_code = "m1"


class M2(BaseModel):
    model_code = "m2"


class ModelMapper:
    def __init__(self, model_list: List[Type[BaseModel]]):
        self.__registry: Dict[str, Type[BaseModel]] = {
            mc.model_code: mc for mc in model_list
        }

    def get_model_class(self, model_code: str) -> Type[BaseModel]:
        return self.__registry[model_code]

    def add_model_class(self, model_class: Type[BaseModel]):
        self.__registry[model_class.model_code] = model_class


mm = ModelMapper([M1, M2])
for m in ["m1", "m2"]:
    print("code: ", m, " class: ", mm.get_model_class(m))
  • What is the actual behavior/output?
    Reported errors:
modelmapper.py:26: error: Key expression in dictionary comprehension has incompatible type "Callable[[], str]"; expected type "str"  [misc]
modelmapper.py:33: error: Invalid index type "Callable[[], str]" for "Dict[str, Type[BaseModel]]"; expected type "str"  [index]
  • What is the behavior/output you expect?
    No errors.

If you comment out

    @property
    @classmethod
    @abstractmethod
    def model_code(cls) -> str:
        pass

and uncomment

    # model_code: str

then mypy doesn't complain

  • What are the versions of mypy and Python you are using?
    mypy 0.770
    python 3.7.6

  • Do you see the same issue after installing mypy from Git master?
    I didn't want to deal with that!

  • What are the mypy flags you are using? (For example --strict-optional)
    None.

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

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

はじめの一歩

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

調査の方向性

まず、issue に示されている modelmapper.py の再現コードを実行し、抽象クラスのプロパティについて報告されたエラーを確認します。model_code へのアクセスを mypy がどのように処理するかを追跡し、その後、抽象宣言を維持したまま再現コードでエラーが発生しないことを検証します。

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

評価

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

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

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