File: test_converters.py

package info (click to toggle)
python-attrs 25.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,004 kB
  • sloc: python: 10,495; makefile: 153
file content (366 lines) | stat: -rw-r--r-- 9,093 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# SPDX-License-Identifier: MIT

"""
Tests for `attr.converters`.
"""

import pickle

import pytest

import attr

from attr import Converter, Factory, attrib
from attr._compat import _AnnotationExtractor
from attr.converters import default_if_none, optional, pipe, to_bool


class TestConverter:
    @pytest.mark.parametrize("takes_self", [True, False])
    @pytest.mark.parametrize("takes_field", [True, False])
    def test_pickle(self, takes_self, takes_field):
        """
        Wrapped converters can be pickled.
        """
        c = Converter(int, takes_self=takes_self, takes_field=takes_field)

        new_c = pickle.loads(pickle.dumps(c))

        assert c == new_c
        assert takes_self == new_c.takes_self
        assert takes_field == new_c.takes_field
        assert c.__call__.__name__ == new_c.__call__.__name__

    @pytest.mark.parametrize(
        "scenario",
        [
            ((False, False), "__attr_converter_le_name(le_value)"),
            (
                (True, True),
                "__attr_converter_le_name(le_value, self, attr_dict['le_name'])",
            ),
            (
                (True, False),
                "__attr_converter_le_name(le_value, self)",
            ),
            (
                (False, True),
                "__attr_converter_le_name(le_value, attr_dict['le_name'])",
            ),
        ],
    )
    def test_fmt_converter_call(self, scenario):
        """
        _fmt_converter_call determines the arguments to the wrapped converter
        according to `takes_self` and `takes_field`.
        """
        (takes_self, takes_field), expect = scenario

        c = Converter(None, takes_self=takes_self, takes_field=takes_field)

        assert expect == c._fmt_converter_call("le_name", "le_value")

    def test_works_as_adapter(self):
        """
        Converter instances work as adapters and pass the correct arguments to
        the wrapped converter callable.
        """
        taken = None
        instance = object()
        field = object()

        def save_args(*args):
            nonlocal taken
            taken = args
            return args[0]

        Converter(save_args)(42, instance, field)

        assert (42,) == taken

        Converter(save_args, takes_self=True)(42, instance, field)

        assert (42, instance) == taken

        Converter(save_args, takes_field=True)(42, instance, field)

        assert (42, field) == taken

        Converter(save_args, takes_self=True, takes_field=True)(
            42, instance, field
        )

        assert (42, instance, field) == taken

    def test_annotations_if_last_in_pipe(self):
        """
        If the wrapped converter has annotations, they are copied to the
        Converter __call__.
        """

        def wrapped(_, __, ___) -> float:
            pass

        c = Converter(wrapped)

        assert float is c.__call__.__annotations__["return"]

        # Doesn't overwrite globally.

        c2 = Converter(int)

        assert float is c.__call__.__annotations__["return"]
        assert None is c2.__call__.__annotations__.get("return")

    def test_falsey_converter(self):
        """
        Passing a false-y instance still produces a valid converter.
        """

        class MyConv:
            def __bool__(self):
                return False

            def __call__(self, value):
                return value * 2

        @attr.s
        class C:
            a = attrib(converter=MyConv())

        c = C(21)
        assert 42 == c.a


class TestOptional:
    """
    Tests for `optional`.
    """

    def test_success_with_type(self):
        """
        Wrapped converter is used as usual if value is not None.
        """
        c = optional(int)

        assert c("42") == 42

    def test_success_with_none(self):
        """
        Nothing happens if None.
        """
        c = optional(int)

        assert c(None) is None

    def test_fail(self):
        """
        Propagates the underlying conversion error when conversion fails.
        """
        c = optional(int)

        with pytest.raises(ValueError):
            c("not_an_int")

    def test_converter_instance(self):
        """
        Works when passed a Converter instance as argument.
        """
        c = optional(Converter(to_bool))

        assert True is c("yes", None, None)


