ipython / ipython/traitlets

changing default value of containers to None

Đang mở
#455 5 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
653
Fork
217
Merge trung bình
2 ngày 21 giờ
Pull request đã merge (30 ngày)
2

Mô tả

Hi,

I wanted to create my own trait types, so all have the same `default_value`, e.g. `None`, and call `tag` automatically, but I'm having problems with `default_value` for containers. I will use `List` as an example, I'm defining a new class:

```
class List(traitlets.List):
allow_none = True
default_value = None
info_text = "a list with default_value = None"

def __init__(self, trait=None, default_value=None, minlen=0, maxlen=sys.maxsize, kw=None,
read_only=None, help=None, config=None, **kwargs):
super(List, self).__init__(trait=trait, default_value=default_value, minlen=0, maxlen=maxlen,
kw=kw, read_only=read_only, help=help, config=config)
self.tag(**kwargs)
```

and I'd like my new class to pass those test:

```
def test_list_default_1():
class HasTr(traitlets.HasTraits):
foo = List()
ht = HasTr()
assert ht.foo is None

def test_list_default_2():
class HasTr(traitlets.HasTraits):
foo = List(default_value=[1])
ht = HasTr()
assert ht.foo == [1]
```

I'm passing the first test, but not the second (in both cases `ht.foo is None`). From quick reading of your code, it looks like the method `make_dynamic_default` is not called when I define my `List` class with class attribute `default_value = None`. Is this on purpose? Do you have any other suggestions how I can write my class (for all traits) that I'm able to pass both tests?

**system Info**
{'commit_hash': 'd86648c5d',
'commit_source': 'installation',
'default_encoding': 'UTF-8',
'ipython_path': '/Users/dorota/anaconda/envs/py35_trlets/lib/python3.5/site-packages/IPython',
'ipython_version': '6.1.0',
'os_name': 'posix',
'platform': 'Darwin-16.1.0-x86_64-i386-64bit',
'sys_executable': '/Users/dorota/anaconda/envs/py35_trlets/bin/python',
'sys_platform': 'darwin',
'sys_version': '3.5.3 | packaged by conda-forge | (default, May 12 2017, '
'15:35:12) \n'
'[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]'}

I'm using the current master.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.