Skip to content

Allow overriding of protocol methods in subclasses #127

Closed
@Sinclert

Description

@Sinclert

Hi there 👋🏻

I opened this issue to discuss one particular use case we, at MadMiner, are having trouble with.

We have a custom vector class, MadMinerParticle, overriding some of the protocol methods coming from its parent class MomentumVector4D (which, in reality, inherits them from VectorObject). When using one of these methods (our trouble is with __iadd__), we noticed that the operation makes use of the __array_ufunc__ method, which calls the _handler_of dispatch function, obtaining the module in which the provided object class was defined.

vector/src/vector/_methods.py

Lines 2547 to 2553 in d9cd956

if isinstance(obj, Vector):
if handler is None:
handler = obj
elif _handler_priority.index(
type(obj).__module__
) > _handler_priority.index(type(handler).__module__):
handler = obj

The problem is that, with a custom VectorObject subclass, the module information points to a Python module that is on the client code, and therefore, not recognized among the valid list of modules.

vector/src/vector/_methods.py

Lines 2529 to 2533 in d9cd956

_handler_priority = [
"vector._backends.object_",
"vector._backends.numpy_",
"vector._backends.awkward_",
]


Could you expand the logic on the _handler_of dispatch function so that, not only the object's type __module__ gets looked up, but the complete list of parent classes in MRO order?

Thanks in advance.

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