python / python/mypy

Abstract class properties that are classes incorrectly reported in assignments

Offen
#8,993 4 Kommentare 5 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

  • 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 Type


class ISolution(ABC):
    @abstractmethod
    def __init__(self, b: int):
        pass

    @abstractmethod
    def func1(self, a: int) -> int:
        pass


class MySolution(ISolution):
    def __init__(self, b: int):
        self._b = b

    def func1(self, a: int) -> int:
        return a + self._b


class IHolder(ABC):
    @property
    @classmethod
    @abstractmethod
    def SolutionClass(cls) -> Type[ISolution]:
        pass

    # SolutionClass: Type[ISolution]

    @abstractmethod
    def __init__(self, c: int):
        pass

    @abstractmethod
    def cval(self) -> int:
        pass


class Main(IHolder):
    SolutionClass = MySolution

    def __init__(self, c: int):
        self._c = c

    def cval(self) -> int:
        return self._c


def check(holderclass: Type[IHolder], a: int, b: int, c: int) -> bool:
    holder = holderclass(c)
    solclass: Type[ISolution] = holderclass.SolutionClass
    return solclass(b).func1(a) == holder.cval()


print("should be true ", check(Main, 5, 5, 10))
print("should be false ", check(Main, 3, 4, 10))
  • What is the actual behavior/output?
classprop.py:53: error: Incompatible types in assignment (expression has type "Callable[[], Type[ISolution]]", variable has type "Type[ISolution]")

This is referring to the line:

    solclass: Type[ISolution] = holderclass.SolutionClass
  • What is the behavior/output you expect?
    No error to be reported.

Note. If you comment out

    @property
    @classmethod
    @abstractmethod
    def SolutionClass(cls) -> Type[ISolution]:
        pass

and then uncomment

    SolutionClass: Type[ISolution]

then mypy says the code is fine.

  • 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'm trying to avoid dealing 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, die Diagnose mit dem bereitgestellten Beispiel für abstrakte Klasse, property und classmethod zu reproduzieren. Vergleiche die Typinferenz für holderclass.SolutionClass mit der annotierten Version des Klassenattributs und überprüfe anschließend, dass die Zuweisung an solclass keinen inkompatiblen Typ mehr meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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