ipython / ipython/traitlets

Traitlets validates default even if overrided

Đang mở
#548 3 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ả

To make a required trait, I thought I could use:

```
from traitlets import HasTraits, Unicode

class Foo(HasTraits):
bar = Unicode(default_value=None, allow_none=False, required=True)
```

As expected, this raises a trait error, if Foo is instantiated without arguments:

```
f = Foo()
```

But: this also raises, when bar is provided:

```
f = Foo(bar='test')
```

This error is raised:
```
TraitError Traceback (most recent call last)
in
----> 1 Foo(bar='test')

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in __new__(cls, *args, **kwargs)
956 else:
957 inst = new_meth(cls, *args, **kwargs)
--> 958 inst.setup_instance(*args, **kwargs)
959 return inst
960

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
984 self._trait_notifiers = {}
985 self._trait_validators = {}
--> 986 super(HasTraits, self).setup_instance(*args, **kwargs)
987
988 def __init__(self, *args, **kwargs):

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
975 else:
976 if isinstance(value, BaseDescriptor):
--> 977 value.instance_init(self)
978
979

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in instance_init(self, obj)
520 if (self._dynamic_default_callable(obj) is None) \
521 and (self.default_value is not Undefined):
--> 522 v = self._validate(obj, self.default_value)
523 if self.name is not None:
524 obj._trait_values[self.name] = v

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in _validate(self, obj, value)
589 return value
590 if hasattr(self, 'validate'):
--> 591 value = self.validate(obj, value)
592 if obj._cross_validation_lock is False:
593 value = self._cross_validate(obj, value)

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in validate(self, obj, value)
2052 msg = "Could not decode {!r} for unicode trait '{}' of {} instance."
2053 raise TraitError(msg.format(value, self.name, class_of(obj)))
-> 2054 self.error(obj, value)
2055
2056

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in error(self, obj, value)
623 e = "The '%s' trait must be %s, but a value of %r was specified." \
624 % (self.name, self.info(), repr_type(value))
--> 625 raise TraitError(e)
626
627 def get_metadata(self, key, default=None):

TraitError: The 'bar' trait of a Foo instance must be a unicode string, but a value of None was specified.
```

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.