Avoid specialized assert formatting when we detect that `__eq__` is overridden
See original GitHub issueThe issue reported for introspecting numpy arrays #5347, is still present for dataclasses when invoking assertrepr_compare
.
When testing:
from dataclasses import dataclass
import numpy as np
@dataclass
class A:
a: np.ndarray
def __eq__(self, other):
return np.array_equal(self.a, other.a)
def test_fails():
assert A(np.array([1,2])) == A(np.array([2,1]))
Instead of an “Drill down” explanation you get:
def test_fails():
> assert A(np.array([1,2])) == A(np.array([2,1]))
E assert A(a=array([1, 2])) == A(a=array([2, 1]))
E (pytest_assertion plugin: representation of details failed: .../_pytest/assertion/util.py:430: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
E Probably an object has a faulty __repr__.)
(Ubuntu 21.10, pytest 6.25, python 3.9)
One way to solve it: since pytest only sees difference on repr
perhaps instead of comparing arr1 == arr2
when determining if the fields are different, perhaps it should consider whether repr(arr1) == repr(arr2)
which sidesteps the issue with array not returning bool for comparison.
one could do
for field in fields_to_check:
if repr(getattr(left, field)) == repr(getattr(right, field)):
same.append(field)
else:
diff.append(field)
If the __repr__
s are the same and the object are not the same (or vice versa), then the explanation shown by pytest will regardless be misleading.
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
Accessing @attr.s() decorator parameter values · Issue #602 ...
In #python today someone wanted to find out if their class was frozen. ... Avoid specialized assert formatting when we detect that __eq__...
Read more >How do you assert that a certain exception is thrown in JUnit ...
It depends on the JUnit version and what assert libraries you use. For JUnit5 and 4.13 see answer; If you use AssertJ or...
Read more >assert - cppreference.com
A portable way to include one is to use a comma operator provided it has not been overloaded, or use && with a...
Read more >A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
In this article, we'll cover multiple mock interfaces, listening invocations, matchers, and argument captors, and see firsthand how Mockito makes your tests ...
Read more >Changelog — pytest documentation
#9362: pytest now avoids specialized assert formatting when it is detected that the default __eq__ is overridden in attrs or dataclasses .
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RonnyPfannschmidt that would create a bug in the other direction in the default case (since
dataclass
is just going to use==
and if we used numpy’s comparison then it would be different)JFTR attr.s doesn’t ignore a custom eq if auto_detect=True. In attr.define it’s on by default.
I‘m planning on adding an
__attrs_effective_params__
(https://github.com/python-attrs/attrs/issues/602) but didn’t get around to it yet. 😦