Skip to content
Snippets Groups Projects
Commit c60993d7 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[baseformat] Fix handling of scalar value if it's considered zero by python

In this case, don't do the check as numpy.array will guess the
value as int64 whatever the original dtype might be. In this case
return 0 as the dtype requested.
parent fad34fe3
Branches
Tags
1 merge request!24Improve scalar zero handling
Pipeline #24041 passed
...@@ -95,10 +95,11 @@ def setup_scalar(formatname, attrname, dtype, value, casting, add_defaults): ...@@ -95,10 +95,11 @@ def setup_scalar(formatname, attrname, dtype, value, casting, add_defaults):
if value is None: # use the default for the type if value is None: # use the default for the type
return dtype.type() return dtype.type()
else: else:
if not numpy.can_cast(numpy.array(value).dtype, dtype, casting=casting): if value: # zero is classified as int64 which can't be safely casted to uint64
raise TypeError("cannot safely cast attribute `%s' on dataformat " \ if not numpy.can_cast(numpy.array(value).dtype, dtype, casting=casting):
"`%s' with type `%s' to `%s' without precision loss" % \ raise TypeError("cannot safely cast attribute `%s' on dataformat " \
(attrname, formatname, numpy.array(value).dtype, dtype)) "`%s' with type `%s' to `%s' without precision loss" % \
(attrname, formatname, numpy.array(value).dtype, dtype))
return dtype.type(value) return dtype.type(value)
elif issubclass(dtype, str): # it is a string elif issubclass(dtype, str): # it is a string
......
...@@ -111,6 +111,21 @@ def test_single_unsigned_integer64(): ...@@ -111,6 +111,21 @@ def test_single_unsigned_integer64():
#---------------------------------------------------------- #----------------------------------------------------------
def test_zero():
value = 0
np_types = [numpy.uint64, numpy.uint32, numpy.uint16, numpy.uint8,
numpy.int64, numpy.int32, numpy.int16, numpy.int8]
dataformats = [('user/single_integer/1', numpy.int32),
('user/single_unsigned_integer/1', numpy.uint32),
('user/single_integer64/1', numpy.int64),
('user/single_unsigned_integer64/1', numpy.uint64)]
for dataformat, return_type in dataformats:
for np_type in np_types:
obj = doit(dataformat, 'value', np_type(value))
assert numpy.equal(obj.value, value)
assert isinstance(obj.value, return_type)
#---------------------------------------------------------- #----------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment