Repository navigation
Expand file tree
/
Copy pathtest_attributes.py
More file actions
586 lines (410 loc) · 16.7 KB
/
Copy pathtest_attributes.py
File metadata and controls
586 lines (410 loc) · 16.7 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
"""Tests for attribute-mode bindings."""
from typing import Any, ClassVar
import pytest
import wrapture
from wrapture import binding
class Model:
status = "new" # plain class default
def __init__(self) -> None:
self.count = 0
class Priced:
def __init__(self) -> None:
self._price = 100
@property
def price(self) -> int:
return self._price
@price.setter
def price(self, value: int) -> None:
if value < 0:
raise ValueError("negative price")
self._price = value
@price.deleter
def price(self) -> None:
self._price = 0
# ---------------------------------------------------------------------------
# passthrough semantics: applied with no behaviour, nothing changes
# ---------------------------------------------------------------------------
def test_applied_binding_with_no_behaviour_is_transparent() -> None:
status = binding(Model, "status")
with status:
model = Model()
assert model.status == "new" # class default read
model.status = "sent"
assert model.status == "sent" # instance value beats default
assert vars(model)["status"] == "sent"
del model.status
assert model.status == "new" # default visible again
with pytest.raises(AttributeError):
del model.status # nothing left to delete
assert vars(Model)["status"] == "new" # restored exactly
def test_class_level_access_returns_the_descriptor() -> None:
status = binding(Model, "status").apply()
try:
from wrapture.attributes import AttributeDescriptor
assert type(vars(Model)["status"]) is AttributeDescriptor
assert Model.status == "new" # proxy delegates to the prior
finally:
status.remove()
def test_property_keeps_working_beneath_the_binding() -> None:
price = binding(Priced, "price")
with price:
priced = Priced()
assert priced.price == 100 # getter runs
priced.price = 50 # setter runs
assert priced.price == 50
with pytest.raises(ValueError, match="negative"):
priced.price = -1 # setter validation honoured
del priced.price # deleter runs
assert priced.price == 0
assert isinstance(vars(Priced)["price"], property) # restored
# ---------------------------------------------------------------------------
# get behaviour
# ---------------------------------------------------------------------------
def test_get_returns_replaces_the_value() -> None:
status = binding(Model, "status").on_get.returns("stub")
with status:
assert Model().status == "stub"
assert Model().status == "new"
def test_get_transforms_rewrites_the_value_read() -> None:
status = binding(Model, "status").on_get.transforms(str.upper)
with status:
model = Model()
assert model.status == "NEW"
model.status = "sent"
assert model.status == "SENT" # instance value transformed too
assert vars(model)["status"] == "sent" # stored value untouched
def test_get_validates_observes_the_value() -> None:
seen: list[str] = []
status = binding(Model, "status").on_get.validates(seen.append)
with status:
assert Model().status == "new"
assert seen == ["new"]
def test_get_raises_injects_a_failure() -> None:
status = binding(Model, "status").on_get.raises(RuntimeError("no reads"))
with status:
with pytest.raises(RuntimeError, match="no reads"):
_ = Model().status
def test_get_decorates_controls_the_read() -> None:
status = binding(Model, "status")
def around(read: Any, instance: Any) -> str:
return f"<{read()}:{type(instance).__name__}>"
status.on_get.decorates(around)
with status:
assert Model().status == "<new:Model>"
# ---------------------------------------------------------------------------
# set behaviour
# ---------------------------------------------------------------------------
def test_set_transforms_rewrites_the_value_written() -> None:
status = binding(Model, "status").on_set.transforms(str.lower)
with status:
model = Model()
model.status = "SENT"
assert vars(model)["status"] == "sent"
def test_set_validates_can_reject() -> None:
def known(value: str) -> None:
assert value in ("new", "sent"), f"unknown status: {value}"
status = binding(Model, "status").on_set.validates(known)
with status:
model = Model()
model.status = "sent"
with pytest.raises(AssertionError, match="unknown status"):
model.status = "bogus"
assert model.status == "sent" # rejected write did not land
def test_set_rejects_makes_the_attribute_read_only() -> None:
status = binding(Model, "status").on_set.rejects()
with status:
model = Model()
assert model.status == "new" # reads unaffected
with pytest.raises(AttributeError, match="can't set"):
model.status = "sent"
def test_set_decorates_controls_the_write() -> None:
written: list[tuple[str, str]] = []
status = binding(Model, "status")
def around(write: Any, instance: Any, value: str) -> None:
written.append((type(instance).__name__, value))
write(value)
status.on_set.decorates(around)
with status:
model = Model()
model.status = "sent"
assert model.status == "sent"
assert written == [("Model", "sent")]
# ---------------------------------------------------------------------------
# delete behaviour
# ---------------------------------------------------------------------------
def test_delete_rejects_prevents_deletion() -> None:
status = binding(Model, "status").on_delete.rejects()
with status:
model = Model()
model.status = "sent"
with pytest.raises(AttributeError, match="can't delete"):
del model.status
assert model.status == "sent"
def test_delete_validates_observes_the_instance() -> None:
seen: list[Any] = []
status = binding(Model, "status").on_delete.validates(seen.append)
with status:
model = Model()
model.status = "sent"
del model.status
assert model.status == "new"
assert seen == [model]
def test_delete_decorates_controls_the_delete() -> None:
status = binding(Model, "status")
def around(erase: Any, instance: Any) -> None:
instance.status_archive = instance.status
erase()
status.on_delete.decorates(around)
with status:
model = Model()
model.status = "sent"
del model.status
assert model.status_archive == "sent" # type: ignore[attr-defined]
assert model.status == "new"
# ---------------------------------------------------------------------------
# operations are independent
# ---------------------------------------------------------------------------
def test_operations_configure_independently() -> None:
status = binding(Model, "status")
status.on_get.transforms(str.upper)
status.on_set.rejects()
with status:
model = Model()
assert model.status == "NEW"
with pytest.raises(AttributeError):
model.status = "sent"
status.on_set.passes_through() # clears set only
model.status = "sent"
assert model.status == "SENT" # get behaviour still applies
# ---------------------------------------------------------------------------
# missing attributes
# ---------------------------------------------------------------------------
def test_missing_ok_binds_an_instance_only_attribute() -> None:
class Session:
def __init__(self) -> None:
self.token = "abc"
seen: list[str] = []
token = binding(Session, "token", missing_ok=True)
token.on_set.validates(seen.append)
with token:
session = Session() # __init__ write goes through the binding
assert session.token == "abc"
session.token = "xyz"
assert seen == ["abc", "xyz"]
assert "token" not in vars(Session) # no residue on the class
def test_missing_attribute_reads_raise_until_written() -> None:
class Bare:
pass
value = binding(Bare, "value", missing_ok=True)
with value:
bare = Bare()
with pytest.raises(AttributeError):
_ = bare.value # type: ignore[attr-defined]
bare.value = 1 # type: ignore[attr-defined]
assert bare.value == 1 # type: ignore[attr-defined]
assert "value" not in vars(Bare)
def test_explicit_mode_typo_surfaces_at_apply() -> None:
# An explicit mode= skips creation-time detection, so a misspelled
# name must fail at apply() instead, for both modes.
attr = binding(Model, "sttaus", mode="attribute")
with pytest.raises(AttributeError, match="sttaus"):
attr.apply()
assert not attr.applied
call = binding(Model, "sttaus", mode="callable")
with pytest.raises(AttributeError):
call.apply()
assert not call.applied
# ---------------------------------------------------------------------------
# inherited attributes
# ---------------------------------------------------------------------------
def test_inherited_default_is_found_and_restored() -> None:
class Base:
colour = "red"
class Derived(Base):
pass
colour = binding(Derived, "colour").on_get.transforms(str.upper)
with colour:
assert Derived().colour == "RED" # inherited default beneath
assert "colour" in vars(Derived) # shadowing descriptor installed
assert Base().colour == "red" # base class untouched
assert "colour" not in vars(Derived) # shadow removed, not restored
assert Derived().colour == "red"
# ---------------------------------------------------------------------------
# suspension
# ---------------------------------------------------------------------------
def test_suspended_attribute_binding_is_transparent_and_counts() -> None:
status = binding(Model, "status").on_get.returns("stub").apply()
try:
model = Model()
assert model.status == "stub"
status.suspend()
assert model.status == "new" # real read
model.status = "sent" # real write
del model.status # real delete
assert status.suspended_calls == 3
status.resume()
assert model.status == "stub"
finally:
status.remove()
# ---------------------------------------------------------------------------
# lifecycle parity with callable mode
# ---------------------------------------------------------------------------
def test_attribute_binding_lifecycle_and_state() -> None:
status = binding(Model, "status")
assert not status.applied and not status.active
status.apply()
try:
assert status.applied and status.active
assert "active" in repr(status)
finally:
status.remove()
assert not status.active
assert vars(Model)["status"] == "new"
# reusable across cycles
with status:
assert status.active
def test_attribute_binding_reports_displacement() -> None:
status = binding(Model, "status").apply()
try:
original = vars(Model)["status"]
Model.status = "hijacked"
assert status.applied
assert not status.active
assert "displaced" in repr(status)
finally:
Model.status = "new"
status.remove()
assert vars(Model)["status"] == "new"
del original
def test_two_attribute_bindings_compose() -> None:
outer = binding(Model, "status").on_get.transforms(lambda v: f"outer({v})")
inner = binding(Model, "status").on_get.transforms(lambda v: f"inner({v})")
inner.apply()
outer.apply()
try:
assert Model().status == "outer(inner(new))"
finally:
# remove in application order: the buried one is spliced out
inner.remove()
assert Model().status == "outer(new)"
outer.remove()
assert Model().status == "new"
assert vars(Model)["status"] == "new"
# ---------------------------------------------------------------------------
# combining attribute and callable bindings on one method
# ---------------------------------------------------------------------------
class Service:
def ping(self) -> str:
return "pong"
def test_attribute_and_callable_bindings_stack_on_a_method() -> None:
# An attribute binding on a method observes the access that produces
# the bound method; a callable binding observes the call. Stacked,
# both fire. The attribute binding needs an explicit mode=, since
# detection classifies a method as callable.
reads: list[Any] = []
access = binding(Service, "ping", mode="attribute")
access.on_get.validates(reads.append)
calls = binding(Service, "ping").on_call.transforms_result(str.upper)
access.apply()
calls.apply()
try:
service = Service()
assert service.ping() == "PONG" # call behaviour ran
assert len(reads) == 1 # and the access was observed
_ = service.ping # bare access, no call
assert len(reads) == 2
finally:
calls.remove()
access.remove()
assert Service().ping() == "pong"
assert vars(Service)["ping"].__name__ == "ping" # fully restored
def test_stacked_method_bindings_remove_in_either_order() -> None:
reads: list[Any] = []
calls = binding(Service, "ping").on_call.transforms_result(str.upper)
access = binding(Service, "ping", mode="attribute")
access.on_get.validates(reads.append)
# Opposite stacking order to the test above, and the buried callable
# wrapper is removed first, exercising the splice path.
calls.apply()
access.apply()
try:
assert Service().ping() == "PONG"
assert len(reads) == 1
calls.remove()
assert Service().ping() == "pong" # call behaviour gone
assert len(reads) == 2 # access still observed
finally:
access.remove()
assert Service().ping() == "pong"
def test_on_get_can_decorate_the_bound_method_on_the_fly() -> None:
# The pattern documented in docs/monkey-patching.md: minting a wrapt
# FunctionWrapper for the bound method at each access, so decoration
# is decided per access with the instance in hand.
import wrapt
class Gateway:
audited = False
def charge(self, amount: int) -> dict[str, Any]:
return {"id": f"ch_{amount}", "amount": amount}
seen: list[tuple[Any, tuple[Any, ...]]] = []
def audit(
wrapped: Any, instance: Any, args: tuple[Any, ...], kwargs: dict[str, Any]
) -> Any:
seen.append((instance, args))
return wrapped(*args, **kwargs)
def selective(read: Any, instance: Any) -> Any:
bound = read()
if instance.audited:
return wrapt.FunctionWrapper(bound, audit)
return bound
charge = binding(Gateway, "charge", mode="attribute")
charge.on_get.decorates(selective)
with charge:
flagged, unflagged = Gateway(), Gateway()
flagged.audited = True
assert flagged.charge(1) == {"id": "ch_1", "amount": 1}
assert unflagged.charge(2) == {"id": "ch_2", "amount": 2}
# wrapt extracts __self__ from the bound method, so the wrapper
# saw the right instance, and only for the flagged one.
assert seen == [(flagged, (1,))]
# a stored bound method stays decorated when called later
callback = flagged.charge
callback(3)
assert seen[-1] == (flagged, (3,))
assert flagged.charge(4) == {"id": "ch_4", "amount": 4} # restored
assert len(seen) == 2
# ---------------------------------------------------------------------------
# slots
# ---------------------------------------------------------------------------
def test_slots_member_is_attribute_mode() -> None:
class Point:
__slots__ = ("x",)
x: int
x = binding(Point, "x")
assert x.mode == "attribute"
x.on_set.transforms(lambda v: v * 2)
with x:
point = Point()
point.x = 5
assert point.x == 10
point2 = Point()
point2.x = 5
assert point2.x == 5 # restored
# ---------------------------------------------------------------------------
# group with mixed modes
# ---------------------------------------------------------------------------
def test_group_mixes_callable_and_attribute_bindings() -> None:
class Ledger:
rate: ClassVar[float] = 0.05
def record(self, entry: str) -> str:
return f"led-{entry}"
group = wrapture.bindings(
rate=binding(Ledger, "rate"), record=binding(Ledger, "record")
)
group.rate.on_get.returns(0.10)
group.record.on_call.returns("led-stub")
with group:
ledger = Ledger()
assert ledger.rate == 0.10
assert ledger.record("x") == "led-stub"
assert Ledger().rate == 0.05
assert Ledger().record("x") == "led-x"