From 3e738154671f23fca0275736fea5a52ad4b3cc1c 2014-02-03 20:48:25
From: Jonathan Frederic <jdfreder@calpoly.edu>
Date: 2014-02-03 20:48:25
Subject: [PATCH] fixes to make Connect pass tests

---

diff --git a/IPython/utils/tests/test_traitlets.py b/IPython/utils/tests/test_traitlets.py
index 2e92bac..c2d3301 100644
--- a/IPython/utils/tests/test_traitlets.py
+++ b/IPython/utils/tests/test_traitlets.py
@@ -32,7 +32,7 @@ from IPython.utils.traitlets import (
     HasTraits, MetaHasTraits, TraitType, Any, CBytes, Dict,
     Int, Long, Integer, Float, Complex, Bytes, Unicode, TraitError,
     Undefined, Type, This, Instance, TCPAddress, List, Tuple,
-    ObjectName, DottedObjectName, CRegExp
+    ObjectName, DottedObjectName, CRegExp, Connect
 )
 from IPython.utils import py3compat
 from IPython.testing.decorators import skipif
@@ -974,60 +974,61 @@ def test_dict_assignment():
     nt.assert_equal(d, c.value)
     nt.assert_true(c.value is d)
 
-def test_connect_same:
-    """Verify two traitlets of the same type can be bound together using Connect"""
+class TestConnect(TestCase):
+    def test_connect_same(self):
+        """Verify two traitlets of the same type can be bound together using Connect"""
 
-    # Create two simple classes with Int traitlets.
-    class A():
-        value = Int()
-    a = A(value=9)
-    b = A(value=8)
-
-    # Conenct the two classes.
-    c = Connect((a, 'value'), (b, 'value'))
-
-    # Make sure the values are the same at the point of connection.
-    assertEqual(a.value, b.value)
+        # Create two simple classes with Int traitlets.
+        class A(HasTraits):
+            value = Int()
+        a = A(value=9)
+        b = A(value=8)
 
-    # Change one of the values to make sure they stay in sync.
-    a.value = 5
-    assertEqual(a.value, b.value)
+        # Conenct the two classes.
+        c = Connect((a, 'value'), (b, 'value'))
 
-def test_connect_different:
-    """Verify two traitlets of different types can be bound together using Connect"""
+        # Make sure the values are the same at the point of connection.
+        self.assertEqual(a.value, b.value)
 
-    # Create two simple classes with Int traitlets.
-    class A():
-        value = Int()
-    class B():
-        count = Int()
-    a = A(value=9)
-    b = B(count=8)
+        # Change one of the values to make sure they stay in sync.
+        a.value = 5
+        self.assertEqual(a.value, b.value)
 
-    # Conenct the two classes.
-    c = Connect((a, 'value'), (b, 'count'))
+    def test_connect_different(self):
+        """Verify two traitlets of different types can be bound together using Connect"""
 
-    # Make sure the values are the same at the point of connection.
-    assertEqual(a.value, b.count)
+        # Create two simple classes with Int traitlets.
+        class A(HasTraits):
+            value = Int()
+        class B(HasTraits):
+            count = Int()
+        a = A(value=9)
+        b = B(count=8)
 
-    # Change one of the values to make sure they stay in sync.
-    a.value = 5
-    assertEqual(a.value, b.count)
+        # Conenct the two classes.
+        c = Connect((a, 'value'), (b, 'count'))
 
-def test_disconnect:
-    """Verify two connected traitlets can be disconnected"""
+        # Make sure the values are the same at the point of connection.
+        self.assertEqual(a.value, b.count)
 
-    # Create two simple classes with Int traitlets.
-    class A():
-        value = Int()
-    a = A(value=9)
-    b = A(value=8)
+        # Change one of the values to make sure they stay in sync.
+        a.value = 5
+        self.assertEqual(a.value, b.count)
 
-    # Conenct the two classes.
-    c = Connect((a, 'value'), (b, 'value'))
-    a.value = 4
-    c.disconnect()
+    def test_disconnect(self):
+        """Verify two connected traitlets can be disconnected"""
 
-    # Change one of the values to make sure they stay in sync.
-    a.value = 5
-    assertNotEqual(a.value, b.value)
\ No newline at end of file
+        # Create two simple classes with Int traitlets.
+        class A(HasTraits):
+            value = Int()
+        a = A(value=9)
+        b = A(value=8)
+
+        # Conenct the two classes.
+        c = Connect((a, 'value'), (b, 'value'))
+        a.value = 4
+        c.disconnect()
+
+        # Change one of the values to make sure they stay in sync.
+        a.value = 5
+        self.assertNotEqual(a.value, b.value)
diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py
index ccff9f8..2294d8b 100644
--- a/IPython/utils/traitlets.py
+++ b/IPython/utils/traitlets.py
@@ -199,10 +199,17 @@ class Connect(object):
     """
     updating = False
     def __init__(self, *args):
+        if len(args) < 2:
+            raise TypeError('At least two traitlets must be provided.')
+
         self.objects = args
         for obj,attr in args:
             obj.on_trait_change(self._update, attr)
 
+        # Syncronize the traitlets initially.
+        initial = getattr(args[0][0], args[0][1])
+        self._update(args[0][1], initial, initial)
+
     @contextlib.contextmanager
     def _busy_updating(self):
         self.updating = True