2Tests the handling of explicit protocol classes.
5# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#explicitly-declaring-implementation
7from abc import ABC, abstractmethod
8from typing import ClassVar, Protocol
11class PColor(Protocol):
13 def draw(self) -> str:
16 def complex_method(self) -> int:
20class NiceColor(PColor):
21 def draw(self) -> str:
25class BadColor(PColor):
26 def draw(self) -> str:
27 return super().draw() # E: no default implementation
Expected a ty diagnostic for this line
30class ImplicitColor: # Note no 'PColor' base here
31 def draw(self) -> str:
32 return "probably gray"
34 def complex_method(self) -> int:
38v1: PColor = NiceColor() # OK
39v2: PColor = ImplicitColor() # OK
43 rgb: tuple[int, int, int]
47 def intensity(self) -> int:
50 def transparency(self) -> int:
55 def __init__(self, red: int, green: int, blue: str) -> None:
56 self.rgb = red, green, blue # E: 'blue' must be 'int'
[invalid-assignment] Object of type `tuple[int, int, str]` is not assignable to attribute `rgb` of type `tuple[int, int, int]`
60p = Point(0, 0, "") # E: Cannot instantiate abstract class
Expected a ty diagnostic for this line
63class Proto1(Protocol):
65 cm2: ClassVar[int] = 0
75class Proto2(Protocol):
79class Proto3(Proto2, Protocol):
83class Concrete1(Proto1):
89c1 = Concrete1() # E: cannot instantiate abstract class
Expected a ty diagnostic for this line
92class Concrete2(Proto1):
100class Concrete3(Proto1, Proto3):
112class Concrete4(Proto1, Proto3):
125class Proto5(Protocol):
126 def method1(self) -> int:
130class Concrete5(Proto5):
134c5 = Concrete5() # E: cannot instantiate abstract class
Expected a ty diagnostic for this line
137class Proto6(Protocol):
145class Concrete6(Mixin, Proto6):
149class Proto7(Protocol):
155class Mixin7(Proto7, ABC):
156 def method1(self) -> None:
160class Concrete7A(Proto7):
164c7a = Concrete7A() # E: cannot instantiate abstract class
Expected a ty diagnostic for this line
167class Concrete7B(Mixin7, Proto7):