-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest_deprecated_decorator.py
95 lines (66 loc) · 3.02 KB
/
test_deprecated_decorator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import warnings
from typing import Any
import pytest
from mcstatus.utils import deprecated
def test_deprecated_function_produces_warning():
f = deprecated()(lambda: None)
with warnings.catch_warnings(record=True) as w:
f()
assert issubclass(w[-1].category, DeprecationWarning)
assert "deprecated" in str(w[-1].message).lower()
def test_deprecated_class_produces_warning():
test_cls: Any = type("TestCls", (), {"foo": lambda: None}) # https://github.com/microsoft/pyright/discussions/3438
dep_cls = deprecated(methods=["foo"])(test_cls)
with warnings.catch_warnings(record=True) as w:
dep_cls.foo()
assert issubclass(w[-1].category, DeprecationWarning)
assert "deprecated" in str(w[-1].message).lower()
def test_deprecated_function_return_result():
f = deprecated()(lambda x: x)
with warnings.catch_warnings(record=True):
result = f(10)
assert result == 10
def test_deprecated_class_return_result():
test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # https://github.com/microsoft/pyright/discussions/3438
dep_cls = deprecated(methods=["foo"])(test_cls)
with warnings.catch_warnings(record=True):
result = dep_cls.foo(10)
assert result == 10
def test_deprecated_function_with_methods():
with pytest.raises(ValueError):
deprecated(methods=["__str__"])(lambda: None)
def test_deprecated_class_without_methods():
test_cls: Any = type("TestCls", (), {"foo": lambda x: x}) # https://github.com/microsoft/pyright/discussions/3438
with pytest.raises(ValueError):
deprecated()(test_cls)
@pytest.mark.parametrize(
"arguments",
[
{"replacement": "library.newfunction"},
{"version": "4.8.1"},
{"date": "2022-06"},
{"msg": "Why would you still use this?"},
{"replacement": "new_function", "version": "0.1.2", "msg": "Function got renamed"},
{"replacement": "new_function", "date": "2022-11", "msg": "Function got renamed"},
],
)
def test_deprecated_arguments(arguments):
f = deprecated(**arguments)(lambda: None)
with warnings.catch_warnings(record=True) as w:
f()
warning_message = str(w[-1].message)
for value in arguments.values():
assert value in warning_message
def test_deprecated_no_call_decorator():
"""This makes sure deprecated decorator can run directly on a function without first being called.
Usually we use deprecated decorator with arguments like ``@deprecated(msg='hi')``, however this means
that using it normally would usually require doing ``@deprecated()``, but since all of the arguments
are optional, we can also support using ``@deprecated``, without calling it first. This tests that
this behavior is in fact working.
"""
f = deprecated(lambda x: x)
with warnings.catch_warnings(record=True) as w:
result = f(10)
assert issubclass(w[-1].category, DeprecationWarning)
assert "deprecated" in str(w[-1].message).lower()
assert result == 10