-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_strict.py
106 lines (82 loc) · 2.25 KB
/
test_strict.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
# pylint: disable=attribute-defined-outside-init, no-self-use, too-few-public-methods
import unittest
from pystrict import strict, StrictError
class TestStrict(unittest.TestCase):
def test_frozen(self):
@strict
class Foo:
cdef: int = 1
idef: int
def __init__(self):
self.idef = 2
with self.assertRaises(StrictError):
x = Foo()
x.bad = 1
x = Foo()
x.cdef = 2
x.idef = 3
def test_frozen_inherit(self):
@strict
class Foo:
cdef: int = 1
idef: int
def __init__(self):
self.idef = 2
@strict
class Bar(Foo):
def __init__(self):
self.bdef = 2
super().__init__()
self.edef = 2
with self.assertRaises(StrictError):
x = Foo()
x.bad = 1
x = Foo()
x.cdef = 2
x.idef = 3
y = Bar()
y.cdef = 2
y.idef = 3
y.bdef = 4
y.edef = 4
def test_untagged_inherit(self):
@strict
class Foo:
cdef: int = 1
idef: int
def __init__(self):
self.idef = 2
class Bar(Foo):
def __init__(self):
self.bdef = 2
super().__init__()
self.edef = 2
# parent still strict
with self.assertRaises(StrictError):
x = Foo()
x.bad = 1
# child not tagged strict... so ... not strict at all
y = Bar()
y.whatever = 9
def test_init_typing(self):
with self.assertRaises(StrictError):
@strict
class Foo:
def __init__(self, z):
pass
Foo(1)
def test_strict_function(self):
with self.assertRaises(StrictError):
@strict
def plusone(x) -> int:
return x + 1
plusone(2)
with self.assertRaises(StrictError):
@strict
def plusone2(x: int):
return x + 1
plusone2(2)
@strict
def bop(x: int) -> int:
return x + 1
bop(1)