Declaring classes as pure mixins
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
(This is an extension to #241 and copied from the discussion there.)
What about having a magic class decorator (with no or minimal runtime effect) or a base class that declares a class as a "pure mixin"? A pure mixin could only be used for implementation inheritance, and it wouldn't be type checked by itself at all -- it would only be type checked as part of implementation inheritance. I think that this would allow type checkers to check some mixin use cases that are tricky right now with minimal code changes. The alternative would be to use ABCs to declare the things that the mixin expects to be defined elsewhere, but they are harder to use and arguably add boilerplate.
Example:
@puremixin
class MyMixin:
def do_stuff(self) -> int:
self.one_thing() # ok, even though not defined in this class
return self.second_thing(1) # also ok
class Thing(Implementation[MyMixin]):
def one_thing(self) -> None: pass # ok
def second_thing(self, x: int) -> str: # error: return type not compatible with do_stuff
return 'x'
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Dies ist ein Designvorschlag, der aus der Diskussion in #241 übernommen wurde und keine Dateien oder Tests nennt. Lies zunächst #241 und die zugehörige Diskussion, kläre anschließend die Semantik eines Pure-Mixin-Dekorators oder einer Pure-Mixin-Basisklasse und wie der gezeigte Fall Implementation[MyMixin] geprüft werden sollte; als abgeschlossen gilt die Aufgabe, wenn ein abgestimmtes Design vorliegt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100