Class with only `__init_subclass__` raises `attr-defined`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Hello! I dug around the issue tracker to try and find any references to this kind of issue, but didn't find anything relevant. Apologies if this is a duplicate, but it seemed like it was unique enough for a report.
Bug Report
When adding a type hint to a mixin class's function, an error is raised. Previously, the function was untyped so was not being evaluated (i.e. not in --strict mode).
To Reproduce
I boiled it down to a simpler example. The contents of main.py:
class HasEvents:
def __init_subclass__(cls, /, **kwargs):
super().__init_subclass__(**kwargs)
cls.foo = [] # this type doesn't appear to matter
return cls
def record_event(self, *, some_other_arg: str):
event = self.foo.append(some_other_arg) # <== error here
# do something else with `event`...
return event
https://mypy-play.net/?mypy=1.2.0&python=3.11&gist=90a1e568fd9630201f21c28f94167138
https://gist.github.com/mypy-play/90a1e568fd9630201f21c28f94167138
Actual Behavior
main.py:8: error: "HasEvents" has no attribute "Event" [attr-defined]
I found that changing __init_subclass__ to __init__ passes.
Your Environment
I tried a variety of Python and mypy versions from the playground to see if it was new, and went back to Python 3.7 and mypy 0.750 - so versions, flags and command line options have been isolated from the equation.
Pull request part of the greater project, where I disabled the check: pypi/warehouse@bc76ecf (#13593)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione minima in main.py e con il comportamento di init_subclass quando viene verificato self.foo in record_event. Traccia il percorso rilevante del controllo dei tipi e confrontalo con la variante funzionante con init. Il lavoro è completato quando il caso riprodotto non segnala più un errore attr-defined e i controlli esistenti rimangono corretti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100