Skip to content

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

@h3ndrk

Description

@h3ndrk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions