Skip to content

Commit c58b0fb

Browse files
committed
Use ATTRS_EQ_FIELD for attrs 19.2 compat
1 parent a79acf2 commit c58b0fb

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

src/_pytest/assertion/util.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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
11+
from _pytest.compat import ATTRS_EQ_FIELD
1212

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

115115

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-
128116
def isiterable(obj):
129117
try:
130118
iter(obj)
@@ -388,7 +376,9 @@ def _compare_eq_cls(left, right, verbose, type_fns):
388376
fields_to_check = [field for field, info in all_fields.items() if info.compare]
389377
elif isattrs(left):
390378
all_fields = left.__attrs_attrs__
391-
fields_to_check = [field.name for field in all_fields if attrsfieldhaseq(field)]
379+
fields_to_check = [
380+
field.name for field in all_fields if getattr(field, ATTRS_EQ_FIELD)
381+
]
392382

393383
same = []
394384
diff = []

src/_pytest/compat.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ def overload(f): # noqa: F811
356356
return f
357357

358358

359-
attrs_has_eq = getattr(attr, "__version_info__", (0, 0)) >= (19, 2)
360-
if attrs_has_eq:
361-
attrs_no_eq = {"eq": False}
359+
if getattr(attr, "__version_info__", ()) >= (19, 2):
360+
ATTRS_EQ_FIELD = "eq"
362361
else:
363-
attrs_no_eq = {"cmp": False}
362+
ATTRS_EQ_FIELD = "cmp"

src/_pytest/mark/structures.py

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

1010
from ..compat import ascii_escaped
11-
from ..compat import attrs_no_eq
11+
from ..compat import ATTRS_EQ_FIELD
1212
from ..compat import getfslineno
1313
from ..compat import NOTSET
1414
from _pytest.outcomes import fail
@@ -368,7 +368,8 @@ def __repr__(self):
368368
return "<NodeKeywords for node {}>".format(self.node)
369369

370370

371-
@attr.s(hash=False, **attrs_no_eq) # type: ignore
371+
# mypy cannot find this overload, remove when on attrs>=19.2
372+
@attr.s(hash=False, **{ATTRS_EQ_FIELD: False}) # type: ignore
372373
class NodeMarkers:
373374
"""
374375
internal structure for storing marks belonging to a node

testing/test_assertion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +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
12+
from _pytest.compat import ATTRS_EQ_FIELD
1313

1414

1515
def mock_config():
@@ -688,7 +688,7 @@ def test_attrs_with_attribute_comparison_off(self):
688688
@attr.s
689689
class SimpleDataObject:
690690
field_a = attr.ib()
691-
field_b = attr.ib(**attrs_no_eq)
691+
field_b = attr.ib(**{ATTRS_EQ_FIELD: False})
692692

693693
left = SimpleDataObject(1, "b")
694694
right = SimpleDataObject(1, "b")

0 commit comments

Comments
 (0)