|
| 1 | +"""Tests for `typing.Self`.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import NamedTuple, Optional, TypedDict |
| 5 | + |
| 6 | +from attrs import define |
| 7 | +from typing_extensions import Self |
| 8 | + |
| 9 | +from cattrs import Converter |
| 10 | +from cattrs.cols import ( |
| 11 | + namedtuple_dict_structure_factory, |
| 12 | + namedtuple_dict_unstructure_factory, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@define |
| 17 | +class WithSelf: |
| 18 | + myself: Optional[Self] |
| 19 | + myself_with_default: Optional[Self] = None |
| 20 | + |
| 21 | + |
| 22 | +@define |
| 23 | +class WithSelfSubclass(WithSelf): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class WithSelfDataclass: |
| 29 | + myself: Optional[Self] |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class WithSelfDataclassSubclass(WithSelfDataclass): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +@define |
| 38 | +class WithListOfSelf: |
| 39 | + myself: Optional[Self] |
| 40 | + selves: list[WithSelf] |
| 41 | + |
| 42 | + |
| 43 | +class WithSelfTypedDict(TypedDict): |
| 44 | + field: int |
| 45 | + myself: Optional[Self] |
| 46 | + |
| 47 | + |
| 48 | +class WithSelfNamedTuple(NamedTuple): |
| 49 | + myself: Optional[Self] |
| 50 | + |
| 51 | + |
| 52 | +def test_self_roundtrip(genconverter): |
| 53 | + """A simple roundtrip works.""" |
| 54 | + initial = WithSelf(WithSelf(None, WithSelf(None))) |
| 55 | + raw = genconverter.unstructure(initial) |
| 56 | + |
| 57 | + assert raw == { |
| 58 | + "myself": { |
| 59 | + "myself": None, |
| 60 | + "myself_with_default": {"myself": None, "myself_with_default": None}, |
| 61 | + }, |
| 62 | + "myself_with_default": None, |
| 63 | + } |
| 64 | + |
| 65 | + assert genconverter.structure(raw, WithSelf) == initial |
| 66 | + |
| 67 | + |
| 68 | +def test_self_roundtrip_dataclass(genconverter): |
| 69 | + """A simple roundtrip works for dataclasses.""" |
| 70 | + initial = WithSelfDataclass(WithSelfDataclass(None)) |
| 71 | + raw = genconverter.unstructure(initial) |
| 72 | + |
| 73 | + assert raw == {"myself": {"myself": None}} |
| 74 | + |
| 75 | + assert genconverter.structure(raw, WithSelfDataclass) == initial |
| 76 | + |
| 77 | + |
| 78 | +def test_self_roundtrip_typeddict(genconverter): |
| 79 | + """A simple roundtrip works for TypedDicts.""" |
| 80 | + genconverter.register_unstructure_hook(int, str) |
| 81 | + |
| 82 | + initial: WithSelfTypedDict = {"field": 1, "myself": {"field": 2, "myself": None}} |
| 83 | + raw = genconverter.unstructure(initial) |
| 84 | + |
| 85 | + assert raw == {"field": "1", "myself": {"field": "2", "myself": None}} |
| 86 | + |
| 87 | + assert genconverter.structure(raw, WithSelfTypedDict) == initial |
| 88 | + |
| 89 | + |
| 90 | +def test_self_roundtrip_namedtuple(genconverter): |
| 91 | + """A simple roundtrip works for NamedTuples.""" |
| 92 | + genconverter.register_unstructure_hook_factory( |
| 93 | + lambda t: t is WithSelfNamedTuple, namedtuple_dict_unstructure_factory |
| 94 | + ) |
| 95 | + genconverter.register_structure_hook_factory( |
| 96 | + lambda t: t is WithSelfNamedTuple, namedtuple_dict_structure_factory |
| 97 | + ) |
| 98 | + |
| 99 | + initial = WithSelfNamedTuple(WithSelfNamedTuple(None)) |
| 100 | + raw = genconverter.unstructure(initial) |
| 101 | + |
| 102 | + assert raw == {"myself": {"myself": None}} |
| 103 | + |
| 104 | + assert genconverter.structure(raw, WithSelfNamedTuple) == initial |
| 105 | + |
| 106 | + |
| 107 | +def test_subclass_roundtrip(genconverter): |
| 108 | + """A simple roundtrip works for a dataclass subclass.""" |
| 109 | + initial = WithSelfSubclass(WithSelfSubclass(None)) |
| 110 | + raw = genconverter.unstructure(initial) |
| 111 | + |
| 112 | + assert raw == { |
| 113 | + "myself": {"myself": None, "myself_with_default": None}, |
| 114 | + "myself_with_default": None, |
| 115 | + } |
| 116 | + |
| 117 | + assert genconverter.structure(raw, WithSelfSubclass) == initial |
| 118 | + |
| 119 | + |
| 120 | +def test_subclass_roundtrip_dataclass(genconverter): |
| 121 | + """A simple roundtrip works for a dataclass subclass.""" |
| 122 | + initial = WithSelfDataclassSubclass(WithSelfDataclassSubclass(None)) |
| 123 | + raw = genconverter.unstructure(initial) |
| 124 | + |
| 125 | + assert raw == {"myself": {"myself": None}} |
| 126 | + |
| 127 | + assert genconverter.structure(raw, WithSelfDataclassSubclass) == initial |
| 128 | + |
| 129 | + |
| 130 | +def test_nested_roundtrip(genconverter: Converter): |
| 131 | + """A more complex roundtrip, with several Self classes.""" |
| 132 | + initial = WithListOfSelf(WithListOfSelf(None, []), [WithSelf(WithSelf(None))]) |
| 133 | + raw = genconverter.unstructure(initial) |
| 134 | + |
| 135 | + assert raw == { |
| 136 | + "myself": {"myself": None, "selves": []}, |
| 137 | + "selves": [ |
| 138 | + { |
| 139 | + "myself": {"myself": None, "myself_with_default": None}, |
| 140 | + "myself_with_default": None, |
| 141 | + } |
| 142 | + ], |
| 143 | + } |
| 144 | + |
| 145 | + assert genconverter.structure(raw, WithListOfSelf) == initial |
0 commit comments