Assignability to a Protocol Using `Self` Must Respect Variance
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 38/100
- Issue-Typ
- Dokumentation
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- python
- Bereich
- documentation
Rechercherichtung
Beginne mit docs/spec/generics.rst bei den verlinkten Zeilen 2540-2544 und überprüfe die umgebende Diskussion über Self in Protokollen. Führe die bereitgestellten Beispiele mit mypy und pyright aus und aktualisiere anschließend den Absatz und das nahegelegene Beispiel, um kontravariante, kovariante und invariante Verwendungen von Self zu erläutern; abgeschlossen ist die Arbeit, wenn die Spezifikation dem demonstrierten Verhalten der Checker entspricht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
I just stumbled across this paragraph:
If I understand it correctly, then the following code is fine, i.e., Foo is assignable to Proto according to the explanation:
from __future__ import annotations
from typing import Protocol, Self
class Proto(Protocol):
def f(self, x: Self) -> None: ...
class Foo:
def f(self, x: Sub) -> None:
pass
class Sub(Foo):
pass
x: Proto = Foo()
However, type checkers like mypy and pyright reject this code. This aligns with my expectation: If I have an instance x: Proto, then I should be able to call x.f(x). But for y: Foo, I cannot call y.f(y).
The paragraph in question should distinguish between covariant, contravariant, and invariant occurences of Self:
from __future__ import annotations
from typing import Protocol, Self
class Proto(Protocol):
def f(self, x: Self) -> None: ... # contravariant
def g(self, x: Self) -> Self: ... # x: contravariant, return type: covariant
def h(self, x: list[Self]) -> None: ... # invariant
class Sup:
pass
class Foo(Sup):
def f(self, x: Sup) -> None: # x can be of type Foo or any superclass
pass
def g(self, x: Sup) -> Sub: # return type can be Foo or any subclass
raise RuntimeError()
def h(self, x: list[Foo]) -> None: # no sub-/superclass allowed as argument to list
pass
class Sub(Foo):
pass
x: Proto = Foo() # OK, also according to mypy and pyright
I assume that all the uses of Self are valid in this example, at least mypy and pyright do not complain and I couldn’t find any statement that would restrict Self in protocols to covariant positions or even return types only. Maybe it also makes sense to adjust the example after the paragraph in question. Currently, it only uses Self in a return type.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 4 Std. 16 Min.
- Gemergte PRs (30 T.)
- 7
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
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
python/typing#2356 · 2 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/typing#2227 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
python/typing#2150 · 2 Kommentare · 2 Reaktionen ·
Maintainer antworten meist innerhalb von 1 Tag
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
python/typing#2351 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
mozilla/bedrock#17413 · 1 Reaktion ·
Maintainer antworten meist innerhalb von 2 Tagen
-
instance instance add
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
searxng/searx-instances#943 · 1 Kommentar ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug tools
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
lance-format/lance#9655 ·
Maintainer antworten meist innerhalb von 2 Tagen