Assignability to a Protocol Using `Self` Must Respect Variance
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 38/100
- Tipo di issue
- Documentazione
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- documentation
Direzione di ricerca
Inizia da docs/spec/generics.rst alle righe collegate 2540-2544 e rivedi la discussione circostante su Self nei protocolli. Esegui gli esempi forniti con mypy e pyright, quindi aggiorna il paragrafo e l'esempio adiacente per spiegare gli usi controvarianti, covarianti e invarianti di Self; il lavoro è concluso quando la specifica corrisponde al comportamento dimostrato dai checker.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
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.
Altre issue di python/typing
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
topic: conformance tests topic: typing spec
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
Tutte le issue di python/typing
Issue simili
-
Add: hunch Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
DiamondLightSource/dodal#2211 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
openml/openml-python#1749 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
sipyourdrink-ltd/bernstein#6191 ·