traitlets
traitlets copied to clipboard
Issue with default_value for List trait
I can't seem to set default_value of List trait to be None. When I try to set it the using the default_value argument in the constructor, the value is set to []. After initialization I can set it to None. I am using traitlets 4.3.1. It works when the trait is an Instance of List.
from traitlets import List, Instance
from ipywidgets import DOMWidget
class Test(DOMWidget):
arr = Instance(List, default_value=None, allow_none=True).tag(sync=True)
arr_2 = List(default_value=None, allow_none=True).tag(sync=True)
test_obj = Test()
print(test_obj.arr is None)
print(test_obj.arr_2 is None)
print(test_obj.arr_2)
Output: True False []
Which version of traitlets?
traitlets 4.3.1
I don't know if this is related, but I am having issues with the default_value on Instance. This is using the current master .
class Foo(traitlets.HasTraits):
bar = traitlets.Instance('jinja2.Environment', default_value=jinja2.Environment())
print(type(Foo.bar.default()))
print(hasattr(Foo.bar, 'make_dynamic_default'))
print(getattr(Foo.bar, 'make_dynamic_default')())
gives
<class 'NoneType'>
True
None
It might be that make_dynamic_defaults should be checked for None.
@SylvainCorlay I am reasonably certain that the original error results here on master, but was also present here in the 4.0 release. This seems like a bug, but the quick fixes I tried, seem to raise errors elsewhere in tests - thus making changes there could potentially cause downstream problems.
@tonyfast #367 fixes the bug you found which appears to be unrelated in function to the original.
Yes, the original container class has a convoluted logic. I wanted to change this when we did the traitlets refactoring but we had to stop at some point.
Yes, the original container class has a convoluted logic. I wanted to change this when we did the traitlets refactoring but we had to stop at some point.
@SylvainCorlay are you saying this is a change you want to make, or a change that broke too many things for you to go forward with it?