|
| 1 | +import pytest |
| 2 | + |
| 3 | +from mplotutils._deprecate import _deprecate_positional_args |
| 4 | + |
| 5 | + |
| 6 | +def test_deprecate_positional_args_warns_for_function(): |
| 7 | + @_deprecate_positional_args("v0.1") |
| 8 | + def f1(a, b, *, c="c", d="d"): |
| 9 | + return a, b, c, d |
| 10 | + |
| 11 | + result = f1(1, 2) |
| 12 | + assert result == (1, 2, "c", "d") |
| 13 | + |
| 14 | + result = f1(1, 2, c=3, d=4) |
| 15 | + assert result == (1, 2, 3, 4) |
| 16 | + |
| 17 | + with pytest.warns(FutureWarning, match=r".*v0.1"): |
| 18 | + result = f1(1, 2, 3) |
| 19 | + assert result == (1, 2, 3, "d") |
| 20 | + |
| 21 | + with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"): |
| 22 | + result = f1(1, 2, 3) |
| 23 | + assert result == (1, 2, 3, "d") |
| 24 | + |
| 25 | + with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"): |
| 26 | + result = f1(1, 2, 3, 4) |
| 27 | + assert result == (1, 2, 3, 4) |
| 28 | + |
| 29 | + @_deprecate_positional_args("v0.1") |
| 30 | + def f2(a="a", *, b="b", c="c", d="d"): |
| 31 | + return a, b, c, d |
| 32 | + |
| 33 | + with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 34 | + result = f2(1, 2) |
| 35 | + assert result == (1, 2, "c", "d") |
| 36 | + |
| 37 | + @_deprecate_positional_args("v0.1") |
| 38 | + def f3(a, *, b="b", **kwargs): |
| 39 | + return a, b, kwargs |
| 40 | + |
| 41 | + with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 42 | + result = f3(1, 2) |
| 43 | + assert result == (1, 2, {}) |
| 44 | + |
| 45 | + with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 46 | + result = f3(1, 2, f="f") |
| 47 | + assert result == (1, 2, {"f": "f"}) |
| 48 | + |
| 49 | + # @_deprecate_positional_args("v0.1") |
| 50 | + # def f4(a, /, *, b="b", **kwargs): |
| 51 | + # return a, b, kwargs |
| 52 | + |
| 53 | + # result = f4(1) |
| 54 | + # assert result == (1, "b", {}) |
| 55 | + |
| 56 | + # result = f4(1, b=2, f="f") |
| 57 | + # assert result == (1, 2, {"f": "f"}) |
| 58 | + |
| 59 | + # with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 60 | + # result = f4(1, 2, f="f") |
| 61 | + # assert result == (1, 2, {"f": "f"}) |
| 62 | + |
| 63 | + with pytest.raises(TypeError, match=r"Keyword-only param without default"): |
| 64 | + |
| 65 | + @_deprecate_positional_args("v0.1") |
| 66 | + def f5(a, *, b, c=3, **kwargs): |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +def test_deprecate_positional_args_warns_for_class(): |
| 71 | + class A1: |
| 72 | + @_deprecate_positional_args("v0.1") |
| 73 | + def method(self, a, b, *, c="c", d="d"): |
| 74 | + return a, b, c, d |
| 75 | + |
| 76 | + result = A1().method(1, 2) |
| 77 | + assert result == (1, 2, "c", "d") |
| 78 | + |
| 79 | + result = A1().method(1, 2, c=3, d=4) |
| 80 | + assert result == (1, 2, 3, 4) |
| 81 | + |
| 82 | + with pytest.warns(FutureWarning, match=r".*v0.1"): |
| 83 | + result = A1().method(1, 2, 3) |
| 84 | + assert result == (1, 2, 3, "d") |
| 85 | + |
| 86 | + with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"): |
| 87 | + result = A1().method(1, 2, 3) |
| 88 | + assert result == (1, 2, 3, "d") |
| 89 | + |
| 90 | + with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"): |
| 91 | + result = A1().method(1, 2, 3, 4) |
| 92 | + assert result == (1, 2, 3, 4) |
| 93 | + |
| 94 | + class A2: |
| 95 | + @_deprecate_positional_args("v0.1") |
| 96 | + def method(self, a=1, b=1, *, c="c", d="d"): |
| 97 | + return a, b, c, d |
| 98 | + |
| 99 | + with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"): |
| 100 | + result = A2().method(1, 2, 3) |
| 101 | + assert result == (1, 2, 3, "d") |
| 102 | + |
| 103 | + with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"): |
| 104 | + result = A2().method(1, 2, 3, 4) |
| 105 | + assert result == (1, 2, 3, 4) |
| 106 | + |
| 107 | + class A3: |
| 108 | + @_deprecate_positional_args("v0.1") |
| 109 | + def method(self, a, *, b="b", **kwargs): |
| 110 | + return a, b, kwargs |
| 111 | + |
| 112 | + with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 113 | + result = A3().method(1, 2) |
| 114 | + assert result == (1, 2, {}) |
| 115 | + |
| 116 | + with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 117 | + result = A3().method(1, 2, f="f") |
| 118 | + assert result == (1, 2, {"f": "f"}) |
| 119 | + |
| 120 | + # class A4: |
| 121 | + # @_deprecate_positional_args("v0.1") |
| 122 | + # def method(self, a, /, *, b="b", **kwargs): |
| 123 | + # return a, b, kwargs |
| 124 | + |
| 125 | + # result = A4().method(1) |
| 126 | + # assert result == (1, "b", {}) |
| 127 | + |
| 128 | + # result = A4().method(1, b=2, f="f") |
| 129 | + # assert result == (1, 2, {"f": "f"}) |
| 130 | + |
| 131 | + # with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"): |
| 132 | + # result = A4().method(1, 2, f="f") |
| 133 | + # assert result == (1, 2, {"f": "f"}) |
| 134 | + |
| 135 | + with pytest.raises(TypeError, match=r"Keyword-only param without default"): |
| 136 | + |
| 137 | + class A5: |
| 138 | + @_deprecate_positional_args("v0.1") |
| 139 | + def __init__(self, a, *, b, c=3, **kwargs): |
| 140 | + pass |
0 commit comments