Allow Self or Self-like type arguments for generics
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Beginne mit den Beispielen zu AbstractContextManager und der ausdrücklich genannten Einschränkung bei der Verwendung von Self als generischem Typargument. Vergleiche das gewünschte Verhalten für ManagerB und SubManager mit den aktuellen Regeln für Self, Vererbung und generische Typargumente. Als abgeschlossen gilt die Aufgabe, wenn eine allgemeine Lösung die konkrete Unterklasse korrekt ableitet und zugleich das korrekte Verhalten für überschriebene oder geerbte enter-Methoden beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
from contextlib import AbstractContextManager
class ManagerA(AbstractContextManager):
def __init__(self, x: int) -> None:
self._x = x
def __enter__(self) -> int:
return self._x
This can raise type check errors because AbstractContextManager is a generic type, and this code omits the type argument. Fair enough, and easy to fix:
from contextlib import AbstractContextManager
class ManagerA(AbstractContextManager[int]):
def __init__(self, x: int) -> None:
self._x = x
def __enter__(self) -> int:
return self._x
But what if we are not overriding the __enter__ method? Consider this example:
from contextlib import AbstractContextManager
class ManagerB(AbstractContextManager):
def __init__(self, x: int) -> None:
self._x = x
# Other methods are defined, but __enter__ is not overridden
In this case, there is no great way to accurate type annotate it in order to reflect the fact that the __enter__ method always returns the object that it was called on (which is the default implementation of the __enter__ method in AbstractContextManager.
If we were willing to mark this class as final, then we could annotate it as:
class ManagerB(AbstractContextManager["ManagerB"]):
And that would work. But if we want to be able to subclass it, then this would no longer be accurate. Consider:
from contextlib import AbstractContextManager
from types import TracebackType
from typing import Type
class ManagerB(AbstractContextManager["ManagerB"]):
def __init__(self, x: int) -> None:
self._x = x
# Other methods are defined, but __enter__ is not overridden
def __exit__(
self, exc_type: Type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None) -> bool | None:
return None
class SubManager(ManagerB):
def harf(self) -> None:
print("harf")
with SubManager(1) as subman:
subman.harf()
This runs fine, but the type checker will rightly complain. The annotations indicate that subman should be of type ManagerB, but ManagerB has no harf attribute.
It feels like it would be appropriate to use:
class ManagerB(AbstractContextManager[Self]):
But Self is not allowed to be used in this context.
The feature I would like to see is some way to accurately handle non-final subclasses of AbstractContextManager that do not overwrite the __enter__ method (or that overwrite it but still return self).
Obviously this issue applies generally to generics -- this is just an obvious example since the Python implementation of that class makes it impossible to type annotate many of its subclasses. But ideally any solution would apply generally to generics -- essentially giving a way to use Self (or something Self-like) as the type argument for a generic.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typing
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
stephrobert/dsoxlab#238 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
sublimehq/package_control#1780 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
nwg-piotr/nwg-displays#145 ·