-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
381 lines (275 loc) · 10.2 KB
/
tests.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from types import MethodType
from typing import Tuple, Any, Dict
import pytest # type: ignore
from polymorphism import Polymorphism, overload
def test_overload():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: str, y: str) -> type:
return str
simple = Simple()
assert simple.calc(1, 2) == int
assert simple.calc('1', '2') == str
def test_internal_dispatch():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
def calc(self, x: str, y: str, *, cast: type) -> type:
return self.calc(cast(x), cast(y))
simple = Simple()
assert simple.calc('1', '2', cast=float) == float
def test_dispatch_with_only_keyword():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: str, y: str, *, cast: type) -> Any:
raise Exception
def calc(self, x: str, y: str) -> type:
return str
simple = Simple()
assert simple.calc('1', '2') == str
def test_call_keyword_arguments():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
simple = Simple()
assert simple.calc(x=1, y=2) == int
assert simple.calc(y=2.0, x=1.0) == float
def test_signature_overlap():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> Tuple[type, type]:
return int, int
def calc(self, x: int, y: float) -> Tuple[type, type]:
return int, float
assert Simple().calc(1, 2) == (int, int)
def test_using_simple_descriptor():
# noinspection PyPep8Naming
class dummy:
def __init__(self, func):
self.func = func
def __get__(self, instance, class_):
if instance is None:
return self
return MethodType(self.func, instance)
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@dummy
def calc(self, x: int, y: int) -> type:
return int
@classmethod
def calc(cls, x: float, y: float) -> type:
return float
simple = Simple()
assert simple.calc(1, 2) == int
assert simple.calc(1.0, 2.0) == float
def test_complicated_descriptor_with_convention():
# noinspection PyPep8Naming
class dummy:
def __init__(self, func):
self.__wrapped__ = func
def __get__(self, instance, class_):
if instance is None:
return self
return MethodType(self, instance)
def __call__(self, *args, **kwargs):
return self.__wrapped__(*args, **kwargs)
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@dummy
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
simple = Simple()
assert simple.calc(1, 2) == int
assert simple.calc(1.0, 2.0) == float
def test_complicated_descriptor_without_convention():
# noinspection PyPep8Naming
class dummy:
def __init__(self, func):
self.func = func
def __get__(self, instance, class_):
if instance is None:
return self
return MethodType(self, instance)
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
with pytest.raises(TypeError, match='__wrapped__'):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@dummy
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
def test_data_descriptor_overload():
with pytest.raises(TypeError, match='shade'):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
@property
def calc(self) -> type:
return float
def test_var_positional_arguments():
with pytest.raises(TypeError, match='variable length'):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, *y: Tuple[int]) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
def test_var_keyword_arguments():
with pytest.raises(TypeError, match='variable length'):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, **y: Dict[str, float]) -> type:
return float
def test_method_without_arguments():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self) -> None:
return None
simple = Simple()
assert simple.calc() is None
def test_instance_method_on_class():
# noinspection PyPep8Naming
class dummy:
def __init__(self, func):
self.func = func
def __get__(self, instance, class_):
if instance is None:
return self
return MethodType(self.func, instance)
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@dummy
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
with pytest.raises(TypeError, match='is not callable'):
# noinspection PyCallByClass
Simple.calc(1, 2)
def test_class_method_on_instance():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@classmethod
def calc(cls, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
assert Simple().calc(1, 2) == int
def test_class_method_on_class():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> Tuple[type, type]:
return int, int
@staticmethod
def calc(x: int, y: float) -> Tuple[type, type]:
return int, float
@classmethod
def calc(cls, x: int, y: str) -> Tuple[type, type]:
return int, str
# noinspection PyCallByClass
assert Simple.calc(1, y='2') == (int, str)
def test_static_method():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
@staticmethod
def calc(x: int, y: int) -> type:
return int
@staticmethod
def calc(x: float, y: float) -> type:
return float
def calc(self, x: str, y: str) -> type:
return str
assert Simple.calc(1.0, 2.0) == float
def test_ambiguous_overload_default_argument():
with pytest.raises(
TypeError, match=r'Overloading .* \({0}, {0}\) types'.format(int)
):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: int, y: int, z: int = 3) -> type:
return int
def test_ambiguous_overload_class_method():
with pytest.raises(
TypeError, match=r'Overloading .* \({0}, {0}\) types'.format(int)
):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
@classmethod
def calc(cls, x: int, y: int) -> type:
return int
def test_ambiguous_overload_static_method():
with pytest.raises(
TypeError, match=r'Overloading .* \({0}, {0}\) types'.format(int)
):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
@staticmethod
def calc(x: int, y: int) -> type:
return int
def test_class_without_overload():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x, *args, y: int) -> type:
return int
simple = Simple()
# noinspection PyTypeChecker
assert simple.calc('1', y='2') == int
def test_argument_without_annotation():
with pytest.raises(
TypeError, match=r'Argument .* should be annotated'
):
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y) -> type:
return int
@staticmethod
def calc(x: int, y: int) -> type:
return int
def test_mismatched_call():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple(Polymorphism):
def calc(self, x: int, y: int) -> type:
return int
def calc(self, x: float, y: float) -> type:
return float
with pytest.raises(TypeError, match='not exists'):
Simple().calc(1, 2.0)
def test_overload_decorator():
# noinspection PyMethodMayBeStatic,PyRedeclaration
class Simple:
@overload
def calc(self, x: int, y: int) -> type:
return int
@calc.overload
def calc_str(self, x: str, y: str) -> type:
return str
# noinspection PyNestedDecorators
@calc.overload
@classmethod
def calc_float(cls, x: float, y: float) -> type:
return float
# noinspection PyCallByClass
assert Simple.calc(1.0, 2.0) == float