File size: 2,624 Bytes
d8a7ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Tests the handling of explicit protocol classes.
"""

# Specification: https://typing.readthedocs.io/en/latest/spec/protocol.html#explicitly-declaring-implementation

from abc import ABC, abstractmethod
from typing import ClassVar, Protocol


class PColor(Protocol):
    @abstractmethod
    def draw(self) -> str:
        ...

    def complex_method(self) -> int:
        return 1


class NiceColor(PColor):
    def draw(self) -> str:
        return "deep blue"


class BadColor(PColor):
    def draw(self) -> str:
        return super().draw()  # E: no default implementation


class ImplicitColor:  # Note no 'PColor' base here
    def draw(self) -> str:
        return "probably gray"

    def complex_method(self) -> int:
        return 0


v1: PColor = NiceColor()  # OK
v2: PColor = ImplicitColor()  # OK


class RGB(Protocol):
    rgb: tuple[int, int, int]
    other: int

    @abstractmethod
    def intensity(self) -> int:
        return 1

    def transparency(self) -> int:
        ...


class Point(RGB):
    def __init__(self, red: int, green: int, blue: str) -> None:
        self.rgb = red, green, blue  # E: 'blue' must be 'int'
        self.other = 0


p = Point(0, 0, "")  # E: Cannot instantiate abstract class


class Proto1(Protocol):
    cm1: ClassVar[int]
    cm2: ClassVar[int] = 0

    im1: int
    im2: int = 2
    im3: int

    def __init__(self):
        self.im3 = 3


class Proto2(Protocol):
    cm10: int


class Proto3(Proto2, Protocol):
    cm11: int


class Concrete1(Proto1):
    def __init__(self):
        self.im1 = 1
        self.im3 = 3


c1 = Concrete1()  # E: cannot instantiate abstract class


class Concrete2(Proto1):
    cm1 = 3
    im1 = 0


c2 = Concrete2()


class Concrete3(Proto1, Proto3):
    cm1 = 3

    def __init__(self):
        self.im1 = 0
        self.cm10 = 10
        self.cm11 = 11


c3 = Concrete3()


class Concrete4(Proto1, Proto3):
    cm1 = 3
    cm10 = 3

    def __init__(self):
        self.im1 = 3
        self.im10 = 10
        self.cm11 = 3


c4 = Concrete4()


class Proto5(Protocol):
    def method1(self) -> int:
        ...


class Concrete5(Proto5):
    pass


c5 = Concrete5()  # E: cannot instantiate abstract class


class Proto6(Protocol):
    x: int


class Mixin:
    x = 3


class Concrete6(Mixin, Proto6):
    pass


class Proto7(Protocol):
    @abstractmethod
    def method1(self):
        ...


class Mixin7(Proto7, ABC):
    def method1(self) -> None:
        pass


class Concrete7A(Proto7):
    pass


c7a = Concrete7A()  # E: cannot instantiate abstract class


class Concrete7B(Mixin7, Proto7):
    pass


c7b = Concrete7B()