|
| 1 | + |
| 2 | +from collections import deque |
| 3 | +from collections.abc import Callable, Collection, Iterable, MutableSequence, MutableSet, Sequence, Set |
| 4 | +from typing import Any, Union |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from attrs import define |
| 9 | +from cattrs.converters import BaseConverter |
| 10 | +from cattrs.strategies import configure_union_single_collection_dispatch |
| 11 | + |
| 12 | + |
| 13 | +@define |
| 14 | +class CollectionParameter: |
| 15 | + type_factory: Callable[[Any], type[Collection]] |
| 16 | + factory: Callable[[Iterable], Collection] |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture( |
| 20 | + params=[ |
| 21 | + pytest.param(CollectionParameter(lambda t: deque[t], deque), id="deque"), |
| 22 | + pytest.param(CollectionParameter(lambda t: frozenset[t], frozenset), id="frozenset"), |
| 23 | + pytest.param(CollectionParameter(lambda t: list[t], list), id="list"), |
| 24 | + pytest.param(CollectionParameter(lambda t: MutableSequence[t], list), id="MutableSequence"), |
| 25 | + pytest.param(CollectionParameter(lambda t: MutableSet[t], set), id="MutableSet"), |
| 26 | + pytest.param(CollectionParameter(lambda t: Sequence[t], tuple), id="Sequence"), |
| 27 | + pytest.param(CollectionParameter(lambda t: Set[t], frozenset), id="Set"), |
| 28 | + pytest.param(CollectionParameter(lambda t: set[t], set), id="set"), |
| 29 | + pytest.param(CollectionParameter(lambda t: tuple[t, ...], tuple), id="tuple"), |
| 30 | + ], |
| 31 | +) |
| 32 | +def collection(request: pytest.FixtureRequest) -> CollectionParameter: |
| 33 | + return request.param |
| 34 | + |
| 35 | + |
| 36 | +def test_works_with_simple_union(converter: BaseConverter, collection: CollectionParameter): |
| 37 | + configure_union_single_collection_dispatch(converter) |
| 38 | + |
| 39 | + union = Union[collection.type_factory(str) | str] |
| 40 | + |
| 41 | + assert converter.structure("abcd", union) == "abcd" |
| 42 | + assert converter.structure("abcd", str) == "abcd" |
| 43 | + |
| 44 | + |
| 45 | + expected_structured = collection.factory(["abcd"]) |
| 46 | + assert converter.structure(["abcd"], union) == expected_structured |
| 47 | + assert converter.structure(["abcd"], collection.type_factory(str)) == expected_structured |
| 48 | + assert converter.structure(deque(["abcd"]), union) == expected_structured |
| 49 | + assert converter.structure(deque(["abcd"]), collection.type_factory(str)) == expected_structured |
| 50 | + assert converter.structure(frozenset(["abcd"]), union) == expected_structured |
| 51 | + assert converter.structure(frozenset(["abcd"]), collection.type_factory(str)) == expected_structured |
| 52 | + assert converter.structure(set(["abcd"]), union) == expected_structured |
| 53 | + assert converter.structure(set(["abcd"]), collection.type_factory(str)) == expected_structured |
| 54 | + assert converter.structure(tuple(["abcd"]), union) == expected_structured |
| 55 | + assert converter.structure(tuple(["abcd"]), collection.type_factory(str)) == expected_structured |
| 56 | + |
| 57 | + |
| 58 | +def test_apply_union_disambiguation(converter: BaseConverter, collection: CollectionParameter): |
| 59 | + configure_union_single_collection_dispatch(converter) |
| 60 | + |
| 61 | + @define(frozen=True) |
| 62 | + class A: |
| 63 | + a: int |
| 64 | + |
| 65 | + @define(frozen=True) |
| 66 | + class B: |
| 67 | + b: int |
| 68 | + |
| 69 | + collection_type = collection.type_factory(Union[A, B]) |
| 70 | + union = Union[collection_type, A, B] |
| 71 | + |
| 72 | + assert converter.structure({"a": 1}, union) == A(1) |
| 73 | + assert converter.structure({"a": 1}, Union[A, B]) == A(1) |
| 74 | + assert converter.structure({"a": 1}, A) == A(1) |
| 75 | + assert converter.structure({"b": 2}, union) == B(2) |
| 76 | + assert converter.structure({"b": 2}, Union[A, B]) == B(2) |
| 77 | + assert converter.structure({"b": 2}, B) == B(2) |
| 78 | + |
| 79 | + expected_structured = collection.factory([A(1), B(2)]) |
| 80 | + assert converter.structure([{"a": 1}, {"b": 2}], union) == expected_structured |
| 81 | + assert converter.structure([{"a": 1}, {"b": 2}], collection_type) == expected_structured |
| 82 | + assert converter.structure(deque([{"a": 1}, {"b": 2}]), union) == expected_structured |
| 83 | + assert converter.structure(deque([{"a": 1}, {"b": 2}]), collection_type) == expected_structured |
| 84 | + assert converter.structure(tuple([{"a": 1}, {"b": 2}]), union) == expected_structured |
| 85 | + assert converter.structure(tuple([{"a": 1}, {"b": 2}]), collection_type) == expected_structured |
0 commit comments