Ignore inherited field when comparing Attributes (#684)
* Ignore inherited field when comparing Attributes fixes #682 * add newsfragment
This commit is contained in:
parent
dfb2ee284d
commit
bfd7bb49b4
|
@ -0,0 +1 @@
|
||||||
|
The ``inherited`` field of ``attr.Attribute`` (introduced in 20.1.0) instances is not considered when hashing and comparing anymore.
|
|
@ -2185,6 +2185,8 @@ class Attribute(object):
|
||||||
|
|
||||||
.. versionadded:: 20.1.0 *inherited*
|
.. versionadded:: 20.1.0 *inherited*
|
||||||
.. versionadded:: 20.1.0 *on_setattr*
|
.. 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`.
|
For the full version history of the fields, see `attr.ib`.
|
||||||
"""
|
"""
|
||||||
|
@ -2354,8 +2356,11 @@ _a = [
|
||||||
]
|
]
|
||||||
|
|
||||||
Attribute = _add_hash(
|
Attribute = _add_hash(
|
||||||
_add_eq(_add_repr(Attribute, attrs=_a), attrs=_a),
|
_add_eq(
|
||||||
attrs=[a for a in _a if a.hash],
|
_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"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -691,6 +691,26 @@ class TestAttributes(object):
|
||||||
class C(object):
|
class C(object):
|
||||||
x = attr.ib(factory=Factory(list))
|
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.")
|
@pytest.mark.skipif(PY2, reason="keyword-only arguments are PY3-only.")
|
||||||
class TestKeywordOnlyAttributes(object):
|
class TestKeywordOnlyAttributes(object):
|
||||||
|
|
Loading…
Reference in New Issue