|
| 1 | +from typing import Literal |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from arch.vendor._decorators import deprecate_kwarg, indent |
| 6 | + |
| 7 | + |
| 8 | +@deprecate_kwarg("old", "new", {"yes": True, "no": False}, stacklevel=2) |
| 9 | +def f(x: int, *, old: Literal["yes", "no"] = "yes", new: bool = True) -> int: |
| 10 | + if new: |
| 11 | + return x + 1 |
| 12 | + else: |
| 13 | + return x - 1 |
| 14 | + |
| 15 | + |
| 16 | +@deprecate_kwarg("old", None, stacklevel=2) |
| 17 | +def g(x: int, *, old: bool = True) -> int: |
| 18 | + """ |
| 19 | + Function with keyword-only arguments. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + x : int |
| 24 | + An integer value. |
| 25 | + """ |
| 26 | + return x + 1 |
| 27 | + |
| 28 | + |
| 29 | +@deprecate_kwarg("old", "new", stacklevel=2) |
| 30 | +def bar(old=False, new=False): |
| 31 | + return new |
| 32 | + |
| 33 | + |
| 34 | +def _baz_mapping(old: Literal["yes", "no"]) -> bool: |
| 35 | + if old == "yes": |
| 36 | + return True |
| 37 | + elif old == "no": |
| 38 | + return False |
| 39 | + else: |
| 40 | + raise ValueError(old) |
| 41 | + |
| 42 | + |
| 43 | +@deprecate_kwarg("old", "new", _baz_mapping, stacklevel=2) |
| 44 | +def baz(x: int, *, old: Literal["yes", "no"] = "yes", new: bool = True) -> int: |
| 45 | + if new: |
| 46 | + return x + 1 |
| 47 | + else: |
| 48 | + return x - 1 |
| 49 | + |
| 50 | + |
| 51 | +def test_deprecate_kwarg(): |
| 52 | + """ |
| 53 | + Test the deprecation of the `y` keyword argument in the function `f`. |
| 54 | + """ |
| 55 | + with pytest.warns(FutureWarning, match="the old"): |
| 56 | + f(1, old="yes") |
| 57 | + with pytest.warns(FutureWarning, match="the old"): |
| 58 | + f(1, old="no") |
| 59 | + f(2, new=True) |
| 60 | + f(2, new=False) |
| 61 | + with pytest.raises(TypeError): |
| 62 | + f(2, old="yes", new=True) |
| 63 | + baz(2, old="yes") |
| 64 | + baz(2, old="no") |
| 65 | + with pytest.raises(ValueError): |
| 66 | + baz(2, old="maybe") |
| 67 | + |
| 68 | + |
| 69 | +def test_deprecate_kwarg_no_alt(): |
| 70 | + """ |
| 71 | + Test the deprecation of the `y` keyword argument in the function `f`. |
| 72 | + """ |
| 73 | + with pytest.warns(FutureWarning, match="the 'old'"): |
| 74 | + g(1, old=True) |
| 75 | + |
| 76 | + |
| 77 | +def test_bad_deprecate_kwarg(): |
| 78 | + def constructor(): |
| 79 | + @deprecate_kwarg("old", None, [("yes", True), ("no", False)]) |
| 80 | + def h(x: int, *, old: bool = True) -> int: |
| 81 | + """ |
| 82 | + Function with keyword-only arguments. |
| 83 | +
|
| 84 | + Parameters |
| 85 | + ---------- |
| 86 | + x : int |
| 87 | + An integer value. |
| 88 | + """ |
| 89 | + return x + 1 |
| 90 | + |
| 91 | + return h |
| 92 | + |
| 93 | + with pytest.raises(TypeError): |
| 94 | + constructor() |
| 95 | + |
| 96 | + |
| 97 | +def test_simple_depr(): |
| 98 | + with pytest.warns(FutureWarning, match="the 'old'"): |
| 99 | + bar(old=True) |
| 100 | + |
| 101 | + |
| 102 | +def test_indent(): |
| 103 | + res = indent( |
| 104 | + """ |
| 105 | +This is a test |
| 106 | +""", |
| 107 | + 1, |
| 108 | + ) |
| 109 | + assert res[:5] == "\n" + " " * 4 |
| 110 | + assert res[5:] == "This is a test\n " |
| 111 | + |
| 112 | + assert indent(None) == "" |
0 commit comments