Skip to content

Commit

Permalink
Drop the deprecated Attribute.cmp (#939)
Browse files Browse the repository at this point in the history
* Drop the deprecated Attribute.cmp

This has nothing to do with the cmp argument to attr.s/define which have been
undeprecated.

* Add newsfragment

* Add test for attr.ib(cmp=)
  • Loading branch information
hynek committed Mar 21, 2022
1 parent 980c8b0 commit c422bf8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 54 deletions.
2 changes: 2 additions & 0 deletions changelog.d/939.breaking.rst
@@ -0,0 +1,2 @@
The deprecated ``cmp`` attribute of ``attrs.Attribute`` has been removed.
This does not affect the *cmp* argument to ``attr.s`` that can be used as a shortcut to set *eq* and *order* at the same time.
17 changes: 0 additions & 17 deletions src/attr/_make.py
@@ -1,11 +1,9 @@
# SPDX-License-Identifier: MIT


import copy
import linecache
import sys
import typing
import warnings

from operator import itemgetter

Expand Down Expand Up @@ -1093,12 +1091,6 @@ def _add_method_dunders(self, method):
return method


_CMP_DEPRECATION = (
"The usage of `cmp` is deprecated and will be removed on or after "
"2021-06-01. Please use `eq` and `order` instead."
)


def _determine_attrs_eq_order(cmp, eq, order, default_eq):
"""
Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
Expand Down Expand Up @@ -2594,15 +2586,6 @@ def from_counting_attr(cls, name, ca, type=None):
**inst_dict
)

@property
def cmp(self):
"""
Simulate the presence of a cmp attribute and warn.
"""
warnings.warn(_CMP_DEPRECATION, DeprecationWarning, stacklevel=2)

return self.eq and self.order

# Don't use attr.evolve since fields(Attribute) doesn't work
def evolve(self, **changes):
"""
Expand Down
47 changes: 10 additions & 37 deletions tests/test_functional.py
Expand Up @@ -12,7 +12,7 @@

import pytest

from hypothesis import assume, given
from hypothesis import given
from hypothesis.strategies import booleans

import attr
Expand All @@ -21,8 +21,6 @@
from attr._make import NOTHING, Attribute
from attr.exceptions import FrozenInstanceError

from .strategies import optional_bool


@attr.s
class C1:
Expand Down Expand Up @@ -643,44 +641,19 @@ class C:

assert ei.value.args[0] in possible_errors

@given(cmp=optional_bool, eq=optional_bool, order=optional_bool)
def test_cmp_deprecated_attribute(self, cmp, eq, order):
@pytest.mark.parametrize("slots", [True, False])
@pytest.mark.parametrize("cmp", [True, False])
def test_attrib_cmp_shortcut(self, slots, cmp):
"""
Accessing Attribute.cmp raises a deprecation warning but returns True
if cmp is True, or eq and order are *both* effectively True.
Setting cmp on `attr.ib`s sets both eq and order.
"""
# These cases are invalid and raise a ValueError.
assume(cmp is None or (eq is None and order is None))
assume(not (eq is False and order is True))

if cmp is not None:
rv = cmp
elif eq is True or eq is None:
rv = order is None or order is True
elif cmp is None and eq is None and order is None:
rv = True
elif cmp is None or eq is None:
rv = False
else:
pytest.fail(
"Unexpected state: cmp=%r eq=%r order=%r" % (cmp, eq, order)
)

with pytest.deprecated_call() as dc:

@attr.s
class C:
x = attr.ib(cmp=cmp, eq=eq, order=order)

assert rv == attr.fields(C).x.cmp

(w,) = dc.list
@attr.s(slots=slots)
class C:
x = attr.ib(cmp=cmp)

assert (
"The usage of `cmp` is deprecated and will be removed on or after "
"2021-06-01. Please use `eq` and `order` instead."
== w.message.args[0]
)
assert cmp is attr.fields(C).x.eq
assert cmp is attr.fields(C).x.order

@pytest.mark.parametrize("slots", [True, False])
def test_no_setattr_if_validate_without_validators(self, slots):
Expand Down

0 comments on commit c422bf8

Please sign in to comment.