Skip to content

Commit

Permalink
Ignore inherited field when comparing Attributes (#684)
Browse files Browse the repository at this point in the history
* Ignore inherited field when comparing Attributes

fixes #682

* add newsfragment
  • Loading branch information
hynek committed Sep 3, 2020
1 parent dfb2ee2 commit bfd7bb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/684.change.rst
@@ -0,0 +1 @@
The ``inherited`` field of ``attr.Attribute`` (introduced in 20.1.0) instances is not considered when hashing and comparing anymore.
9 changes: 7 additions & 2 deletions src/attr/_make.py
Expand Up @@ -2185,6 +2185,8 @@ class Attribute(object):
.. versionadded:: 20.1.0 *inherited*
.. versionadded:: 20.1.0 *on_setattr*
.. versionchanged:: 20.2.0 *inherited* is not taken into account for
equality checks and hashing anymore.
For the full version history of the fields, see `attr.ib`.
"""
Expand Down Expand Up @@ -2354,8 +2356,11 @@ def _setattrs(self, name_values_pairs):
]

Attribute = _add_hash(
_add_eq(_add_repr(Attribute, attrs=_a), attrs=_a),
attrs=[a for a in _a if a.hash],
_add_eq(
_add_repr(Attribute, attrs=_a),
attrs=[a for a in _a if a.name != "inherited"],
),
attrs=[a for a in _a if a.hash and a.name != "inherited"],
)


Expand Down
20 changes: 20 additions & 0 deletions tests/test_make.py
Expand Up @@ -691,6 +691,26 @@ def test_sugar_callable(self):
class C(object):
x = attr.ib(factory=Factory(list))

def test_inherited_does_not_affect_hashing_and_equality(self):
"""
Whether or not an Attribute has been inherited doesn't affect how it's
hashed and compared.
"""

@attr.s
class BaseClass(object):
x = attr.ib()

@attr.s
class SubClass(BaseClass):
pass

ba = attr.fields(BaseClass)[0]
sa = attr.fields(SubClass)[0]

assert ba == sa
assert hash(ba) == hash(sa)


@pytest.mark.skipif(PY2, reason="keyword-only arguments are PY3-only.")
class TestKeywordOnlyAttributes(object):
Expand Down

0 comments on commit bfd7bb4

Please sign in to comment.