File: test_deprecation_helpers.py

package info (click to toggle)
python-xarray 2025.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,796 kB
  • sloc: python: 115,416; makefile: 258; sh: 47
file content (140 lines) | stat: -rw-r--r-- 4,630 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pytest

from xarray.util.deprecation_helpers import _deprecate_positional_args


def test_deprecate_positional_args_warns_for_function():
    @_deprecate_positional_args("v0.1")
    def f1(a, b, *, c="c", d="d"):
        return a, b, c, d

    result = f1(1, 2)
    assert result == (1, 2, "c", "d")

    result = f1(1, 2, c=3, d=4)
    assert result == (1, 2, 3, 4)

    with pytest.warns(FutureWarning, match=r".*v0.1"):
        result = f1(1, 2, 3)  # type: ignore[misc]
    assert result == (1, 2, 3, "d")

    with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"):
        result = f1(1, 2, 3)  # type: ignore[misc]
    assert result == (1, 2, 3, "d")

    with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"):
        result = f1(1, 2, 3, 4)  # type: ignore[misc]
    assert result == (1, 2, 3, 4)

    @_deprecate_positional_args("v0.1")
    def f2(a="a", *, b="b", c="c", d="d"):
        return a, b, c, d

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = f2(1, 2)  # type: ignore[misc]
    assert result == (1, 2, "c", "d")

    @_deprecate_positional_args("v0.1")
    def f3(a, *, b="b", **kwargs):
        return a, b, kwargs

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = f3(1, 2)  # type: ignore[misc]
    assert result == (1, 2, {})

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = f3(1, 2, f="f")  # type: ignore[misc]
    assert result == (1, 2, {"f": "f"})

    @_deprecate_positional_args("v0.1")
    def f4(a, /, *, b="b", **kwargs):
        return a, b, kwargs

    result = f4(1)
    assert result == (1, "b", {})

    result = f4(1, b=2, f="f")
    assert result == (1, 2, {"f": "f"})

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = f4(1, 2, f="f")  # type: ignore[misc]
    assert result == (1, 2, {"f": "f"})

    with pytest.raises(TypeError, match=r"Keyword-only param without default"):

        @_deprecate_positional_args("v0.1")
        def f5(a, *, b, c=3, **kwargs):
            pass


def test_deprecate_positional_args_warns_for_class():
    class A1:
        @_deprecate_positional_args("v0.1")
        def method(self, a, b, *, c="c", d="d"):
            return a, b, c, d

    result = A1().method(1, 2)
    assert result == (1, 2, "c", "d")

    result = A1().method(1, 2, c=3, d=4)
    assert result == (1, 2, 3, 4)

    with pytest.warns(FutureWarning, match=r".*v0.1"):
        result = A1().method(1, 2, 3)  # type: ignore[misc]
    assert result == (1, 2, 3, "d")

    with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"):
        result = A1().method(1, 2, 3)  # type: ignore[misc]
    assert result == (1, 2, 3, "d")

    with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"):
        result = A1().method(1, 2, 3, 4)  # type: ignore[misc]
    assert result == (1, 2, 3, 4)

    class A2:
        @_deprecate_positional_args("v0.1")
        def method(self, a=1, b=1, *, c="c", d="d"):
            return a, b, c, d

    with pytest.warns(FutureWarning, match=r"Passing 'c' as positional"):
        result = A2().method(1, 2, 3)  # type: ignore[misc]
    assert result == (1, 2, 3, "d")

    with pytest.warns(FutureWarning, match=r"Passing 'c, d' as positional"):
        result = A2().method(1, 2, 3, 4)  # type: ignore[misc]
    assert result == (1, 2, 3, 4)

    class A3:
        @_deprecate_positional_args("v0.1")
        def method(self, a, *, b="b", **kwargs):
            return a, b, kwargs

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = A3().method(1, 2)  # type: ignore[misc]
    assert result == (1, 2, {})

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = A3().method(1, 2, f="f")  # type: ignore[misc]
    assert result == (1, 2, {"f": "f"})

    class A4:
        @_deprecate_positional_args("v0.1")
        def method(self, a, /, *, b="b", **kwargs):
            return a, b, kwargs

    result = A4().method(1)
    assert result == (1, "b", {})

    result = A4().method(1, b=2, f="f")
    assert result == (1, 2, {"f": "f"})

    with pytest.warns(FutureWarning, match=r"Passing 'b' as positional"):
        result = A4().method(1, 2, f="f")  # type: ignore[misc]
    assert result == (1, 2, {"f": "f"})

    with pytest.raises(TypeError, match=r"Keyword-only param without default"):

        class A5:
            @_deprecate_positional_args("v0.1")
            def __init__(self, a, *, b, c=3, **kwargs):
                pass