python / python/mypy

More problems with abstract class properties

Open
#8,996 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-descriptors
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.