-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathtest_abstract.py
205 lines (137 loc) · 4.4 KB
/
test_abstract.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
import abc
from typing import ClassVar, TYPE_CHECKING
import equinox as eqx
import pytest
if TYPE_CHECKING:
from typing import ClassVar as AbstractClassVar, ClassVar as AbstractVar
else:
from equinox.internal import AbstractClassVar, AbstractVar
def test_abstract_method():
class A(eqx.Module):
@abc.abstractmethod
def x(self): ...
class B(A):
pass
with pytest.raises(TypeError, match="abstract method"):
A()
with pytest.raises(TypeError, match="abstract method"):
B()
def test_abstract_attribute_stringified():
with pytest.raises(NotImplementedError):
class A(eqx.Module):
x: "AbstractVar[bool]"
def test_abstract_attribute():
class A(eqx.Module):
x: AbstractVar[bool]
assert A.__abstractvars__ == frozenset({"x"})
assert A.__abstractclassvars__ == frozenset()
with pytest.raises(TypeError, match="abstract attributes"):
A()
class B(A):
y: int
assert B.__abstractvars__ == frozenset({"x"})
assert B.__abstractclassvars__ == frozenset()
with pytest.raises(TypeError, match="abstract attributes"):
B(y=2)
class C(A):
x: bool
y: str
assert C.__abstractvars__ == frozenset()
assert C.__abstractclassvars__ == frozenset()
C(x=True, y="hi")
class D(A):
y: str
x: bool # different order
assert D.__abstractvars__ == frozenset()
assert D.__abstractclassvars__ == frozenset()
d = D("hi", True)
assert d.x is True
assert d.y == "hi"
class E(A):
y: str
@property
def x(self):
return True
assert E.__abstractvars__ == frozenset()
assert E.__abstractclassvars__ == frozenset()
e = E(y="hi")
assert e.x is True
assert e.y == "hi"
with pytest.raises(TypeError, match="unsubscripted"):
class F(eqx.Module):
x: AbstractVar
class G(A):
x: AbstractVar[bool]
assert G.__abstractvars__ == frozenset({"x"})
assert G.__abstractclassvars__ == frozenset()
class H(A):
x: AbstractClassVar[bool]
assert H.__abstractvars__ == frozenset()
assert H.__abstractclassvars__ == frozenset({"x"})
class I(A): # noqa: E742
x: bool
assert I.__abstractvars__ == frozenset()
assert I.__abstractclassvars__ == frozenset()
I(True)
with pytest.raises(TypeError, match="cannot have value"):
class J(eqx.Module):
x: AbstractVar[bool] = True
class K(A):
x = True
y: bool = False
assert K.__abstractvars__ == frozenset()
assert K.__abstractclassvars__ == frozenset()
K()
class L(A):
x = True
assert L.__abstractvars__ == frozenset()
assert L.__abstractclassvars__ == frozenset()
L()
def test_abstract_class_attribute():
class A(eqx.Module):
x: AbstractClassVar[bool]
assert A.__abstractvars__ == frozenset()
assert A.__abstractclassvars__ == frozenset({"x"})
with pytest.raises(TypeError, match="abstract class attributes"):
A()
class B(A):
y: int
assert B.__abstractvars__ == frozenset()
assert B.__abstractclassvars__ == frozenset({"x"})
with pytest.raises(TypeError, match="abstract class attributes"):
B(y=2)
with pytest.raises(TypeError, match="unsubscripted"):
class C(eqx.Module):
x: AbstractClassVar
class D(A):
x: AbstractClassVar[bool]
assert D.__abstractvars__ == frozenset()
assert D.__abstractclassvars__ == frozenset({"x"})
class E(A):
x: ClassVar[bool]
assert E.__abstractvars__ == frozenset()
assert E.__abstractclassvars__ == frozenset()
with pytest.raises(TypeError, match="cannot have value"):
class F(eqx.Module):
x: AbstractClassVar[bool] = True
class G(A):
x = True
y: bool = False
assert G.__abstractvars__ == frozenset()
assert G.__abstractclassvars__ == frozenset()
class H(A):
x = True
assert H.__abstractvars__ == frozenset()
assert H.__abstractclassvars__ == frozenset()
class I(A): # noqa: E742
x: AbstractVar[bool]
assert I.__abstractvars__ == frozenset()
assert I.__abstractclassvars__ == frozenset({"x"})
def test_abstract_multiple_inheritance():
class A(eqx.Module):
x: AbstractVar[int]
class B(eqx.Module):
x: int
class C(B, A):
pass
C(1)