traitlets icon indicating copy to clipboard operation
traitlets copied to clipboard

Issue with default_value for List trait

Open ssunkara1 opened this issue 8 years ago • 7 comments

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 []

ssunkara1 avatar Jan 27 '17 15:01 ssunkara1

Which version of traitlets?

SylvainCorlay avatar Jan 28 '17 11:01 SylvainCorlay

traitlets 4.3.1

ssunkara1 avatar Jan 28 '17 16:01 ssunkara1

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.

tonyfast avatar Jan 29 '17 02:01 tonyfast

@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.

rmorshea avatar Jan 30 '17 04:01 rmorshea

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 avatar Jan 30 '17 14:01 SylvainCorlay

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 avatar Jan 30 '17 14:01 SylvainCorlay

@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?

rmorshea avatar Feb 03 '17 22:02 rmorshea