Skip to content

@attr.define fails to auto-detect __eq__ #670

Closed

Description

@rouge8

@attr.define is failing to auto-detect __eq__. My understanding from the docs is that this should work, but ValueError is not being raised unless I use @attr.define(eq=False).

import attr

@attr.define
class Treatment:
    treatment: str

    def __eq__(self, other):
        raise ValueError


assert Treatment("foo") == "foo"
$ pytest t.py
================================ test session starts ================================
platform darwin -- Python 3.8.5, pytest-6.0.1, py-1.8.2, pluggy-0.13.1
rootdir: /Users/andy/tmp
plugins: Faker-4.1.1
collected 0 items / 1 error                                                         

====================================== ERRORS =======================================
_______________________________ ERROR collecting t.py _______________________________
t.py:11: in <module>
    assert Treatment("foo") == "foo"
E   AssertionError: assert Treatment(treatment='foo') == 'foo'
E    +  where Treatment(treatment='foo') = <class 't.Treatment'>('foo')
============================== short test summary info ==============================
ERROR t.py - AssertionError: assert Treatment(treatment='foo') == 'foo'
!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!

Activity

  1. added a commit that references this issue on Aug 21, 2020
    62a6fe0
  2. hynek commented on Aug 21, 2020

    @hynek
    Member

    Ugh yeah that's unfortunate. Thanks for quick reporting! You should be able to work around it by setting @attr.define(eq=None) for now.

  3. added a commit that references this issue on Aug 21, 2020
    358c85e
  4. rouge8 commented on Aug 21, 2020

    @rouge8
    ContributorAuthor

    Yeah, I worked around it by setting @attr.define(eq=False) for the classes that need it. Thanks for the quick fix and loving the new APIs so far! As soon as I saw the new release I started purging @attr.s(auto_attribs=True) and attr.dataclass from our codebase. 😁

  5. hynek commented on Aug 21, 2020

    @hynek
    Member

    That's the hope! 😅 Now let's hope the mypy gods are gracious to us. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions