Traits default handler method called before regular traits are initialised
- Dominant language
- Python
- Stars
- 462
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
The below Traits default getter is called before regular traits are initialised.
```python
import traits
from traits.api import HasStrictTraits, Instance, Int, Property
class Setting(HasStrictTraits):
sometrait = Int(37)
class SomeClass(HasStrictTraits):
service = Int()
setting = Instance(Setting)
def _setting_default(self):
# Problem: this is executed at object instantiation time, not lazily.
# So "service" isn't initialised at this point.
print("In _setting_default")
print("self.service is:", self.service)
return Setting()
keyword_list = Property(Int, depends_on=["setting:sometrait"])
def _get_keyword_list(self):
return 31415
print(traits.__version__)
model = SomeClass(service=53)
```
This results in an _access_ to the `service` trait, which then forces the trait to have the _default_ value (even though a value is supplied on instantiation of `SomeClass`). I would expect `service` to have a value of either `Undefined` or `53` during its lifetime.
```
5.1.1
In _setting_default
self.service is: 0
```
This doesn't happen if I change `depends_on=['setting:sometrait']` to `depends_on=['setting']`:
```
5.1.1
```
---
A similar thing happens when the magic default method is replaced by a Traits Property getter.
The below code raises a `ValueError`.
Note, that the extended trait listener is still needed for reproducing the error.
```python
import traits
from traits.api import HasStrictTraits, Instance, Int, Property
class MyInt(int):
pass
class MyClass(HasStrictTraits):
regular = Instance(MyInt)
primary = Property(Int(), depends_on=['regular'])
def _get_primary(self):
if self.regular is None:
raise ValueError("I'd not like to handle Nones")
secondary = Property(Int(), depends_on=['primary:+'])
def _get_secondary(self):
pass
print(traits.__version__)
MyClass(regular=MyInt())
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.