Skip to content

Commit

Permalink
Fix default values that are not comparable with '==' (#586)
Browse files Browse the repository at this point in the history
Fix #585
  • Loading branch information
nicoddemus authored and hynek committed Oct 15, 2019
1 parent 3432df5 commit e2b02ec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/585.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``auto_attribs`` usage when default values cannot be compared directly with ``==``, such as ``numpy`` arrays.
2 changes: 1 addition & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def _patch_original_class(self):
for name in self._attr_names:
if (
name not in base_names
and getattr(cls, name, _sentinel) != _sentinel
and getattr(cls, name, _sentinel) is not _sentinel
):
try:
delattr(cls, name)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,18 @@ class C:

with pytest.raises(AttributeError):
C.y

def test_non_comparable_defaults(self):
"""
Regression test for #585: objects that are not directly comparable
(for example numpy arrays) would cause a crash when used as
default values of an attrs auto-attrib class.
"""

class NonComparable:
def __eq__(self, other):
raise ValueError

@attr.s(auto_attribs=True)
class C:
x: typing.Any = NonComparable()

0 comments on commit e2b02ec

Please sign in to comment.