Skip to content

Commit 94f077a

Browse files
committed
Merge pull request pyca#2847 from reaperhulk/backport-delattr
backport lukasa's delattr fix
2 parents bf1566e + cf45d87 commit 94f077a

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/cryptography/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ def __getattr__(self, attr):
119119
def __setattr__(self, attr, value):
120120
setattr(self._module, attr, value)
121121

122+
def __delattr__(self, attr):
123+
obj = getattr(self._module, attr)
124+
if isinstance(obj, _DeprecatedValue):
125+
warnings.warn(obj.message, obj.warning_class, stacklevel=2)
126+
127+
delattr(self._module, attr)
128+
122129
def __dir__(self):
123130
return ["_module"] + dir(self._module)
124131

tests/test_warnings.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import types
99
import warnings
1010

11+
import pytest
12+
1113
from cryptography.utils import deprecated
1214

1315

@@ -45,3 +47,42 @@ def test_deprecated(self, monkeypatch):
4547
assert msg2.message.args == ("more deprecated text",)
4648

4749
assert "Y" in dir(mod)
50+
51+
def test_deleting_deprecated_members(self, monkeypatch):
52+
mod = types.ModuleType("TestDeprecated/test_deprecated")
53+
monkeypatch.setitem(sys.modules, mod.__name__, mod)
54+
mod.X = deprecated(
55+
value=1,
56+
module_name=mod.__name__,
57+
message="deprecated message text",
58+
warning_class=DeprecationWarning
59+
)
60+
mod.Y = deprecated(
61+
value=2,
62+
module_name=mod.__name__,
63+
message="more deprecated text",
64+
warning_class=PendingDeprecationWarning,
65+
)
66+
mod = sys.modules[mod.__name__]
67+
mod.Z = 3
68+
69+
with warnings.catch_warnings(record=True) as log:
70+
warnings.simplefilter("always", PendingDeprecationWarning)
71+
warnings.simplefilter("always", DeprecationWarning)
72+
del mod.X
73+
del mod.Y
74+
del mod.Z
75+
76+
[msg1, msg2] = log
77+
assert msg1.category is DeprecationWarning
78+
assert msg1.message.args == ("deprecated message text",)
79+
80+
assert msg2.category is PendingDeprecationWarning
81+
assert msg2.message.args == ("more deprecated text",)
82+
83+
assert "X" not in dir(mod)
84+
assert "Y" not in dir(mod)
85+
assert "Z" not in dir(mod)
86+
87+
with pytest.raises(AttributeError):
88+
del mod.X

0 commit comments

Comments
 (0)