diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2016-03-21 09:01:47 -0400 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2016-03-21 09:01:47 -0400 |
commit | 26af1f094ce27792ad6982633008d12e6c482f73 (patch) | |
tree | 983869e3b752a9b51ac40e993c9c1818f45ecc93 | |
parent | 2e820e2a9c43be843a31db10900f19f735b28232 (diff) | |
parent | beee981420164fb6f2a8e153081b261c11fddd99 (diff) | |
download | cryptography-26af1f094ce27792ad6982633008d12e6c482f73.tar.gz cryptography-26af1f094ce27792ad6982633008d12e6c482f73.tar.bz2 cryptography-26af1f094ce27792ad6982633008d12e6c482f73.zip |
Merge pull request #2845 from Lukasa/issue/2844
Add __delattr__ to _ModuleWithDeprecations.
-rw-r--r-- | src/cryptography/utils.py | 7 | ||||
-rw-r--r-- | tests/test_warnings.py | 41 |
2 files changed, 48 insertions, 0 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py index 53795732..64a1c785 100644 --- a/src/cryptography/utils.py +++ b/src/cryptography/utils.py @@ -119,6 +119,13 @@ class _ModuleWithDeprecations(object): def __setattr__(self, attr, value): setattr(self._module, attr, value) + def __delattr__(self, attr): + obj = getattr(self._module, attr) + if isinstance(obj, _DeprecatedValue): + warnings.warn(obj.message, obj.warning_class, stacklevel=2) + + delattr(self._module, attr) + def __dir__(self): return ["_module"] + dir(self._module) diff --git a/tests/test_warnings.py b/tests/test_warnings.py index 9946baa7..d27e757f 100644 --- a/tests/test_warnings.py +++ b/tests/test_warnings.py @@ -8,6 +8,8 @@ import sys import types import warnings +import pytest + from cryptography.utils import deprecated @@ -45,3 +47,42 @@ class TestDeprecated(object): assert msg2.message.args == ("more deprecated text",) assert "Y" in dir(mod) + + def test_deleting_deprecated_members(self, monkeypatch): + mod = types.ModuleType("TestDeprecated/test_deprecated") + monkeypatch.setitem(sys.modules, mod.__name__, mod) + mod.X = deprecated( + value=1, + module_name=mod.__name__, + message="deprecated message text", + warning_class=DeprecationWarning + ) + mod.Y = deprecated( + value=2, + module_name=mod.__name__, + message="more deprecated text", + warning_class=PendingDeprecationWarning, + ) + mod = sys.modules[mod.__name__] + mod.Z = 3 + + with warnings.catch_warnings(record=True) as log: + warnings.simplefilter("always", PendingDeprecationWarning) + warnings.simplefilter("always", DeprecationWarning) + del mod.X + del mod.Y + del mod.Z + + [msg1, msg2] = log + assert msg1.category is DeprecationWarning + assert msg1.message.args == ("deprecated message text",) + + assert msg2.category is PendingDeprecationWarning + assert msg2.message.args == ("more deprecated text",) + + assert "X" not in dir(mod) + assert "Y" not in dir(mod) + assert "Z" not in dir(mod) + + with pytest.raises(AttributeError): + del mod.X |