Allow a base class to restrict the type of subclass's inner class
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
Feature
For regular classvars, attributes and methods, mypy already checks that the types in a subclass match those declared in the superclass. However, this checking does not extend to inner classes in the subclass. It seems inner classes are ignored by mypy entirely.
For example, a framework could define an abstract class Model whose inner class Meta must implement a certain protocol:
# Defined in the framework
class InnerMetaProtocol(Protocol):
example_attr: str
class Model:
Meta: type[InnerMetaProtocol]
# An application developer using framework
class Widget(Model):
class Meta:
example_attr = 123 # This should be an error: int is not str
Class Widget implements Model, whose attribute Meta should conform to type[InnerMetaProtocol], meaning it requires attribute example_attr: str. But currently mypy ignores this constraint: Playground.
It's probably useful to support this with not just protocols, but also concrete classes (e.g. Meta: type[ConcreteClass]).
Pitch
This pattern of inner Meta classes is used quite extensively in the Django ecosystem, including for Django models, forms, Django REST Framework serializers, djangorestframework-dataclasses, etc. I'm sure there are other use cases as well, but I don't know off the top of my head.
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 l'esempio Playground della issue e confronta il tipo dichiarato di Model.Meta con la classe interna Meta della sottoclasse. Il lavoro è completato quando mypy segnala l'esempio incompatibile con int e supporta i casi indicati del protocollo e della classe concreta.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100