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
|
from dataclasses import dataclass, field
from email.policy import default
from importlib.metadata import metadata
from typing import Optional
import pytest
from typing_extensions import Annotated
from mashumaro import DataClassDictMixin, pass_through
from mashumaro.config import (
TO_DICT_ADD_BY_ALIAS_FLAG,
TO_DICT_ADD_OMIT_NONE_FLAG,
BaseConfig,
)
from mashumaro.exceptions import MissingField
from mashumaro.types import Alias
@dataclass
class Aliased(DataClassDictMixin):
a: int = field(metadata={"alias": "alias_a"})
b: Annotated[int, Alias("alias_b")]
@dataclass
class AliasedWithSerializeByAliasFlag(DataClassDictMixin):
a: int = field(metadata={"alias": "alias_a"})
b: Annotated[int, Alias("alias_b")]
class Config(BaseConfig):
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
def test_alias():
assert Aliased.from_dict({"alias_a": 123, "alias_b": 456}) == Aliased(
a=123, b=456
)
with pytest.raises(MissingField):
assert Aliased.from_dict({"a": 123, "alias_b": 456})
with pytest.raises(MissingField):
assert Aliased.from_dict({"alias_a": 123, "b": 456})
def test_alias_with_default():
@dataclass
class DataClass(DataClassDictMixin):
a: int = field(default=111, metadata={"alias": "alias_a"})
b: Annotated[int, Alias("alias_b")] = 222
assert DataClass.from_dict({"alias_a": 123, "alias_b": 456}) == DataClass(
a=123, b=456
)
assert DataClass.from_dict({}) == DataClass(a=111, b=222)
def test_alias_with_omit_none():
@dataclass
class DataClass(DataClassDictMixin):
a: Optional[int] = field(default=None, metadata={"alias": "alias_a"})
b: Annotated[Optional[int], Alias("alias_b")] = None
class Config(BaseConfig):
code_generation_options = [
TO_DICT_ADD_BY_ALIAS_FLAG,
TO_DICT_ADD_OMIT_NONE_FLAG,
]
instance = DataClass()
assert instance.to_dict(omit_none=True) == {}
assert instance.to_dict(by_alias=True) == {
"alias_a": None,
"alias_b": None,
}
assert instance.to_dict(omit_none=True, by_alias=True) == {}
instance = DataClass(a=123, b=456)
assert instance.to_dict(omit_none=True) == {"a": 123, "b": 456}
assert instance.to_dict(by_alias=True) == {"alias_a": 123, "alias_b": 456}
assert instance.to_dict(omit_none=True, by_alias=True) == {
"alias_a": 123,
"alias_b": 456,
}
def test_serialize_by_alias_config_option():
@dataclass
class DataClass(DataClassDictMixin):
a: int = field(metadata={"alias": "alias_a"})
b: Annotated[int, Alias("alias_b")]
class Config(BaseConfig):
serialize_by_alias = True
assert DataClass(123, 456).to_dict() == {"alias_a": 123, "alias_b": 456}
def test_serialize_by_alias_code_generation_flag():
@dataclass
class DataClass(DataClassDictMixin):
x: int = field(metadata={"alias": "alias_x"})
y: Annotated[int, Alias("alias_y")]
class Config(BaseConfig):
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
instance = DataClass(x=123, y=456)
assert instance.to_dict() == {"x": 123, "y": 456}
assert instance.to_dict(by_alias=True) == {"alias_x": 123, "alias_y": 456}
def test_serialize_by_alias_code_generation_flag_without_alias():
@dataclass
class DataClass(DataClassDictMixin):
x: int
class Config(BaseConfig):
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
instance = DataClass(x=123)
assert instance.to_dict() == {"x": 123}
assert instance.to_dict(by_alias=True) == {"x": 123}
def test_no_serialize_by_alias_code_generation_flag():
@dataclass
class DataClass(DataClassDictMixin):
x: int = field(metadata={"alias": "alias_x"})
y: Annotated[int, Alias("alias_y")]
instance = DataClass(x=123, y=456)
assert instance.to_dict() == {"x": 123, "y": 456}
with pytest.raises(TypeError):
instance.to_dict(by_alias=True)
def test_serialize_by_alias_flag_for_inner_class_without_it():
@dataclass
class DataClass(DataClassDictMixin):
x: Aliased = field(metadata={"alias": "alias_x"})
y: Annotated[Aliased, Alias("alias_y")]
class Config(BaseConfig):
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
instance = DataClass(Aliased(a=1, b=2), Aliased(a=3, b=4))
assert instance.to_dict() == {"x": {"a": 1, "b": 2}, "y": {"a": 3, "b": 4}}
assert instance.to_dict(by_alias=True) == {
"alias_x": {"a": 1, "b": 2},
"alias_y": {"a": 3, "b": 4},
}
def test_serialize_by_alias_flag_for_inner_class_with_it():
@dataclass
class DataClass(DataClassDictMixin):
x: AliasedWithSerializeByAliasFlag = field(
metadata={"alias": "alias_x"}
)
y: Annotated[AliasedWithSerializeByAliasFlag, Alias("alias_y")]
class Config(BaseConfig):
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
instance = DataClass(
AliasedWithSerializeByAliasFlag(a=1, b=2),
AliasedWithSerializeByAliasFlag(a=3, b=4),
)
assert instance.to_dict() == {"x": {"a": 1, "b": 2}, "y": {"a": 3, "b": 4}}
assert instance.to_dict(by_alias=True) == {
"alias_x": {"alias_a": 1, "alias_b": 2},
"alias_y": {"alias_a": 3, "alias_b": 4},
}
def test_aliases_in_config():
@dataclass
class DataClass(DataClassDictMixin):
a: int = 111
b: Annotated[int, Alias("alias_b")] = 222
class Config(BaseConfig):
aliases = {"a": "alias_a", "b": "alias_c"}
serialize_by_alias = True
assert DataClass.from_dict({"alias_a": 123, "alias_b": 456}) == DataClass(
a=123, b=456
)
assert DataClass.from_dict({}) == DataClass(a=111, b=222)
assert DataClass(a=123, b=456).to_dict() == {
"alias_a": 123,
"alias_b": 456,
}
def test_by_alias_with_serialize_by_alias():
@dataclass
class DataClass(DataClassDictMixin):
a: int = field(metadata={"alias": "alias_a"})
b: Annotated[int, Alias("alias_b")]
class Config(BaseConfig):
serialize_by_alias = True
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
instance = DataClass(a=123, b=456)
assert DataClass.from_dict({"alias_a": 123, "alias_b": 456}) == instance
assert instance.to_dict() == {"alias_a": 123, "alias_b": 456}
assert instance.to_dict(by_alias=False) == {"a": 123, "b": 456}
def test_no_serialize_by_alias_with_serialize_by_alias_and_optional():
@dataclass
class DataClass(DataClassDictMixin):
x: Optional[int] = field(metadata={"alias": "alias_x"})
y: Annotated[Optional[int], Alias("alias_y")]
class Config(BaseConfig):
serialize_by_alias = True
assert DataClass(x=123, y=456).to_dict() == {
"alias_x": 123,
"alias_y": 456,
}
assert DataClass(x=None, y=None).to_dict() == {
"alias_x": None,
"alias_y": None,
}
def test_by_field_with_allow_deserialization_not_by_alias():
@dataclass
class DataClass(DataClassDictMixin):
a1: int = field(metadata={"alias": "alias_a1"})
a2: Annotated[int, Alias("alias_a2")]
b1: Optional[int] = field(metadata={"alias": "alias_b1"})
b2: Annotated[Optional[int], Alias("alias_b2")]
c1: Optional[str] = field(metadata={"alias": "alias_c1"})
c2: Annotated[Optional[str], Alias("alias_c2")]
d1: int = field(
metadata={"alias": "alias_d1", "deserialize": pass_through}
)
d2: Annotated[int, Alias("alias_d2")] = field(
metadata={"deserialize": pass_through}
)
e1: int = field(metadata={"alias": "alias_e1"}, default=5)
e2: Annotated[int, Alias("alias_e2")] = 5
f1: Optional[int] = field(metadata={"alias": "alias_f1"}, default=6)
f2: Annotated[Optional[int], Alias("alias_f2")] = 6
g1: Optional[str] = field(metadata={"alias": "alias_g1"}, default="7")
g2: Annotated[Optional[str], Alias("alias_g2")] = "7"
h1: int = field(
metadata={"alias": "alias_h1", "deserialize": pass_through},
default=8,
)
h2: Annotated[int, Alias("alias_h2")] = field(
metadata={"deserialize": pass_through}, default=8
)
class Config(BaseConfig):
serialize_by_alias = True
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
allow_deserialization_not_by_alias = True
instance = DataClass(a1=1, a2=1, b1=2, b2=2, c1="3", c2="3", d1=4, d2=4)
assert (
DataClass.from_dict(
{
"a1": 1,
"a2": 1,
"b1": 2,
"b2": 2,
"c1": "3",
"c2": "3",
"d1": 4,
"d2": 4,
"e1": 5,
"e2": 5,
"f1": 6,
"f2": 6,
"g1": "7",
"g2": "7",
"h1": 8,
"h2": 8,
}
)
== instance
)
assert (
DataClass.from_dict(
{
"alias_a1": 1,
"alias_a2": 1,
"alias_b1": 2,
"alias_b2": 2,
"alias_c1": "3",
"alias_c2": "3",
"alias_d1": 4,
"alias_d2": 4,
"alias_e1": 5,
"alias_e2": 5,
"alias_f1": 6,
"alias_f2": 6,
"alias_g1": "7",
"alias_g2": "7",
"alias_h1": 8,
"alias_h2": 8,
}
)
== instance
)
assert instance.to_dict() == {
"alias_a1": 1,
"alias_a2": 1,
"alias_b1": 2,
"alias_b2": 2,
"alias_c1": "3",
"alias_c2": "3",
"alias_d1": 4,
"alias_d2": 4,
"alias_e1": 5,
"alias_e2": 5,
"alias_f1": 6,
"alias_f2": 6,
"alias_g1": "7",
"alias_g2": "7",
"alias_h1": 8,
"alias_h2": 8,
}
assert instance.to_dict(by_alias=False) == {
"a1": 1,
"a2": 1,
"b1": 2,
"b2": 2,
"c1": "3",
"c2": "3",
"d1": 4,
"d2": 4,
"e1": 5,
"e2": 5,
"f1": 6,
"f2": 6,
"g1": "7",
"g2": "7",
"h1": 8,
"h2": 8,
}
def test_order_of_metadata_and_annotated():
@dataclass
class DataClass(DataClassDictMixin):
x: Annotated[int, Alias("foo")] = field(metadata={"alias": "bar"})
class Config(BaseConfig):
serialize_by_alias = True
instance = DataClass(42)
assert DataClass.from_dict({"bar": 42}) == instance
assert instance.to_dict() == {"bar": 42}
|