Skip to content

override generated __eq__ to work with numpy fields #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chobeat opened this issue Jul 17, 2018 · 4 comments
Closed

override generated __eq__ to work with numpy fields #409

chobeat opened this issue Jul 17, 2018 · 4 comments

Comments

@chobeat
Copy link
Contributor

chobeat commented Jul 17, 2018

I would like to know how to override generated methods in attrs. From the documentation is not clear.

My problem is that if I put some numpy arrays as values, the regular "==" operator fails because np.array_equal should be used instead.

The error is the following:

    def __eq__(self, other):
        if other.__class__ is not self.__class__:
            return NotImplemented
        return  (
            self.raw,
            self.processed,
        ) == (
            other.raw,
>           other.processed,
        )
E       ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
@hynek
Copy link
Member

hynek commented Jul 20, 2018

I’m afraid you’ll have to disable attrs’ eq and write it yourself. This is a super edgy edge case so I don’t think it makes a lot of sense to add special hooks for it. Are you aware of the reason why numpy arrays don't allow for comparison using ==?

@chobeat
Copy link
Contributor Author

chobeat commented Jul 21, 2018

Yes, there's a very explicit error message if you try do it and it makes sense: for the way they make use of python syntax, a == would be ambigous if used to compare 2 arrays, so they offer numpy.array_equal.

I’m afraid you’ll have to disable attrs’ eq and write it yourself.

Can you explain me how? Just writing a eq method in my class doesn't work because I believe attrs eq is generated afterward.

@hynek
Copy link
Member

hynek commented Jul 21, 2018

Your approach might work in the future (see #324) until then you'll have to pass @attr.s(cmp=False).

@chobeat
Copy link
Contributor Author

chobeat commented Jul 21, 2018

Oh, perfect, thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants