From c03e46f1ad99e8b01a71c6ad43acfc6240670051 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 6 Oct 2019 08:47:17 -0300 Subject: [PATCH] Fix warnings with attrs 19.2 and fix object assertions (#5902) Fix warnings with attrs 19.2 and fix object assertions --- src/_pytest/assertion/util.py | 5 ++++- src/_pytest/compat.py | 11 +++++++++-- src/_pytest/mark/structures.py | 4 +++- testing/test_assertion.py | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 1fee64ce08d..c382f1c6091 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -12,6 +12,7 @@ from ..compat import Sequence from _pytest import outcomes from _pytest._io.saferepr import saferepr +from _pytest.compat import ATTRS_EQ_FIELD # The _reprcompare attribute on the util module is used by the new assertion # interpretation code and assertion rewriter to detect this plugin was @@ -374,7 +375,9 @@ def _compare_eq_cls(left, right, verbose, type_fns): fields_to_check = [field for field, info in all_fields.items() if info.compare] elif isattrs(left): all_fields = left.__attrs_attrs__ - fields_to_check = [field.name for field in all_fields if field.cmp] + fields_to_check = [ + field.name for field in all_fields if getattr(field, ATTRS_EQ_FIELD) + ] same = [] diff = [] diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index df661bfff02..38531a9d48c 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -13,6 +13,7 @@ import sys from contextlib import contextmanager +import attr import py import six from six import text_type @@ -406,8 +407,8 @@ def _setup_collect_fakemodule(): pytest.collect = ModuleType("pytest.collect") pytest.collect.__all__ = [] # used for setns - for attr in COLLECT_FAKEMODULE_ATTRIBUTES: - setattr(pytest.collect, attr, getattr(pytest, attr)) + for attribute in COLLECT_FAKEMODULE_ATTRIBUTES: + setattr(pytest.collect, attribute, getattr(pytest, attribute)) if _PY2: @@ -455,3 +456,9 @@ def dec(fn): else: from functools import lru_cache # noqa: F401 + + +if getattr(attr, "__version_info__", ()) >= (19, 2): + ATTRS_EQ_FIELD = "eq" +else: + ATTRS_EQ_FIELD = "cmp" diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index bfbe71c2654..0ccd8141dd9 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -8,6 +8,7 @@ import six from ..compat import ascii_escaped +from ..compat import ATTRS_EQ_FIELD from ..compat import getfslineno from ..compat import MappingMixin from ..compat import NOTSET @@ -377,7 +378,8 @@ def __repr__(self): return "" % (self.node,) -@attr.s(cmp=False, hash=False) +# mypy cannot find this overload, remove when on attrs>=19.2 +@attr.s(hash=False, **{ATTRS_EQ_FIELD: False}) # type: ignore class NodeMarkers(object): """ internal structure for storing marks belonging to a node diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 2085ffd8b44..a94dcd701fa 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -14,6 +14,7 @@ from _pytest import outcomes from _pytest.assertion import truncate from _pytest.assertion import util +from _pytest.compat import ATTRS_EQ_FIELD PY3 = sys.version_info >= (3, 0) @@ -687,7 +688,7 @@ def test_attrs_with_attribute_comparison_off(self): @attr.s class SimpleDataObject(object): field_a = attr.ib() - field_b = attr.ib(cmp=False) + field_b = attr.ib(**{ATTRS_EQ_FIELD: False}) left = SimpleDataObject(1, "b") right = SimpleDataObject(1, "b")