Skip to content

Commit

Permalink
fix flake8 errors with new flake8 package
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Aug 8, 2023
2 parents 6abe465 + 822fe76 commit 92fc181
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
37 changes: 20 additions & 17 deletions hsds/util/arrayUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,27 @@ def fillVlenArray(rank, data, arr, index):
data_json,
] # listify

if isVlen(data_dtype):
arr = np.zeros((npoints,), dtype=data_dtype)
fillVlenArray(np_shape_rank, data_json, arr, 0)
if not (None in data_json):
if isVlen(data_dtype):
arr = np.zeros((npoints,), dtype=data_dtype)
fillVlenArray(np_shape_rank, data_json, arr, 0)
else:
try:
arr = np.array(data_json, dtype=data_dtype)
except UnicodeEncodeError as ude:
msg = "Unable to encode data"
raise ValueError(msg) from ude
# raise an exception of the array shape doesn't match the selection shape
# allow if the array is a scalar and the selection shape is one element,
# numpy is ok with this
if arr.size != npoints:
msg = "Input data doesn't match selection number of elements"
msg += f" Expected {npoints}, but received: {arr.size}"
raise ValueError(msg)
if arr.shape != data_shape:
arr = arr.reshape(data_shape) # reshape to match selection
else:
try:
arr = np.array(data_json, dtype=data_dtype)
except UnicodeEncodeError as ude:
msg = "Unable to encode data"
raise ValueError(msg) from ude
# raise an exception of the array shape doesn't match the selection shape
# allow if the array is a scalar and the selection shape is one element,
# numpy is ok with this
if arr.size != npoints:
msg = "Input data doesn't match selection number of elements"
msg += f" Expected {npoints}, but received: {arr.size}"
raise ValueError(msg)
if arr.shape != data_shape:
arr = arr.reshape(data_shape) # reshape to match selection
arr = np.array([]).astype(data_dtype)

return arr

Expand Down
6 changes: 3 additions & 3 deletions hsds/util/hdf5dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def getNumpyTypename(hdf5TypeName, typeClass=None):

def createBaseDataType(typeItem):
dtRet = None
if type(typeItem) == str:
if isinstance(typeItem, str):
# should be one of the predefined types
dtName = getNumpyTypename(typeItem)
dtRet = np.dtype(dtName)
Expand Down Expand Up @@ -717,7 +717,7 @@ def createDataType(typeItem):
dtRet = np.dtype(dtName)
return dtRet # return predefined type

if type(typeItem) != dict:
if not isinstance(typeItem, dict):
raise TypeError("invalid type")

if "class" not in typeItem:
Expand All @@ -735,7 +735,7 @@ def createDataType(typeItem):
subtypes = []
for field in fields:

if type(field) != dict:
if not isinstance(field, dict):
raise TypeError("Expected dictionary type for field")
if "name" not in field:
raise KeyError("'name' missing from field")
Expand Down
2 changes: 1 addition & 1 deletion tests/integ/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def getRangeGetEndpoint():

def validateId(id):
"""Return true if the parameter looks like a UUID"""
if type(id) != str:
if not isinstance(id, str):
# should be a string
return False
if len(id) != 38:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/array_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,20 @@ def testGetNumpyValue(self):
self.assertTrue(False)
except ValueError:
pass # expected

def testJsonToArrayOnNoneArray(self):
data_dtype = np.dtype("i4")
data_shape = [0, ]
data_json = [None]
arr = None

try:
arr = jsonToArray(data_shape, data_dtype, data_json)
except Exception as e:
print(f"Exception while testing jsonToArray on array with None elements: {e}")

self.assertTrue(len(arr) == 0)
self.assertTrue(arr.dtype == data_dtype)


if __name__ == "__main__":
Expand Down

0 comments on commit 92fc181

Please sign in to comment.