Repository navigation
Expand file tree
/
Copy pathtest_module_attributes.py
More file actions
391 lines (271 loc) · 11.1 KB
/
Copy pathtest_module_attributes.py
File metadata and controls
391 lines (271 loc) · 11.1 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
"""Tests for attribute-mode bindings whose owner is a module."""
import sys
import types
from collections.abc import Iterator
from typing import Any
import pytest
import wrapture
from wrapture import binding
from wrapture.attributes import _module_class
def _swap_class(module: Any, cls: type) -> None:
"""Assign a module's __class__ the way a lazy-import shim would."""
module.__class__ = cls
@pytest.fixture
def config() -> Iterator[Any]:
"""A throwaway module registered in sys.modules, so string targets
resolve to it, removed again afterwards. Typed Any so attribute
access on it is unchecked."""
module: Any = types.ModuleType("wrapture_modattr_config")
vars(module)["TIMEOUT"] = 30
vars(module)["RETRIES"] = 3
sys.modules[module.__name__] = module
try:
yield module
finally:
sys.modules.pop(module.__name__, None)
assert type(module) is types.ModuleType
# ---------------------------------------------------------------------------
# detection, labels and passthrough
# ---------------------------------------------------------------------------
def test_module_data_is_attribute_mode(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
assert timeout.mode == "attribute"
assert timeout.label is None
assert timeout.path == "wrapture_modattr_config:TIMEOUT"
assert timeout.path == "wrapture_modattr_config:TIMEOUT"
def test_string_target_resolves_module(config: Any) -> None:
timeout = binding("wrapture_modattr_config", "TIMEOUT")
with timeout:
assert timeout.active
assert config.TIMEOUT == 30
def test_passthrough_leaves_value_and_type(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
with timeout:
assert type(config) is not types.ModuleType
assert issubclass(type(config), types.ModuleType)
assert type(config).__name__ == "module"
assert config.TIMEOUT == 30
config.TIMEOUT = 40
assert config.TIMEOUT == 40
assert vars(config)["TIMEOUT"] == 40
del config.TIMEOUT
assert not hasattr(config, "TIMEOUT")
config.TIMEOUT = 30
assert type(config) is types.ModuleType
assert config.TIMEOUT == 30
def test_other_attributes_are_untouched(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
with timeout:
assert "TIMEOUT" in vars(type(config))
assert "RETRIES" not in vars(type(config))
assert config.RETRIES == 3
assert config.__name__ == "wrapture_modattr_config"
# ---------------------------------------------------------------------------
# behaviour
# ---------------------------------------------------------------------------
def test_get_returns_and_phases(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
timeout.on_get.returns(5)
timeout.on_get.then(after=2).passes_through()
with timeout:
assert config.TIMEOUT == 5
assert config.TIMEOUT == 5
assert config.TIMEOUT == 30
def test_get_returns_from(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
timeout.on_get.returns_from(iter([1, 2]))
timeout.on_get.then().passes_through()
with timeout:
assert [config.TIMEOUT for _ in range(3)] == [1, 2, 30]
def test_set_transforms_and_raises(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
timeout.on_set.transforms(lambda value: value * 2)
with timeout:
config.TIMEOUT = 10
assert config.TIMEOUT == 20
config.TIMEOUT = 30
frozen = binding(config, "TIMEOUT")
frozen.on_set.raises(RuntimeError("frozen"))
with frozen:
with pytest.raises(RuntimeError, match="frozen"):
config.TIMEOUT = 1
assert config.TIMEOUT == 30
def test_delete_raises(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
timeout.on_delete.raises(PermissionError("keep it"))
with timeout:
with pytest.raises(PermissionError):
del config.TIMEOUT
assert config.TIMEOUT == 30
def test_suspend_and_resume(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
timeout.on_get.returns(5)
with timeout:
timeout.suspend()
assert config.TIMEOUT == 30
assert timeout.active
timeout.resume()
assert config.TIMEOUT == 5
# ---------------------------------------------------------------------------
# recording
# ---------------------------------------------------------------------------
def test_events_carry_module_as_instance(config: Any) -> None:
timeout = binding(config, "TIMEOUT")
with timeout, wrapture.timeline() as tape:
_ = config.TIMEOUT
config.TIMEOUT = 31
del config.TIMEOUT
config.TIMEOUT = 30
events = tape.for_binding(timeout)
assert [event.kind for event in events] == ["get", "set", "delete", "set"]
assert all(event.instance is config for event in events)
assert events[0].path == "wrapture_modattr_config:TIMEOUT"
assert events[0].result == 30
assert events[1].value == 31
assert events[1].previous == 30
assert events[2].previous == 31
# ---------------------------------------------------------------------------
# sharing one class across bindings
# ---------------------------------------------------------------------------
def test_two_names_share_one_class(config: Any) -> None:
timeout = binding(config, "TIMEOUT").apply()
cls = type(config)
retries = binding(config, "RETRIES").apply()
assert type(config) is cls
assert "TIMEOUT" in vars(cls) and "RETRIES" in vars(cls)
timeout.remove()
assert type(config) is cls
assert "TIMEOUT" not in vars(cls)
assert config.RETRIES == 3
retries.remove()
assert type(config) is types.ModuleType
def test_two_bindings_on_one_name_compose(config: Any) -> None:
outer = binding(config, "TIMEOUT")
inner = binding(config, "TIMEOUT")
inner.on_get.returns(1)
outer.on_get.transforms(lambda value: value + 1)
inner.apply()
outer.apply()
assert config.TIMEOUT == 2
# Removing the inner one first splices it out of the chain.
inner.remove()
assert config.TIMEOUT == 31
assert outer.active and not inner.active
outer.remove()
assert config.TIMEOUT == 30
assert type(config) is types.ModuleType
def test_remove_in_either_order(config: Any) -> None:
first = binding(config, "TIMEOUT").apply()
second = binding(config, "TIMEOUT").apply()
first.remove()
assert second.active and not first.active
assert config.TIMEOUT == 30
second.remove()
assert type(config) is types.ModuleType
# ---------------------------------------------------------------------------
# missing names and module __getattr__
# ---------------------------------------------------------------------------
def test_missing_name_needs_missing_ok(config: Any) -> None:
# Detection resolves the name and fails there; an explicit mode=
# defers the check to apply, where the missing_ok hint is given.
with pytest.raises(Exception, match="NOPE"):
binding(config, "NOPE")
with pytest.raises(AttributeError, match="missing_ok"):
binding(config, "NOPE", mode="attribute").apply()
assert type(config) is types.ModuleType
def test_missing_ok_sees_later_assignment(config: Any) -> None:
later = binding(config, "LATER", missing_ok=True)
with later, wrapture.timeline() as tape:
with pytest.raises(AttributeError):
_ = config.LATER
config.LATER = 1
assert config.LATER == 1
del config.LATER
assert [event.kind for event in tape.for_binding(later)] == [
"get",
"set",
"get",
"delete",
]
assert tape.for_binding(later)[0].exception is not None
assert type(config) is types.ModuleType
def test_missing_ok_defers_to_module_getattr(config: Any) -> None:
vars(config)["__getattr__"] = lambda name: f"virtual {name}"
ghost = binding(config, "GHOST", missing_ok=True)
ghost.on_get.then(after=1).returns("stubbed")
with ghost:
assert config.GHOST == "virtual GHOST"
assert config.GHOST == "stubbed"
assert config.GHOST == "virtual GHOST"
# ---------------------------------------------------------------------------
# dotted paths and submodules
# ---------------------------------------------------------------------------
def test_dotted_path_to_submodule_attribute(config: Any) -> None:
sub = types.ModuleType("wrapture_modattr_config.sub")
vars(sub)["LEVEL"] = "info"
vars(config)["sub"] = sub
level = binding(config, "sub.LEVEL")
level.on_get.returns("debug")
assert level.label is None
assert level.path == "wrapture_modattr_config:sub.LEVEL"
with level:
assert config.sub.LEVEL == "debug"
assert type(config) is types.ModuleType
assert type(sub) is types.ModuleType
assert sub.LEVEL == "info"
# ---------------------------------------------------------------------------
# interaction with value bindings and class swaps
# ---------------------------------------------------------------------------
def test_value_binding_on_top_is_seen_as_set(config: Any) -> None:
watched = binding(config, "TIMEOUT")
held = binding(config, attr="TIMEOUT").overrides(99)
with watched, wrapture.timeline() as tape:
with held:
assert config.TIMEOUT == 99
assert config.TIMEOUT == 30
kinds = [event.kind for event in tape.for_binding(watched)]
assert kinds == ["set", "get", "set", "get"]
def test_class_swapped_on_top_is_left_in_place(config: Any) -> None:
timeout = binding(config, "TIMEOUT").apply()
ours = type(config)
class Layered(ours): # type: ignore[misc, valid-type]
pass
_swap_class(config, Layered)
assert config.TIMEOUT == 30
timeout.remove()
assert not timeout.active
assert type(config) is Layered
assert _module_class(config) is None
assert "TIMEOUT" not in vars(ours)
# A fresh binding layers again above the foreign class.
again = binding(config, "TIMEOUT").apply()
assert type(config) is not Layered
assert issubclass(type(config), Layered)
again.remove()
assert type(config) is Layered
_swap_class(config, types.ModuleType)
def test_existing_subclass_is_preserved_beneath(config: Any) -> None:
class Lazy(types.ModuleType):
def __getattr__(self, name: str) -> Any:
return f"lazy {name}"
_swap_class(config, Lazy)
timeout = binding(config, "TIMEOUT")
with timeout:
assert issubclass(type(config), Lazy)
assert config.TIMEOUT == 30
assert config.UNKNOWN == "lazy UNKNOWN"
assert type(config) is Lazy
_swap_class(config, types.ModuleType)
def test_refused_when_class_cannot_be_assigned() -> None:
class Stuck(types.ModuleType):
@property
def __class__(self) -> type:
return type(self)
@__class__.setter
def __class__(self, value: type) -> None:
raise TypeError("__class__ assignment is not allowed")
stuck = Stuck("stuck")
vars(stuck)["TIMEOUT"] = 1
with pytest.raises(TypeError, match="attr='TIMEOUT'"):
binding(stuck, "TIMEOUT").apply()
assert type(stuck) is Stuck