Trait initializers don't work consistent when copying with deepcopy
- Dominant language
- Python
- Stars
- 462
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
Don't know if this a bug, but at least i stumbled upon it:
According to documentation using "traits_init", overriding "__init__" or using dynamic initialization with "_name_default" should all lead to the same behavior.
Nevertheless using "traits_init" yields different results when copying objects using this method.
```
import copy
from traits.api import *
class Bla(HasTraits):
blubb = Int(0)
class BaseModel(HasTraits):
bla: Bla = Instance(Bla)
class DunderInitModel(BaseModel):
def __init__(self, **traits):
super().__init__(**traits)
self.bla = Bla()
class TraitInitModel(BaseModel):
def traits_init(self):
self.bla = Bla()
if __name__ == '__main__':
m1 = DunderInitModel()
m1.bla.blubb = 999
print(f"Model1.bla.blubb (__init__): {copy.deepcopy(m1).bla.blubb}")
m2 = TraitInitModel()
m2.bla.blubb = 999
print(f"Model2.bla.blubb (traits_init): {copy.deepcopy(m2).bla.blubb}")
```
prints out:
```
Model1.bla.blubb (__init__): 999
Model2.bla.blubb (traits_init): 0
```
Those two operations should get the same result, shouldn't they?
I can see that inside *traits.has_traits.HasTraits.clone_traits*, *traits_init* is executed *after* copy_traits, which overwrites the copied traits.
Using Win10, traits version 6.3.2, Python 3.8
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by running the Python reproduction in the issue with Traits 6.3.2 and compare the deepcopy results for DunderInitModel and TraitInitModel. Then inspect traits.has_traits.HasTraits.clone_traits, where the issue reports that traits_init runs after copy_traits. Done means both initialization approaches preserve the copied blubb value consistently, with a regression test for the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100