-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.py
More file actions
983 lines (759 loc) · 24.3 KB
/
tests.py
File metadata and controls
983 lines (759 loc) · 24.3 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# Copyright 2017-2020 Kamil Sindi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import pytest
from implements import Interface, implements, get_mro
py36 = pytest.mark.skipif(sys.version_info < (3, 6), reason='requires py3.6')
def test_empty():
class FooInterface(Interface):
pass
@implements(FooInterface)
class FooImplementation:
pass
def test_with_args_kwargs():
class FooInterface(Interface):
def foo(self, a, *args, b=1, **kwargs):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, a, *args, b=7):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, a, *args, b=1, **kwargs):
pass
def test_with_kwarg_only():
class FooInterface(Interface):
def foo(self, a, *, b):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, a, b):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, a, *, b):
pass
def test_property():
class FooInterface(Interface):
@property
def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
@property
def foo(self):
pass
def test_property_inverse():
class FooInterface(Interface):
def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def foo(self):
pass
def test_setters():
class FooInterface(Interface):
@property
def foo(self):
pass
@foo.setter
def foo(self, val):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
@property
def foo(self):
pass
@foo.setter
def foo(self, val):
pass
def test_deleters():
class FooInterface(Interface):
@property
def foo(self):
pass
@foo.deleter
def foo(self, val):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
@property
def foo(self):
pass
@foo.deleter
def foo(self, val):
pass
def test_implementation_implements_more_descriptors():
class FooInterface(Interface):
@property
def foo(self):
pass
# An implementation must implement all data descriptors defined in
# the interface, however, the implementation could define more.
#
# The case below must not generate errors because FooImplementationPass
# defines a foo.setter which isn't defined by FooInterface
@implements(FooInterface)
class FooImplementationPass:
@property
def foo(self):
pass
@foo.setter
def foo(self, val):
pass
def test_missing_method():
class FooInterface(Interface):
def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self):
pass
def test_missing_argument():
class FooInterface(Interface):
def foo(self, arg):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, arg):
pass
def test_renamed_argument():
class FooInterface(Interface):
def foo(self, arg):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, arrrrg):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, arg):
pass
def test_extra_argument():
class FooInterface(Interface):
def foo(self, arg):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, arg, ument):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, arg):
pass
def test_different_defaults():
class FooInterface(Interface):
def foo(self, arg=7):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, arg=8):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, arg=7):
pass
def test_different_order():
class FooInterface(Interface):
def foo(self, a, b):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, b, a):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, a, b):
pass
def test_missing_kwargs():
class FooInterface(Interface):
def foo(self, **kwargs):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, **kwargs):
pass
def test_missing_property():
class FooInterface(Interface):
@property
def foo(self):
pass
with pytest.raises(NotImplementedError): # missing method
@implements(FooInterface)
class FooImplementationFail1: # skipcq: PYL-W0612
pass
with pytest.raises(NotImplementedError): # missing property decorator
@implements(FooInterface)
class FooImplementationFail2: # skipcq: PYL-W0612
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
@property
def foo(self):
pass
def test_bad_constructor():
class FooInterface(Interface):
def __init__(self, a):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def __init__(self):
pass
@implements(FooInterface)
class FooImplementationPass:
def __init__(self, a):
pass
def test_multiple_errors():
class FooInterface(Interface):
@property
def foo(self):
pass
def __init__(self, a):
pass
# Bad constructor, missing method getter, and missing class attribute (3)
match = r'^Found 3 errors in implementation:\n- .+\n- .+\n- .+\nwith .+'
with pytest.raises(NotImplementedError, match=match):
@implements(FooInterface)
class FooImplementationFail: # skipcq: PYL-W0612
def __init__(self):
pass
def test_static():
class FooInterface(Interface):
@staticmethod
def foo(a, b, c):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail1: # skipcq: PYL-W0612
pass # missing foo
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail2: # skipcq: PYL-W0612
# skipcq: PYL-E0213
def foo(a, b, c): # missing staticmethod decorator
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail3: # skipcq: PYL-W0612
@classmethod # classmethod instead of staticmethod
def foo(cls, a, b, c): # decorator-check fails before signature
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail4: # skipcq: PYL-W0612
@staticmethod
def foo(m, n, o): # staticmethod, but wrong signature
pass
@implements(FooInterface)
class FooImplementationPass:
@staticmethod
def foo(a, b, c):
pass
def test_classmethods():
class FooInterface(Interface):
@classmethod
def foo(cls, a, b, c):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail1: # skipcq: PYL-W0612
pass # missing foo
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail2: # skipcq: PYL-W0612
# skipcq: PYL-E0213
def foo(cls, a, b, c): # missing classmethod decorator
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail3: # skipcq: PYL-W0612
@staticmethod # staticmethod instead of classmethod
def foo(a, b, c): # decorator-check fails before signature
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail4: # skipcq: PYL-W0612
@classmethod
def foo(cls, m, n, o): # classmethod, but wrong signature
pass
@implements(FooInterface)
class FooImplementationPass:
@classmethod
def foo(cls, a, b, c):
pass
def test_classmethod_signature_match():
# For a classmethod, inspect.signature returns a signature with the first
# element (cls) stripped. A classmethod with signature (cls, a, b, c) has
# signature equivalence with a regular method with signature (a, b, c)
#
# Example:
from inspect import signature
class TestA:
@classmethod
def foo(cls, a, b, c):
pass
class TestB:
# skipcq: PYL-E0213
def foo(a, b, c):
pass
assert signature(TestA.foo) == signature(TestB.foo)
# The test below ensures that the above case is flagged
class FooInterface(Interface):
@classmethod
def foo(cls, a, b, c):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
# skipcq: PYL-E0213
def foo(a, b, c):
pass
def test_staticmethod_classmethod_with_decorator():
class FooBarInterface(Interface):
@staticmethod
def foo(a, b, c):
pass
@classmethod
def bar(cls, a, b, c):
pass
import functools
def decorator(func):
@functools.wraps(func)
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner
@implements(FooBarInterface)
class FooBarImplementationPass:
@staticmethod
@decorator
def foo(a, b, c):
pass
@classmethod
@decorator
def bar(cls, a, b, c):
pass
def test_kwargs_only():
class FooInterface(Interface):
def foo(self, *, a):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementation:
def foo(self, a):
pass
def test_multiple_interfaces():
class FooInterface(Interface):
def foo(self):
pass
class BarInterface(Interface):
def bar(self):
pass
with pytest.raises(NotImplementedError):
@implements(BarInterface)
@implements(FooInterface)
class FooImplementationNoBar:
def foo(self, a):
pass
with pytest.raises(NotImplementedError):
@implements(BarInterface)
@implements(FooInterface)
class FooImplementationNoFoo:
def bar(self, a):
pass
@implements(BarInterface)
@implements(FooInterface)
class FooImplementation:
def foo(self):
pass
def bar(self):
pass
def test_interface_name_collision():
class Foo1Interface(Interface):
def foo(self):
pass
class Foo2Interface(Interface):
def foo(self):
pass
@implements(Foo2Interface)
@implements(Foo1Interface)
class FooImplementation:
def foo(self):
pass
def test_interface_name_and_signature_collision():
class Foo1Interface(Interface):
def foo(self):
pass
class Foo2Interface(Interface):
def foo(self) -> str:
return 'foo'
# Two interfaces with different signatures for a given method will
# always result in failure for the implementing class, as the
# implemented method's signature can only satisfy one of the interfaces.
with pytest.raises(NotImplementedError):
@implements(Foo2Interface)
@implements(Foo1Interface)
class FooImplementationFail:
def foo(self):
pass
def test_interface_inheritance():
class BaseInterface(Interface):
def bar(self):
pass
class FooInterface(BaseInterface):
def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self):
pass
def bar(self):
pass
def test_class_inheritance():
class FooInterface(Interface):
def foo(self):
pass
@implements(FooInterface)
class ParentImplementation:
def foo(self):
pass
@implements(FooInterface)
class ChildImplementation(ParentImplementation):
pass
def test_class_multiple_inheritance():
# --------- INTERFACES -----------------------------------------------
#
class FooInterface(Interface):
def foo(self, final):
pass
class BarInterface(Interface):
def bar(self, final):
pass
class FooBarInterface(FooInterface, BarInterface):
pass
# --------- IMPLEMENTATION -------------------------------------------
#
class BaseFooImplementation: # must get overridden
def foo(self, override, my, args):
pass
@implements(FooInterface)
class FooImplementation(BaseFooImplementation):
def foo(self, final): # skipcq: PYL-W0221
pass
@implements(BarInterface)
class BarImplementation:
def bar(self, final):
pass
with pytest.raises(NotImplementedError):
@implements(FooBarInterface)
class SubFooImplementation(FooImplementation): # foo, no bar
pass
@implements(FooInterface)
@implements(BarInterface)
@implements(FooBarInterface)
class FooBarImplementation(FooImplementation, BarImplementation):
pass
def test_rtn_type_annotation():
class FooInterface(Interface):
def foo(self) -> str:
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self) -> int:
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self) -> str:
pass
def test_arg_type_annotation():
class FooInterface(Interface):
def foo(self, arg: str):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
def foo(self, arg: int):
pass
@implements(FooInterface)
class FooImplementationPass:
def foo(self, arg: str):
pass
def test_other_decorator_compat():
def decorator(cls):
class Wrapper:
def __init__(self, *args):
self.wrapped = cls(*args)
def __getattr__(self, name):
print('Getting the {} of {}'.format(name, self.wrapped))
return getattr(self.wrapped, name, None)
return Wrapper
class FooInterface(Interface):
def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
@decorator
class FooImplementationFail:
def __init__(self, x, y):
self.x = x
self.y = y
def foo(self):
pass
@decorator
@implements(FooInterface)
class FooImplementationPass:
def __init__(self, x, y):
self.x = x
self.y = y
def foo(self):
pass
def test_magic_methods():
class FooInterface(Interface):
def __add__(self, other):
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
pass
@implements(FooInterface)
class FooImplementationPass:
def __add__(self, other):
pass
def test_attributes():
class FooInterface(Interface):
a = None
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
pass
@implements(FooInterface)
class FooImplementationPass:
a = 1
b = 2
def test_async():
class AsyncInterface:
async def __aenter__(self):
return self
async def __aexit__(self, *args, **kwargs):
pass
with pytest.raises(NotImplementedError):
@implements(AsyncInterface)
class AsyncImplementation:
pass
def test_async_method():
class AsyncFooInterface:
async def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(AsyncFooInterface)
class FooImplementationFail: # skipcq: PYL-W0612
def foo(self):
pass
@implements(AsyncFooInterface)
class AsyncFooImplementation: # skipcq: PYL-W0612
async def foo(self):
pass
def test_generator():
class GenFooInterface:
def foo(self): # skipcq: PYL-R0201
yield 1
with pytest.raises(NotImplementedError):
@implements(GenFooInterface)
class FooImplementationFail: # skipcq: PYL-W0612
def foo(self):
pass
# must fail a generator which happens to be async
with pytest.raises(NotImplementedError):
@implements(GenFooInterface)
class AsyncGenFooImplementationFail: # skipcq: PYL-W0612
async def foo(self):
yield 1
@implements(GenFooInterface)
class GenFooImplementation: # skipcq: PYL-W0612
def foo(self): # skipcq: PYL-R0201
yield 1
def test_asyncgen_method():
class AsyncGenFooInterface:
async def foo(self):
yield 1
with pytest.raises(NotImplementedError):
@implements(AsyncGenFooInterface)
class AsyncFooImplementationFail: # skipcq: PYL-W0612
async def foo(self):
pass
with pytest.raises(NotImplementedError):
@implements(AsyncGenFooInterface)
class GenFooImplementationFail: # skipcq: PYL-W0612
def foo(self): # skipcq: PYL-R0201
yield 1
@implements(AsyncGenFooInterface)
class AsyncGenFooImplementation: # skipcq: PYL-W0612
async def foo(self):
yield 1
@py36
def test_new_style_descriptors():
class IntField:
def __get__(self, instance, owner):
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, int):
raise ValueError('expecting integer in {}'.format(self.name))
instance.__dict__[self.name] = value
def __set_name__(self, owner, name):
self.name = name # skipcq: PYL-W0201
class FooInterface(Interface):
int_field = IntField()
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
pass
@implements(FooInterface)
class FooImplementationPass:
int_field = IntField()
@py36
def test_new_style_metaclasses():
class Polygon:
def __init_subclass__(cls, sides, **kwargs):
cls.sides = sides
if cls.sides < 3:
raise ValueError('polygons need 3+ sides')
@classmethod
def interior_angles(cls):
return (cls.sides - 2) * 180
class PolygonInterface(Interface):
def rotate(self):
pass
@implements(PolygonInterface)
class Triangle(Polygon, sides=3):
def rotate(self):
pass
def test_descriptors_signature_getter():
class FooInterface(Interface):
@property
def someprop(self) -> str:
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def someprop(self) -> int:
pass
def test_descriptors_signature_setter():
class FooInterface(Interface):
@property
def someprop(self):
pass
@someprop.setter
def someprop(self, value: str) -> str:
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def someprop(self):
pass
@someprop.setter
def someprop(self, value: int) -> float:
pass
def test_descriptors_signature_deleter():
class FooInterface(Interface):
@property
def someprop(self):
pass
@someprop.deleter
def someprop(self) -> str:
pass
with pytest.raises(NotImplementedError):
@implements(FooInterface)
class FooImplementationFail:
@property
def someprop(self):
pass
@someprop.deleter
def someprop(self) -> int:
pass
def test_get_mro():
class RegularClass:
pass
mro = get_mro(RegularClass)
assert object not in mro
expected = RegularClass.mro()[:-1]
assert mro == expected
def test_class_hierarchy_overlap_of_common_class():
class CommonClass:
pass
class FooInterface(CommonClass):
def abc(self) -> str:
pass
with pytest.raises(ValueError):
@implements(FooInterface)
class FooImplemenation(CommonClass):
def abc(self) -> str:
pass
def test_implementation_inheriting_from_interface():
class FooInterface:
def abc(self) -> str:
pass
with pytest.raises(ValueError):
@implements(FooInterface)
class FooImplemenation(FooInterface):
def abc(self) -> str:
pass