Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set a trait link directly #642

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
BaseDescriptor,
HasDescriptors,
CUnicode,
)
SourceLink)
from traitlets.utils import cast_unicode


Expand Down Expand Up @@ -2019,6 +2019,17 @@ def another_update(self, change):
l = link((mc, "i"), (mc, "j"))
self.assertRaises(TraitError, setattr, mc, 'j', 2)

def test_source_link(self):
class A(HasTraits):
a = Int()

x1 = A()
x2 = A(a = SourceLink(x1, "a", transform=(lambda x: x+1, lambda x: x-1)))
x1.a = 3
self.assertEqual(x2.a, 4)
x2.a = 1
self.assertEqual(x1.a, 0)

class TestDirectionalLink(TestCase):
def test_connect_same(self):
"""Verify two traitlets of the same type can be linked together using directional_link."""
Expand Down Expand Up @@ -2110,6 +2121,17 @@ class A(HasTraits):
a.value += 1
self.assertEqual(a.value, b.value)

def test_source_directional_link(self):
class A(HasTraits):
a = Int()

x1 = A()
x2 = A(a = SourceLink(x1, "a", link_type=directional_link, transform=lambda x: x+1))
x1.a = 3
self.assertEqual(x2.a, 4)
x2.a = 1
self.assertEqual(x1.a, 3)

class Pickleable(HasTraits):

i = Int()
Expand Down
21 changes: 17 additions & 4 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@
# Adapted from enthought.traits, Copyright (c) Enthought, Inc.,
# also under the terms of the Modified BSD License.

from ast import literal_eval
import contextlib
import enum
import inspect
import os
import re
import sys
import types
import enum
from ast import literal_eval
from warnings import warn, warn_explicit

from .utils.bunch import Bunch
from .utils.descriptions import describe, class_of, add_article, repr_type
from .utils.getargspec import getargspec
from .utils.importstring import import_item
from .utils.sentinel import Sentinel
from .utils.bunch import Bunch
from .utils.descriptions import describe, class_of, add_article, repr_type

SequenceTypes = (list, tuple, set, frozenset)

Expand Down Expand Up @@ -81,6 +81,7 @@
"BaseDescriptor",
"TraitType",
"parse_notifier_name",
"SourceLink"
]

# any TraitType subclass (that doesn't start with _) will be added automatically
Expand Down Expand Up @@ -246,6 +247,7 @@ def _validate_link(*tuples):
if not trait_name in obj.traits():
raise TypeError("%r has no trait %r" % (obj, trait_name))


class link(object):
"""Link traits from different objects together so they remain in sync.

Expand Down Expand Up @@ -366,6 +368,14 @@ def unlink(self):
dlink = directional_link


class SourceLink:
def __init__(self, obj, attr, link_type=link, transform=None):
self.obj = obj
self.attr = attr
self.link_type = link_type
self.transform = transform


#-----------------------------------------------------------------------------
# Base Descriptor Class
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -600,6 +610,8 @@ def __set__(self, obj, value):
"""
if self.read_only:
raise TraitError('The "%s" trait is read-only.' % self.name)
elif isinstance(value, SourceLink):
value.link_type((value.obj, value.attr), (obj, self.name), transform=value.transform)
else:
self.set(obj, value)

Expand Down Expand Up @@ -731,6 +743,7 @@ def tag(self, **metadata):
def default_value_repr(self):
return repr(self.default_value)


#-----------------------------------------------------------------------------
# The HasTraits implementation
#-----------------------------------------------------------------------------
Expand Down