File: test_multidispatch.py

package info (click to toggle)
python-generic 1.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 360 kB
  • sloc: python: 879; makefile: 126; sh: 2
file content (261 lines) | stat: -rw-r--r-- 6,821 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""Tests for :module:`generic.multidispatch`."""

import logging
from inspect import FullArgSpec

import pytest

from generic.multidispatch import FunctionDispatcher, multidispatch


def create_dispatcher(
    params_arity, args=None, varargs=None, keywords=None, defaults=None
) -> FunctionDispatcher:
    return FunctionDispatcher(
        FullArgSpec(
            args=args,
            varargs=varargs,
            varkw=keywords,
            defaults=defaults,
            kwonlyargs=[],
            kwonlydefaults={},
            annotations={},
        ),
        params_arity,
    )


def test_one_argument():
    dispatcher = create_dispatcher(1, args=["x"])

    dispatcher.register_rule(lambda x: x + 1, int)
    assert dispatcher(1) == 2
    with pytest.raises(TypeError):
        dispatcher("s")

    dispatcher.register_rule(lambda x: f"{x}1", str)
    assert dispatcher(1) == 2
    assert dispatcher("1") == "11"
    with pytest.raises(TypeError):
        dispatcher(())


def test_two_arguments():
    dispatcher = create_dispatcher(2, args=["x", "y"])

    dispatcher.register_rule(lambda x, y: x + y + 1, int, int)
    assert dispatcher(1, 2) == 4
    with pytest.raises(TypeError):
        dispatcher("s", "ss")
    with pytest.raises(TypeError):
        dispatcher(1, "ss")
    with pytest.raises(TypeError):
        dispatcher("s", 2)

    dispatcher.register_rule(lambda x, y: x + y + "1", str, str)
    assert dispatcher(1, 2) == 4
    assert dispatcher("1", "2") == "121"
    with pytest.raises(TypeError):
        dispatcher("1", 1)
    with pytest.raises(TypeError):
        dispatcher(1, "1")

    dispatcher.register_rule(lambda x, y: str(x) + y + "1", int, str)
    assert dispatcher(1, 2) == 4
    assert dispatcher("1", "2") == "121"
    assert dispatcher(1, "2") == "121"
    with pytest.raises(TypeError):
        dispatcher("1", 1)


def test_bottom_rule():
    dispatcher = create_dispatcher(1, args=["x"])

    dispatcher.register_rule(lambda x: x, object)
    assert dispatcher(1) == 1
    assert dispatcher("1") == "1"
    assert dispatcher([1]) == [1]
    assert dispatcher((1,)) == (1,)


def test_subtype_evaluation():
    class Super:
        pass

    class Sub(Super):
        pass

    dispatcher = create_dispatcher(1, args=["x"])

    dispatcher.register_rule(lambda x: x, Super)
    o_super = Super()
    assert dispatcher(o_super) == o_super
    o_sub = Sub()
    assert dispatcher(o_sub) == o_sub
    with pytest.raises(TypeError):
        dispatcher(object())

    dispatcher.register_rule(lambda x: (x, x), Sub)
    o_super = Super()
    assert dispatcher(o_super) == o_super
    o_sub = Sub()
    assert dispatcher(o_sub) == (o_sub, o_sub)


def test_subtype_and_none_evaluation():
    class Super:
        pass

    class Sub(Super):
        pass

    dispatcher = create_dispatcher(2, args=["x", "y"])

    dispatcher.register_rule(lambda x, y: (x, y), Super, None)
    dispatcher.register_rule(lambda x, y: x == y, Sub, Sub)

    o_super = Super()
    assert dispatcher(o_super, None) == (o_super, None)
    o_sub = Sub()
    assert dispatcher(o_sub, None) == (o_sub, None)
    with pytest.raises(TypeError):
        dispatcher(object())


def test_register_rule_with_wrong_arity():
    dispatcher = create_dispatcher(1, args=["x"])
    dispatcher.register_rule(lambda x: x, int)
    with pytest.raises(TypeError):
        dispatcher.register_rule(lambda x, y: x, str)


def test_register_rule_with_different_arg_names():
    dispatcher = create_dispatcher(1, args=["x"])
    dispatcher.register_rule(lambda y: y, int)
    assert dispatcher(1) == 1


def test_dispatching_with_varargs():
    dispatcher = create_dispatcher(1, args=["x"], varargs="va")
    dispatcher.register_rule(lambda x, *va: x, int)
    assert dispatcher(1) == 1
    with pytest.raises(TypeError):
        dispatcher("1", 2, 3)


def test_dispatching_with_varkw():
    dispatcher = create_dispatcher(1, args=["x"], keywords="vk")
    dispatcher.register_rule(lambda x, **vk: x, int)
    assert dispatcher(1) == 1
    with pytest.raises(TypeError):
        dispatcher("1", a=1, b=2)


def test_dispatching_with_kw():
    dispatcher = create_dispatcher(1, args=["x", "y"], defaults=["vk"])
    dispatcher.register_rule(lambda x, y=1: x, int)
    assert dispatcher(1) == 1
    with pytest.raises(TypeError):
        dispatcher("1", k=1)


def test_create_dispatcher_with_pos_args_less_multi_arity():
    with pytest.raises(TypeError):
        create_dispatcher(2, args=["x"])
    with pytest.raises(TypeError):
        create_dispatcher(2, args=["x", "y"], defaults=["x"])


def test_register_rule_with_wrong_number_types_parameters():
    dispatcher = create_dispatcher(1, args=["x", "y"])
    with pytest.raises(TypeError):
        dispatcher.register_rule(lambda x, y: x, int, str)


def test_register_rule_with_partial_dispatching():
    dispatcher = create_dispatcher(1, args=["x", "y"])
    dispatcher.register_rule(lambda x, y: x, int)
    assert dispatcher(1, 2) == 1
    assert dispatcher(1, "2") == 1
    with pytest.raises(TypeError):
        dispatcher("2", 1)
    dispatcher.register_rule(lambda x, y: x, str)
    assert dispatcher(1, 2) == 1
    assert dispatcher(1, "2") == 1
    assert dispatcher("1", "2") == "1"
    assert dispatcher("1", 2) == "1"


def test_default_dispatcher():
    @multidispatch(int, str)
    def func(x, y):
        return str(x) + y

    assert func(1, "2") == "12"
    with pytest.raises(TypeError):
        func(1, 2)
    with pytest.raises(TypeError):
        func("1", 2)
    with pytest.raises(TypeError):
        func("1", "2")


def test_multiple_functions():
    @multidispatch(int, str)
    def func(x, y):
        return str(x) + y

    @func.register(str, str)
    def _(x, y):
        return x + y

    assert func(1, "2") == "12"
    assert func("1", "2") == "12"
    with pytest.raises(TypeError):
        func(1, 2)
    with pytest.raises(TypeError):
        func("1", 2)


def test_default():
    @multidispatch()
    def func(x, y):
        return x + y

    @func.register(str, str)
    def _(x, y):
        return y + x

    assert func(1, 1) == 2
    assert func("1", "2") == "21"


def test_on_classes():
    @multidispatch()
    class A:
        def __init__(self, a, b):
            self.v = a + b

    @A.register(str, str)  # type: ignore[attr-defined]
    class B:
        def __init__(self, a, b):
            self.v = b + a

    assert A(1, 1).v == 2
    assert A("1", "2").v == "21"


def test_logging(caplog):
    @multidispatch(str, str)
    def func(x, y):
        return x + y

    caplog.set_level(logging.DEBUG)
    with pytest.raises(TypeError):
        func(1, 2)

    rec = caplog.records[0]

    assert rec.levelname == "DEBUG"
    assert rec.module == "multidispatch"
    assert rec.name == "generic.multidispatch"