Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More problems with abstract class properties #8996

Open
Dr-Irv opened this issue Jun 12, 2020 · 0 comments
Open

More problems with abstract class properties #8996

Dr-Irv opened this issue Jun 12, 2020 · 0 comments
Labels
feature topic-descriptors Properties, class vs. instance attributes

Comments

@Dr-Irv
Copy link

Dr-Irv commented Jun 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-descriptors Properties, class vs. instance attributes
Projects
None yet
Development

No branches or pull requests

2 participants