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

Function test_observe_iterables

tests/test_traitlets.py:2745–2782  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2743
2744
2745def test_observe_iterables():
2746 class C(HasTraits):
2747 i = Integer()
2748 s = Unicode()
2749
2750 c = C()
2751 recorded = {}
2752
2753 def record(change):
2754 recorded["change"] = change
2755
2756 # observe with names=set
2757 c.observe(record, names={"i", "s"})
2758 c.i = 5
2759 assert recorded["change"].name == "i"
2760 assert recorded["change"].new == 5
2761 c.s = "hi"
2762 assert recorded["change"].name == "s"
2763 assert recorded["change"].new == "hi"
2764
2765 # observe with names=custom container with iter, contains
2766 class MyContainer:
2767 def __init__(self, container):
2768 self.container = container
2769
2770 def __iter__(self):
2771 return iter(self.container)
2772
2773 def __contains__(self, key):
2774 return key in self.container
2775
2776 c.observe(record, names=MyContainer({"i", "s"}))
2777 c.i = 10
2778 assert recorded["change"].name == "i"
2779 assert recorded["change"].new == 10
2780 c.s = "ok"
2781 assert recorded["change"].name == "s"
2782 assert recorded["change"].new == "ok"
2783
2784
2785def test_super_args():

Callers

nothing calls this directly

Calls 3

MyContainerClass · 0.85
observeMethod · 0.80
CClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…