Stop raising a deprecation warning on assoc (#1119)

* Stop warning about assoc & explain why

* Fix tests
This commit is contained in:
Hynek Schlawack 2023-04-04 08:50:55 +02:00 committed by GitHub
parent 5596c4f11d
commit 22ae8473fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 29 deletions

View File

@ -316,6 +316,14 @@ def assoc(inst, **changes):
"""
Copy *inst* and apply *changes*.
This is different from `evolve` that applies the changes to the arguments
that create the new instance.
`evolve`'s behavior is preferable, but there are `edge cases`_ where it
doesn't work. Therefore `assoc` is deprecated, but will not be removed.
.. _`edge cases`: https://github.com/python-attrs/attrs/issues/251
:param inst: Instance of a class with *attrs* attributes.
:param changes: Keyword changes in the new copy.
@ -331,13 +339,6 @@ def assoc(inst, **changes):
This function will not be removed du to the slightly different approach
compared to `attrs.evolve`.
"""
import warnings
warnings.warn(
"assoc is deprecated and will be removed after 2018/01.",
DeprecationWarning,
stacklevel=2,
)
new = copy.copy(inst)
attrs = fields(inst.__class__)
for k, v in changes.items():

View File

@ -467,8 +467,7 @@ class TestAssoc:
pass
i1 = C()
with pytest.deprecated_call():
i2 = assoc(i1)
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
@ -479,8 +478,7 @@ class TestAssoc:
No changes means a verbatim copy.
"""
i1 = C()
with pytest.deprecated_call():
i2 = assoc(i1)
i2 = assoc(i1)
assert i1 is not i2
assert i1 == i2
@ -497,8 +495,7 @@ class TestAssoc:
chosen_names = data.draw(st.sets(st.sampled_from(field_names)))
change_dict = {name: data.draw(st.integers()) for name in chosen_names}
with pytest.deprecated_call():
changed = assoc(original, **change_dict)
changed = assoc(original, **change_dict)
for k, v in change_dict.items():
assert getattr(changed, k) == v
@ -527,22 +524,7 @@ class TestAssoc:
x = attr.ib()
y = attr.ib()
with pytest.deprecated_call():
assert C(3, 2) == assoc(C(1, 2), x=3)
def test_warning(self):
"""
DeprecationWarning points to the correct file.
"""
@attr.s
class C:
x = attr.ib()
with pytest.warns(DeprecationWarning) as wi:
assert C(2) == assoc(C(1), x=2)
assert __file__ == wi.list[0].filename
assert C(3, 2) == assoc(C(1, 2), x=3)
class TestEvolve: