enthought / enthought/traits

Sync_trait() does not propagate mutations of Dict traits

Open
#1,486 2 comments 0 reactions 0 assignees View on GitHub
component: core type: enhancement
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Hi there! This issue is to report an un expected behaviour using `HasTraits.sync_trait()` with `Dict` traits. In the code snippet below, I have to `Dict` traits synchronised: when the `Dict` trait of the parent is reassigned, no problem, the one of the child got modified as well. However, a mutation of the dictionary is not propagated. Is it a known issue ?

```Python3
from traits.api import Dict, HasStrictTraits, Instance

class Bar(HasStrictTraits):

bar_dict = Dict()

class Foo(HasStrictTraits):

foo_dict = Dict()

bar = Instance(Bar)

def __init__(self, **traits):
super().__init__(**traits)

self.sync_trait('foo_dict', self.bar, 'bar_dict')

if __name__ == "__main__":
foo = Foo(bar=Bar())

foo.foo_dict = {"a": 1, "b": 2}
print(f"Bar dict: {foo.bar.bar_dict}")
# prints: Bar dict: {'a': 1, 'b': 2}

foo.foo_dict.update({"c": 3})
print(f"Bar dict: {foo.bar.bar_dict}")
# prints: Bar dict: {'a': 1, 'b': 2}

foo.foo_dict["d"] = 0
print(f"Bar dict: {foo.bar.bar_dict}")
# prints: Bar dict: {'a': 1, 'b': 2}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.