Skip to content

Commit a79acf2

Browse files
committed
Fix warnings with attrs 19.2 and fix object assertions
attrs 19.2 deprecated cmp in favor of the dataclass-ish eq/order duo. This causes deprecation warnings that in turn break some of the cool new deep object comparisons. Since we at attrs expected this to be a problem, it shipped with helpers to write backward and forward compatible code. This PR uses that and avoids changed to minimal versions.
1 parent 9a4c0b9 commit a79acf2

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/_pytest/assertion/util.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import _pytest._code
99
from _pytest import outcomes
1010
from _pytest._io.saferepr import saferepr
11+
from _pytest.compat import attrs_has_eq
1112

1213
# The _reprcompare attribute on the util module is used by the new assertion
1314
# interpretation code and assertion rewriter to detect this plugin was
@@ -112,6 +113,18 @@ def isattrs(obj):
112113
return getattr(obj, "__attrs_attrs__", None) is not None
113114

114115

116+
if attrs_has_eq:
117+
118+
def attrsfieldhaseq(a):
119+
return a.eq
120+
121+
122+
else:
123+
124+
def attrsfieldhaseq(a):
125+
return a.cmp
126+
127+
115128
def isiterable(obj):
116129
try:
117130
iter(obj)
@@ -375,7 +388,7 @@ def _compare_eq_cls(left, right, verbose, type_fns):
375388
fields_to_check = [field for field, info in all_fields.items() if info.compare]
376389
elif isattrs(left):
377390
all_fields = left.__attrs_attrs__
378-
fields_to_check = [field.name for field in all_fields if field.cmp]
391+
fields_to_check = [field.name for field in all_fields if attrsfieldhaseq(field)]
379392

380393
same = []
381394
diff = []

src/_pytest/compat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,10 @@ def funcargnames(self):
354354

355355
def overload(f): # noqa: F811
356356
return f
357+
358+
359+
attrs_has_eq = getattr(attr, "__version_info__", (0, 0)) >= (19, 2)
360+
if attrs_has_eq:
361+
attrs_no_eq = {"eq": False}
362+
else:
363+
attrs_no_eq = {"cmp": False}

src/_pytest/mark/structures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import attr
99

1010
from ..compat import ascii_escaped
11+
from ..compat import attrs_no_eq
1112
from ..compat import getfslineno
1213
from ..compat import NOTSET
1314
from _pytest.outcomes import fail
@@ -367,7 +368,7 @@ def __repr__(self):
367368
return "<NodeKeywords for node {}>".format(self.node)
368369

369370

370-
@attr.s(cmp=False, hash=False)
371+
@attr.s(hash=False, **attrs_no_eq) # type: ignore
371372
class NodeMarkers:
372373
"""
373374
internal structure for storing marks belonging to a node

testing/test_assertion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from _pytest import outcomes
1010
from _pytest.assertion import truncate
1111
from _pytest.assertion import util
12+
from _pytest.compat import attrs_no_eq
1213

1314

1415
def mock_config():
@@ -687,7 +688,7 @@ def test_attrs_with_attribute_comparison_off(self):
687688
@attr.s
688689
class SimpleDataObject:
689690
field_a = attr.ib()
690-
field_b = attr.ib(cmp=False)
691+
field_b = attr.ib(**attrs_no_eq)
691692

692693
left = SimpleDataObject(1, "b")
693694
right = SimpleDataObject(1, "b")

0 commit comments

Comments
 (0)