google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.12k stars 3.23k forks source link

[Python][v23.1.21] Comparison operators of lists are broken if NumPy is available #7890

Open h3ndrk opened 1 year ago

h3ndrk commented 1 year ago

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.id

This __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.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

h3ndrk commented 11 months ago

Probably still broken...

github-actions[bot] commented 5 months ago

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

h3ndrk commented 5 months ago

Probably still broken...