python / python/mypy

More problems with abstract class properties

Offen
#8,996 1 Kommentar 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-descriptors
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den im Issue gezeigten Reproducer modelmapper.py auszuführen, und bestätige die gemeldeten Fehler für die Property der abstrakten Klasse. Verfolge mypy's Verarbeitung des Zugriffs auf model_code und überprüfe anschließend, dass der Reproducer ohne Fehler ausgeführt wird, während die abstrakte Deklaration erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.