← Back to index
protocols_self.py
TP: 0
FP: 0
FN: 0
Optional: 0 / 0
2Tests the handling of annotated "self" parameters in a protocol.
5# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#self-types-in-protocols
7from typing import Generic, Protocol, Self, TypeVar
10C = TypeVar("C", bound="Copyable")
13class Copyable(Protocol):
14 def copy(self: C) -> C:
19 def copy(self) -> "One":
23T = TypeVar("T", bound="Other")
27 def copy(self: T) -> T:
36T1_co = TypeVar("T1_co", covariant=True)
37T2_co = TypeVar("T2_co", covariant=True)
40class P1Parent(Protocol[T2_co]):
41 def f0(self, /) -> Self:
45class P1Child(P1Parent[T2_co], Protocol[T2_co]):
49class C1(Generic[T1_co]):
50 def f0(self, /) -> Self:
54a1: P1Parent[str] = C1[str]()
55b1: P1Child[str] = C1[str]()
58class P2Parent(Protocol[T1_co]):
59 def f0(self, right: Self, /) -> "P2Parent[T1_co]":
63class P2Child(P2Parent[T1_co], Protocol[T1_co]):
67class C2(Generic[T2_co]):
68 def f0(self, other: Self) -> "C2[T2_co]":
72a2: P2Parent[str] = C2[str]() # OK
73b2: P2Child[str] = C2[str]() # OK