MCPcopy Create free account
hub / github.com/ipython/traitlets / test_hold_trait_notifications

Function test_hold_trait_notifications

tests/test_traitlets.py:2323–2365  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2321
2322
2323def test_hold_trait_notifications():
2324 changes = []
2325
2326 class Test(HasTraits):
2327 a = Integer(0)
2328 b = Integer(0)
2329
2330 def _a_changed(self, name, old, new):
2331 changes.append((old, new))
2332
2333 def _b_validate(self, value, trait):
2334 if value != 0:
2335 raise TraitError("Only 0 is a valid value")
2336 return value
2337
2338 # Test context manager and nesting
2339 t = Test()
2340 with t.hold_trait_notifications():
2341 with t.hold_trait_notifications():
2342 t.a = 1
2343 assert t.a == 1
2344 assert changes == []
2345 t.a = 2
2346 assert t.a == 2
2347 with t.hold_trait_notifications():
2348 t.a = 3
2349 assert t.a == 3
2350 assert changes == []
2351 t.a = 4
2352 assert t.a == 4
2353 assert changes == []
2354 t.a = 4
2355 assert t.a == 4
2356 assert changes == []
2357
2358 assert changes == [(0, 4)]
2359 # Test roll-back
2360 try:
2361 with t.hold_trait_notifications():
2362 t.b = 1 # raises a Trait error
2363 except Exception:
2364 pass
2365 assert t.b == 0
2366
2367
2368class RollBack(HasTraits):

Callers

nothing calls this directly

Calls 2

TestClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…