More problems with abstract class properties
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the modelmapper.py reproducer shown in the issue and confirm the reported errors for the abstract class property. Trace mypy's handling of the model_code access, then verify that the reproducer produces no errors while preserving the abstract declaration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100