class TestDefaultIfNone:
    def test_missing_default(self):
        """
        Raises TypeError if neither default nor factory have been passed.
        """
        with pytest.raises(TypeError, match="Must pass either"):
            default_if_none()

    def test_too_many_defaults(self):
        """
        Raises TypeError if both default and factory are passed.
        """
        with pytest.raises(TypeError, match="but not both"):
            default_if_none(True, lambda: 42)

    def test_factory_takes_self(self):
        """
        Raises ValueError if passed Factory has takes_self=True.
        """
        with pytest.raises(ValueError, match="takes_self"):
            default_if_none(Factory(list, takes_self=True))

    @pytest.mark.parametrize("val", [1, 0, True, False, "foo", "", object()])
    def test_not_none(self, val):
        """
        If a non-None value is passed, it's handed down.
        """
        c = default_if_none("nope")

        assert val == c(val)

        c = default_if_none(factory=list)

        assert val == c(val)

    def test_none_value(self):
        """
        Default values are returned when a None is passed.
        """
        c = default_if_none(42)

        assert 42 == c(None)

    def test_none_factory(self):
        """
        Factories are used if None is passed.
        """
        c = default_if_none(factory=list)

        assert [] == c(None)

        c = default_if_none(default=Factory(list))

        assert [] == c(None)


class TestPipe:
    def test_success(self):
        """
        Succeeds if all wrapped converters succeed.
        """
        c = pipe(str, Converter(to_bool), bool)

        assert (
            True
            is c.converter("True", None, None)
            is c.converter(True, None, None)
        )

    def test_fail(self):
        """
        Fails if any wrapped converter fails.
        """
        c = pipe(str, to_bool)

        # First wrapped converter fails:
        with pytest.raises(ValueError):
            c(33)

        # Last wrapped converter fails:
        with pytest.raises(ValueError):
            c("33")

    def test_sugar(self):
        """
        `pipe(c1, c2, c3)` and `[c1, c2, c3]` are equivalent.
        """

        @attr.s
        class C:
            a1 = attrib(default="True", converter=pipe(str, to_bool, bool))
            a2 = attrib(default=True, converter=[str, to_bool, bool])

        c = C()
        assert True is c.a1 is c.a2

    def test_empty(self):
        """
        Empty pipe returns same value.
        """
        o = object()

        assert o is pipe()(o)

    def test_wrapped_annotation(self):
        """
        The return type of the wrapped converter is copied into its __call__
        and ultimately into pipe's wrapped converter.
        """

        def last(value) -> bool:
            return bool(value)

        @attr.s
        class C:
            x = attr.ib(converter=[Converter(int), Converter(last)])

        i = C(5)

        assert True is i.x
        assert (
            bool
            is _AnnotationExtractor(
                attr.fields(C).x.converter.__call__
            ).get_return_type()
        )


class TestOptionalPipe:
    def test_optional(self):
        """
        Nothing happens if None.
        """
        c = optional(pipe(str, Converter(to_bool), bool))

        assert None is c.converter(None, None, None)

    def test_pipe(self):
        """
        A value is given, run it through all wrapped converters.
        """
        c = optional(pipe(str, Converter(to_bool), bool))

        assert (
            True
            is c.converter("True", None, None)
            is c.converter(True, None, None)
        )

    def test_instance(self):
        """
        Should work when set as an attrib.
        """

        @attr.s
        class C:
            x = attrib(
                converter=optional(pipe(str, Converter(to_bool), bool)),
                default=None,
            )

        c1 = C()

        assert None is c1.x

        c2 = C("True")

        assert True is c2.x


class TestToBool:
    def test_unhashable(self):
        """
        Fails if value is unhashable.
        """
        with pytest.raises(ValueError, match="Cannot convert value to bool"):
            to_bool([])

    def test_truthy(self):
        """
        Fails if truthy values are incorrectly converted.
        """
        assert to_bool("t")
        assert to_bool("yes")
        assert to_bool("on")

    def test_falsy(self):
        """
        Fails if falsy values are incorrectly converted.
        """
        assert not to_bool("f")
        assert not to_bool("no")
        assert not to_bool("off")