|
4 | 4 | from tests.helpers.utils import no_warning_call |
5 | 5 |
|
6 | 6 |
|
7 | | -def my_sum(a, b=3): |
| 7 | +def my_sum(a=0, b=3): |
| 8 | + return a + b |
| 9 | + |
| 10 | + |
| 11 | +def my2_sum(a, b): |
8 | 12 | return a + b |
9 | 13 |
|
10 | 14 |
|
11 | 15 | @deprecated(target=my_sum, ver_deprecate="0.1", ver_remove="0.5") |
12 | | -def dep_sum(a, b): |
| 16 | +def dep_sum(a, b=5): |
13 | 17 | pass |
14 | 18 |
|
15 | 19 |
|
16 | | -@deprecated(target=my_sum, ver_deprecate="0.1", ver_remove="0.5") |
| 20 | +@deprecated(target=my2_sum, ver_deprecate="0.1", ver_remove="0.5") |
17 | 21 | def dep2_sum(a, b): |
18 | 22 | pass |
19 | 23 |
|
20 | 24 |
|
| 25 | +@deprecated(target=my2_sum, ver_deprecate="0.1", ver_remove="0.5") |
| 26 | +def dep3_sum(a, b=4): |
| 27 | + pass |
| 28 | + |
| 29 | + |
21 | 30 | def test_deprecated_func(): |
22 | 31 | with pytest.deprecated_call( |
23 | | - match='The `dep_sum` was deprecated since v0.1 in favor of `tests.utilities.test_deprecation.my_sum`.' |
24 | | - ' It will be removed in v0.5.' |
| 32 | + match='`tests.utilities.test_deprecation.dep_sum` was deprecated since v0.1 in favor' |
| 33 | + ' of `tests.utilities.test_deprecation.my_sum`. It will be removed in v0.5.' |
25 | 34 | ): |
26 | | - assert dep_sum(2, b=5) == 7 |
| 35 | + assert dep_sum(2) == 7 |
27 | 36 |
|
28 | 37 | # check that the warning is raised only once per function |
29 | 38 | with no_warning_call(DeprecationWarning): |
30 | | - assert dep_sum(2, b=5) == 7 |
| 39 | + assert dep_sum(3) == 8 |
31 | 40 |
|
32 | 41 | # and does not affect other functions |
33 | 42 | with pytest.deprecated_call( |
34 | | - match='The `dep2_sum` was deprecated since v0.1 in favor of `tests.utilities.test_deprecation.my_sum`.' |
35 | | - ' It will be removed in v0.5.' |
| 43 | + match='`tests.utilities.test_deprecation.dep3_sum` was deprecated since v0.1 in favor' |
| 44 | + ' of `tests.utilities.test_deprecation.my2_sum`. It will be removed in v0.5.' |
36 | 45 | ): |
37 | | - assert dep2_sum(2) == 5 |
| 46 | + assert dep3_sum(2, 1) == 3 |
| 47 | + |
| 48 | + |
| 49 | +def test_deprecated_func_incomplete(): |
| 50 | + |
| 51 | + # missing required argument |
| 52 | + with pytest.raises(TypeError, match="missing 1 required positional argument: 'b'"): |
| 53 | + dep2_sum(2) |
| 54 | + |
| 55 | + # check that the warning is raised only once per function |
| 56 | + with no_warning_call(DeprecationWarning): |
| 57 | + assert dep2_sum(2, 1) == 3 |
| 58 | + |
| 59 | + # reset the warning |
| 60 | + dep2_sum.warned = False |
| 61 | + # does not affect other functions |
| 62 | + with pytest.deprecated_call( |
| 63 | + match='`tests.utilities.test_deprecation.dep2_sum` was deprecated since v0.1 in favor' |
| 64 | + ' of `tests.utilities.test_deprecation.my2_sum`. It will be removed in v0.5.' |
| 65 | + ): |
| 66 | + assert dep2_sum(b=2, a=1) == 3 |
| 67 | + |
| 68 | + |
| 69 | +class NewCls: |
| 70 | + |
| 71 | + def __init__(self, c, d="abc"): |
| 72 | + self.my_c = c |
| 73 | + self.my_d = d |
| 74 | + |
| 75 | + |
| 76 | +class PastCls: |
| 77 | + |
| 78 | + @deprecated(target=NewCls, ver_deprecate="0.2", ver_remove="0.4") |
| 79 | + def __init__(self, c, d="efg"): |
| 80 | + pass |
| 81 | + |
| 82 | + |
| 83 | +def test_deprecated_class(): |
| 84 | + with pytest.deprecated_call( |
| 85 | + match='`tests.utilities.test_deprecation.PastCls` was deprecated since v0.2 in favor' |
| 86 | + ' of `tests.utilities.test_deprecation.NewCls`. It will be removed in v0.4.' |
| 87 | + ): |
| 88 | + past = PastCls(2) |
| 89 | + assert past.my_c == 2 |
| 90 | + assert past.my_d == "efg" |
| 91 | + |
| 92 | + # check that the warning is raised only once per function |
| 93 | + with no_warning_call(DeprecationWarning): |
| 94 | + assert PastCls(c=2, d="") |
0 commit comments