ipython / ipython/traitlets

copy(HasTraits) creates a clone with shared state from the original

未关闭
#481 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
653
派生
217
平均合并
2 天 21 小时
30 天内合并 PR
2

描述

Actually the results of using `copy()` on a `Hastrait` with [borged](https://www.google.it/search?q=python+borg) trait values:

```python
from copy import copy

class C(HasTraits):
a = Int()

c = C(a=1)
copy(c).a = 2
assert c.a == 1 # This FAILS because it is also 2!
```

## Suggested solution
I guess we need this line:
```python
d['_trait_values'] = self._trait_values.copy()
```
in `HasTraits.__getstate__()`:
https://github.com/ipython/traitlets/blob/3164ffb43167e9d1688046fbe0aefa424af5b58d/traitlets/traitlets.py#L1084-L1091

And i'm wondering whether the lock must no be shared, either:
```python
d['_cross_validation_lock'] = False
```

贡献指南

打开贡献指南

调研方向

Start in traitlets/traitlets.py at HasTraits.__getstate__, the location linked in the issue, and reproduce the provided copy() example. Check the suggested handling of _trait_values and _cross_validation_lock, then verify that changing the clone no longer changes the original and that the lock state is appropriate.

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。