python / python/mypy

No error when subclass does not provide a value for ClassVar

Aperta
#14,381 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I've been trying to mypy to typecheck subclasses that need to provide a required value for a ClassVar in a base class that cannot provide the value itself (only has an unintialized attribute marker). The problem is that badly implemented subclasses can ignore this attribute without mypy raising errors about it (though interestingly if the value provided has incorrect type, mypy will raise errors about that).

To Reproduce

from abc import ABC
from typing import ClassVar, Type


class Base(ABC):
    my_super_important_value: ClassVar[str]


class GoodSubclass(Base):
    my_super_important_value = "1337"


# I want mypy to error about an unimplemented variable but it doesn't
class BadSubclass(Base):
    pass


def print_abc(cls: Type[Base]) -> None:
    print(cls.my_super_important_value)


print_abc(GoodSubclass)
# 1337
print_abc(BadSubclass)
# AttributeError: type object 'BadSubclass' has no attribute 'my_super_important_value'

mypy playground

Expected Behavior

Expected mypy to show an error about the BadSubclass class not providing a value for my_super_important_value.

Actual Behavior

No errors.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10

Note that the same code fails when using the Pyre type checker with this error:

14:0: Uninitialized attribute [13]: Attribute `my_super_important_value` inherited from abstract class `Base` in class `BadSubclass` to have type `str` but is never initialized. 

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 Python dell’issue con mypy --strict e verifica che BadSubclass non produca alcun errore, mentre l’accesso a runtime fallisce. Analizza la gestione da parte di mypy delle dichiarazioni ClassVar ereditate e degli attributi astratti o non inizializzati, usando la diagnostica di Pyre come confronto comportamentale. Il lavoro è completato quando mypy segnala che BadSubclass deve fornire my_super_important_value senza compromettere il caso GoodSubclass.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.