python / python/mypy

More problems with abstract class properties

Aperta
#8,996 1 commento 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-descriptors
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il riproduttore modelmapper.py mostrato nell'issue e conferma gli errori segnalati per la proprietà della classe astratta. Traccia la gestione da parte di mypy dell'accesso a model_code, quindi verifica che il riproduttore non produca errori preservando la dichiarazione astratta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.