-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
I'm generating a Python API from the following schema:
struct Uuid {
id: [ubyte: 16];
}
This generates a Python object that contains a _UnPack() method which generates a Python list or a NumPy ndarray depending on whether NumPy can be imported or not (see https://github.com/google/flatbuffers/blob/master/python/flatbuffers/compat.py):
# UuidT
def _UnPack(self, uuid):
if uuid is None:
return
if not uuid.IdIsNone():
if np is None:
self.id = []
for i in range(uuid.IdLength()):
self.id.append(uuid.Id(i))
else:
self.id = uuid.IdAsNumpy()In my project, I'm also using --gen-compare which generates the following equality operator:
def __eq__(self, other):
return type(self) == type(other) and \
self.id == other.idThis __eq__() operator works for list but not for ndarray. For list this yields whether the list items are the same (what we want). For ndarray this will do a component-wise comparison and yields a new ndarray containing boolean values but not a single boolean value that would be required for the __eq__()'s return value. In the generated operator, the boolean ndarray gets converted to a single boolean value resulting in the exception ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
A possible fix would be to add .any() or similar if NumPy is detected.
As a quickfix, I patched the Python library for my project to always return None from import_numpy(). But that is not what we want upstream probably.