File: test_structure.py

package info (click to toggle)
python-cattrs 25.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 12,236; makefile: 155
file content (380 lines) | stat: -rw-r--r-- 10,867 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""Test structuring of collections and primitives."""

from typing import Any, Dict, FrozenSet, List, MutableSet, Optional, Set, Tuple, Union

from attrs import define
from hypothesis import assume, given
from hypothesis.strategies import (
    binary,
    booleans,
    data,
    floats,
    frozensets,
    integers,
    just,
    lists,
    one_of,
    sampled_from,
    sets,
    text,
    tuples,
)
from pytest import raises

from cattrs import BaseConverter
from cattrs._compat import copy_with, is_bare, is_union_type
from cattrs.errors import IterableValidationError, StructureHandlerNotFoundError

from .untyped import (
    deque_seqs_of_primitives,
    dicts_of_primitives,
    lists_of_primitives,
    primitive_strategies,
    seqs_of_primitives,
)

NoneType = type(None)
ints_and_type = tuples(integers(), just(int))
floats_and_type = tuples(floats(allow_nan=False), just(float))
strs_and_type = tuples(text(), just(str))
bytes_and_type = tuples(binary(), just(bytes))

primitives_and_type = one_of(
    ints_and_type, floats_and_type, strs_and_type, bytes_and_type
)

mut_set_types = sampled_from([Set, MutableSet])
set_types = one_of(mut_set_types, just(FrozenSet))


def create_generic_type(generic_types, param_type):
    """Create a strategy for generating parameterized generic types."""
    return one_of(
        generic_types,
        generic_types.map(lambda t: t[Any]),
        generic_types.map(lambda t: t[param_type]),
    )


mut_sets_of_primitives = primitive_strategies.flatmap(
    lambda e: tuples(sets(e[0]), create_generic_type(mut_set_types, e[1]))
)

frozen_sets_of_primitives = primitive_strategies.flatmap(
    lambda e: tuples(frozensets(e[0]), create_generic_type(just(FrozenSet), e[1]))
)

sets_of_primitives = one_of(mut_sets_of_primitives, frozen_sets_of_primitives)


@given(primitives_and_type)
def test_structuring_primitives(primitive_and_type):
    """Test just structuring a primitive value."""
    converter = BaseConverter()
    val, t = primitive_and_type
    assert converter.structure(val, t) == val
    assert converter.structure(val, Any) == val


@given(seqs_of_primitives)
def test_structuring_seqs(seq_and_type):
    """Test structuring sequence generic types."""
    converter = BaseConverter()
    iterable, t = seq_and_type
    converted = converter.structure(iterable, t)
    for x, y in zip(iterable, converted):
        assert x == y


@given(deque_seqs_of_primitives)
def test_structuring_seqs_to_deque(seq_and_type):
    """Test structuring sequence generic types."""
    converter = BaseConverter()
    iterable, t = seq_and_type
    converted = converter.structure(iterable, t)
    for x, y in zip(iterable, converted):
        assert x == y


@given(sets_of_primitives, set_types)
def test_structuring_sets(set_and_type, set_type):
    """Test structuring generic sets."""
    converter = BaseConverter()
    set_, input_set_type = set_and_type

    if input_set_type not in (Set, FrozenSet, MutableSet):
        set_type = set_type[input_set_type.__args__[0]]

    converted = converter.structure(set_, set_type)
    assert converted == set_

    # Set[int] can't be used with isinstance any more.
    non_generic = set_type.__origin__ if set_type.__origin__ is not None else set_type
    assert isinstance(converted, non_generic)

    converted = converter.structure(set_, Any)
    assert converted == set_
    assert isinstance(converted, type(set_))


@given(sets_of_primitives)
def test_stringifying_sets(set_and_type):
    """Test structuring generic sets and converting the contents to str."""
    converter = BaseConverter()
    set_, input_set_type = set_and_type

    if is_bare(input_set_type):
        input_set_type = input_set_type[str]
    else:
        input_set_type = copy_with(input_set_type, str)
    converted = converter.structure(set_, input_set_type)
    assert len(converted) == len(set_)
    for e in set_:
        assert str(e) in converted


@given(lists(primitives_and_type, min_size=1), booleans())
def test_structuring_hetero_tuples(list_of_vals_and_types, detailed_validation):
    """Test structuring heterogenous tuples."""
    converter = BaseConverter(detailed_validation=detailed_validation)
    types = tuple(e[1] for e in list_of_vals_and_types)
    vals = [e[0] for e in list_of_vals_and_types]
    t = Tuple[types] if types else Tuple

    converted = converter.structure(vals, t)

    assert isinstance(converted, tuple)

    for x, y in zip(vals, converted):
        assert x == y

    for x, y in zip(types, converted):
        assert isinstance(y, x)

    t2 = Tuple[(*types, str)]  # one longer
    vals2 = [*vals, None]  # one longer
    expected_exception = IterableValidationError if detailed_validation else ValueError
    with raises(expected_exception):
        converter.structure(vals, t2)
    with raises(expected_exception):
        converter.structure(vals2, t)


@given(lists(primitives_and_type))
def test_stringifying_tuples(list_of_vals_and_types):
    """Stringify all elements of a heterogeneous tuple."""
    converter = BaseConverter()
    vals = [e[0] for e in list_of_vals_and_types]
    if len(list_of_vals_and_types):
        t = Tuple[(str,) * len(list_of_vals_and_types)]
    else:
        t = Tuple

    converted = converter.structure(vals, t)

    assert isinstance(converted, tuple)

    for x, y in zip(vals, converted):
        assert str(x) == y

    for x in converted:
        # this should just be unicode, but in python2, '' is not unicode
        assert isinstance(x, str)


@given(dicts_of_primitives)
def test_structuring_dicts(dict_and_type):
    converter = BaseConverter()
    d, t = dict_and_type

    converted = converter.structure(d, t)

    assert converted == d
    assert converted is not d


@given(dicts_of_primitives, data())
def test_structuring_dicts_opts(dict_and_type, data):
    """Structure dicts, but with optional primitives."""
    converter = BaseConverter()
    d, t = dict_and_type
    assume(not is_bare(t))
    t = copy_with(t, (t.__args__[0], Optional[t.__args__[1]]))
    d = {k: v if data.draw(booleans()) else None for k, v in d.items()}

    converted = converter.structure(d, t)

    assert converted == d
    assert converted is not d


@given(dicts_of_primitives)
def test_stringifying_dicts(dict_and_type):
    converter = BaseConverter()
    d, t = dict_and_type

    converted = converter.structure(d, Dict[str, str])

    for k, v in d.items():
        assert converted[str(k)] == str(v)


@given(primitives_and_type)
def test_structuring_optional_primitives(primitive_and_type):
    """Test structuring Optional primitive types."""
    converter = BaseConverter()
    val, type = primitive_and_type

    assert converter.structure(val, Optional[type]) == val
    assert converter.structure(None, Optional[type]) is None


@given(lists_of_primitives().filter(lambda lp: not is_bare(lp[1])), booleans())
def test_structuring_lists_of_opt(list_and_type, detailed_validation: bool) -> None:
    """Test structuring lists of Optional primitive types."""
    converter = BaseConverter(detailed_validation=detailed_validation)
    lst, t = list_and_type

    lst.append(None)
    args = t.__args__

    is_optional = args[0] is Optional or (
        is_union_type(args[0])
        and len(args[0].__args__) == 2
        and args[0].__args__[1] is NoneType
    )

    if not is_bare(t) and (args[0] not in (Any, str) and not is_optional):
        with raises(
            (TypeError, ValueError)
            if not detailed_validation
            else IterableValidationError
        ):
            converter.structure(lst, t)

    optional_t = Optional[args[0]]
    # We want to create a generic type annotation with an optional
    # type parameter.
    t = copy_with(t, optional_t)

    converted = converter.structure(lst, t)

    for x, y in zip(lst, converted):
        assert x == y


@given(lists_of_primitives())
def test_stringifying_lists_of_opt(list_and_type):
    """Test structuring Optional primitive types into strings."""
    converter = BaseConverter()
    lst, t = list_and_type

    lst.append(None)

    converted = converter.structure(lst, List[Optional[str]])

    for x, y in zip(lst, converted):
        if x is None:
            assert x is y
        else:
            assert str(x) == y


@given(lists(integers()))
def test_structuring_primitive_union_hook(ints):
    """Registering a union loading hook works."""
    converter = BaseConverter()

    def structure_hook(val, cl):
        """Even ints are passed through, odd are stringified."""
        return val if val % 2 == 0 else str(val)

    converter.register_structure_hook(Union[str, int], structure_hook)

    converted = converter.structure(ints, List[Union[str, int]])

    for x, y in zip(ints, converted):
        if x % 2 == 0:
            assert x == y
        else:
            assert str(x) == y


def test_structure_hook_func():
    """testing the hook_func method"""
    converter = BaseConverter()

    def can_handle(cls):
        return cls.__name__.startswith("F")

    def handle(obj, cls):
        return "hi"

    class Foo:
        pass

    class Bar:
        pass

    converter.register_structure_hook_func(can_handle, handle)

    assert converter.structure(10, Foo) == "hi"
    with raises(StructureHandlerNotFoundError) as exc:
        converter.structure(10, Bar)

    assert exc.value.type_ is Bar


def test_structuring_unsupported():
    """Loading unsupported classes should throw."""
    converter = BaseConverter()
    with raises(StructureHandlerNotFoundError) as exc:
        converter.structure(1, BaseConverter)

    assert exc.value.type_ is BaseConverter

    with raises(StructureHandlerNotFoundError) as exc:
        converter.structure(1, Union[int, str])

    assert exc.value.type_ == Union[int, str]


def test_subclass_registration_is_honored():
    """If a subclass is registered after a superclass,
    that subclass handler should be dispatched for
    structure
    """
    converter = BaseConverter()

    class Foo:
        def __init__(self, value):
            self.value = value

    class Bar(Foo):
        pass

    converter.register_structure_hook(Foo, lambda obj, cls: cls("foo"))
    assert converter.structure(None, Foo).value == "foo"
    assert converter.structure(None, Bar).value == "foo"
    converter.register_structure_hook(Bar, lambda obj, cls: cls("bar"))
    assert converter.structure(None, Foo).value == "foo"
    assert converter.structure(None, Bar).value == "bar"


def test_structure_union_edge_case():
    converter = BaseConverter()

    @define
    class A:
        a1: Any
        a2: Optional[Any] = None

    @define
    class B:
        b1: Any
        b2: Optional[Any] = None

    assert converter.structure([{"a1": "foo"}, {"b1": "bar"}], List[Union[A, B]]) == [
        A("foo"),
        B("bar"),
    ]