Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions numtraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@
class NumericalTrait(TraitType):
info_text = 'a numerical trait, either a scalar or a vector'
def __init__(self, ndim=None, shape=None, domain=None,
default=None, convertible_to=None):
super(NumericalTrait, self).__init__()
default=None, convertible_to=None, nullable=False):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest accepts_none instead of nullable - what do you think?

# We use the default value for the traitlet in the following cases:
# - default is not None,
# - default is None and the trait is nullable.
# In the other cases, we assume that a None default means that
# there is no default value defined.
if (default is None and nullable) or default is not None:
super(NumericalTrait, self).__init__(default_value=default)
else:
super(NumericalTrait, self).__init__()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the default_value argument default to? Can you have just one super call and adapt the value of default appropriately?


# Just store all the construction arguments.
self.ndim = ndim
self.shape = shape
self.domain = domain
# TODO: traitlets supports a `default` argument in __init__(), we should
# probably link them together once we start using this.
self.default = default
self.nullable = nullable
self.target_unit = convertible_to

if self.target_unit is not None:
Expand All @@ -65,6 +72,10 @@ def _check_args(self):
raise TraitError("shape={0} and ndim={1} are inconsistent".format(self.shape, self.ndim))

def validate(self, obj, value):
# If the trait is nullable and the value is None,
# then we have nothing to check.
if self.nullable and value is None:
return value

# We proceed by checking whether Numpy tells us the value is a
# scalar. If Numpy isscalar returns False, it could still be scalar
Expand Down
17 changes: 17 additions & 0 deletions test_numtraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ScalarProperties(HasTraits):
d = NumericalTrait(ndim=0, domain='negative')
e = NumericalTrait(ndim=0, domain='strictly-negative')
f = NumericalTrait(ndim=0, domain=(3, 4))
g = NumericalTrait(ndim=0, nullable=True)
h = NumericalTrait(ndim=0, nullable=True, default=1.23)
i = NumericalTrait(ndim=0, default=4.56)

class TestScalar(object):

Expand Down Expand Up @@ -79,6 +82,20 @@ def test_range(self):
self.sp.f = 7
assert exc.value.args[0] == "f should be in the range [3:4]"

def test_nullable_default(self):
assert self.sp.g is None
assert self.sp.h == 1.23
assert self.sp.i == 4.56
self.sp.g = 1.2
assert self.sp.g == 1.2
self.sp.h = None
assert self.sp.h is None
self.sp.i = 1.23
assert self.sp.i == 1.23
with pytest.raises(TraitError) as exc:
self.sp.i = None
assert exc.value.args[0] == "i should be a scalar value"


class ArrayProperties(HasTraits):

Expand Down