python / python/mypy

Typing with ClassVar[Optional[Callable]] doesn't work if the callable has arguments

Offen
#15,026 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Typing an attribute in a base class with ClassVar[Optional[Callable]] results in a type error even though the typing is seemingly sound.

To Reproduce

Playground: https://mypy-play.net/?mypy=master&python=3.11&gist=4f52f561801eeb053b6d0a47c9afbc15

class BaseClass:
    attr: ClassVar[Optional[Callable]] = None

class ChildClass2(BaseClass):
    @staticmethod
    def attr(s: str):
        pass

Expected Behavior

Mypy shouldn't return any errors.

Additionally, the same should happen when Callable is also typed itself, e.g. Callable[[str], None]. Right now both fail when they shouldn't.

Actual Behavior

test.py:10: error: Signature of "attr" incompatible with supertype "BaseClass"  [override]

Your Environment

  • Mypy version used: 1.1.1, 1.2
  • Mypy command-line flags: no relevant options
  • Mypy configuration options from mypy.ini (and other config files): no relevant options
  • Python version used: 3.11

Additional information

Interestingly, if the callable doesn't have any arguments, mypy doesn't fail:

class BaseClass:
    attr: ClassVar[Optional[Callable]] = None

class ChildClass2(BaseClass):
    @staticmethod
    def attr():
        pass

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 mit der verknüpften mypy-play-Reproduktion unter Verwendung von Python 3.11 und vergleiche die untypisierten und typisierten Callable-Fälle. Verfolge den Override-Prüfpfad für eine ClassVar Optional Callable und überprüfe, dass beide staticmethod-Beispiele keinen Fehler wegen inkompatibler Signatur erzeugen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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