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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
|
from dataclasses import dataclass
from datetime import date
from functools import partial
from typing import Generic, Tuple, TypeVar
try:
from typing import TypeVarTuple
except ImportError:
from typing_extensions import TypeVarTuple
try:
from typing import Unpack
except ImportError:
from typing_extensions import Unpack
import pytest
from mashumaro import DataClassDictMixin
from mashumaro.core.const import PY_311_MIN
# noinspection PyProtectedMember
from mashumaro.core.meta.helpers import (
_check_generic,
_flatten_type_args,
resolve_type_params,
type_name,
)
from mashumaro.core.meta.types.common import FieldContext, ValueSpec
# noinspection PyProtectedMember
from mashumaro.core.meta.types.pack import pack_tuple
# noinspection PyProtectedMember
from mashumaro.core.meta.types.unpack import unpack_tuple
from mashumaro.exceptions import MissingField
K = TypeVar("K")
V = TypeVar("V")
Ts = TypeVarTuple("Ts")
_type_name = partial(type_name, short=True)
@dataclass
class MyGenericDataClassTs(Generic[Unpack[Ts]], DataClassDictMixin):
x: Tuple[int, Unpack[Ts]]
class MyGenericTs(Generic[Unpack[Ts]]):
pass
class MyGenericTsK(Generic[Unpack[Ts], K]):
pass
def test_check_generic():
with pytest.raises(TypeError) as e:
_check_generic(object, (K, Unpack[Ts], Unpack[Ts]), (int,))
assert (
str(e.value)
== "Multiple unpacks are disallowed within a single type parameter "
"list for object"
)
with pytest.raises(TypeError) as e:
_check_generic(object, (K, V), (int,))
assert (
str(e.value) == f"Too few arguments for object; actual 1, expected 2"
)
with pytest.raises(TypeError) as e:
_check_generic(object, (K, Unpack[Ts], V), (int,))
assert (
str(e.value)
== "Too few arguments for object; actual 1, expected at least 2"
)
def test_dataclass_with_multiple_unpacks():
with pytest.raises(TypeError) as e:
@dataclass
class DataClass(DataClassDictMixin):
x: Tuple[Unpack[Tuple[int]], Unpack[Tuple[float]]]
typ_name = type_name(Tuple[Unpack[Tuple[int]], Unpack[Tuple[float]]])
assert (
str(e.value)
== "Multiple unpacks are disallowed within a single type parameter "
f"list for {typ_name}"
)
def test_dataclass_with_single_unpack_tuple():
@dataclass
class DataClass(DataClassDictMixin):
a: Tuple[Unpack[Tuple[int, ...]]]
b: Tuple[Unpack[Tuple[int, float, int]]]
c: Tuple[Unpack[Tuple[int]]]
d: Tuple[Unpack[Tuple[()]]]
obj = DataClass(
a=(1, 2, 3, 4, 5),
b=(1, 2.2, 3),
c=(1,),
d=(),
)
assert obj.to_dict() == {
"a": [1, 2, 3, 4, 5],
"b": [1, 2.2, 3],
"c": [1],
"d": [],
}
assert (
DataClass.from_dict(
{
"a": ["1", "2", "3", "4", 5.0],
"b": ["1", "2.2", "3"],
"c": ["1"],
"d": ["1", "2", "3"],
}
)
== obj
)
def test_dataclass_with_mixed_unpack_tuple_ellipsis():
@dataclass
class DataClass(DataClassDictMixin):
a: Tuple[float, Unpack[Tuple[int, ...]]]
b: Tuple[Unpack[Tuple[int, ...]], float]
c: Tuple[float, Unpack[Tuple[int, ...]], float]
obj = DataClass(
a=(1.1, 2, 3, 4, 5),
b=(1, 2, 3, 4, 5.5),
c=(1.1, 2, 3, 4, 5.5),
)
assert obj.to_dict() == {
"a": [1.1, 2, 3, 4, 5],
"b": [1, 2, 3, 4, 5.5],
"c": [1.1, 2, 3, 4, 5.5],
}
assert (
DataClass.from_dict(
{
"a": ["1.1", "2", "3", "4", 5.0],
"b": [1.0, "2", "3", "4", "5.5"],
"c": ["1.1", "2", "3", 4.0, 5.5],
}
)
== obj
)
def test_dataclass_with_mixed_unpack_tuple_multiple_args():
@dataclass
class DataClass(DataClassDictMixin):
a: Tuple[float, Unpack[Tuple[int, float, int]]]
b: Tuple[Unpack[Tuple[int, float, int]], float]
c: Tuple[float, Unpack[Tuple[int, float, int]], float]
obj = DataClass(
a=(1.1, 2, 3.3, 4),
b=(1, 2.2, 3, 4.4),
c=(1.1, 2, 3.3, 4, 5.5),
)
assert obj.to_dict() == {
"a": [1.1, 2, 3.3, 4],
"b": [1, 2.2, 3, 4.4],
"c": [1.1, 2, 3.3, 4, 5.5],
}
assert (
DataClass.from_dict(
{
"a": ["1.1", "2", 3.3, 4.0],
"b": [1.0, 2.2, "3", "4.4"],
"c": ["1.1", "2", 3.3, 4.0, "5.5"],
}
)
== obj
)
def test_dataclass_with_mixed_unpack_tuple_one_arg():
@dataclass
class DataClass(DataClassDictMixin):
a: Tuple[float, Unpack[Tuple[int]]]
b: Tuple[Unpack[Tuple[int]], float]
c: Tuple[float, Unpack[Tuple[int]], float]
obj = DataClass(
a=(1.1, 2),
b=(1, 2.2),
c=(1.1, 2, 3.3),
)
assert obj.to_dict() == {
"a": [1.1, 2],
"b": [1, 2.2],
"c": [1.1, 2, 3.3],
}
assert (
DataClass.from_dict(
{
"a": ["1.1", 2.0],
"b": [1.0, "2.2"],
"c": ["1.1", 2.0, 3.3],
}
)
== obj
)
def test_dataclass_with_mixed_unpack_empty_tuple():
@dataclass
class DataClass(DataClassDictMixin):
a: Tuple[float, Unpack[Tuple[()]]]
b: Tuple[Unpack[Tuple[()]], float]
c: Tuple[float, Unpack[Tuple[()]], float]
obj = DataClass(
a=(1.1,),
b=(1.1,),
c=(1.1, 2.2),
)
assert obj.to_dict() == {
"a": [1.1],
"b": [1.1],
"c": [1.1, 2.2],
}
assert (
DataClass.from_dict(
{
"a": ["1.1"],
"b": ["1.1"],
"c": ["1.1", "2.2"],
}
)
== obj
)
@pytest.mark.skipif(not PY_311_MIN, reason="requires python 3.11")
def test_type_name_for_unpacks_py_311():
assert _type_name(Tuple[Unpack[Tuple[int, ...]]]) == "Tuple[int, ...]"
assert _type_name(Tuple[Unpack[Tuple[int, float]]]) == "Tuple[int, float]"
assert (
_type_name(Tuple[int, Unpack[Tuple[float, ...]]])
== "Tuple[int, *Tuple[float, ...]]"
)
assert (
_type_name(Tuple[int, Unpack[Tuple[float, str]]])
== "Tuple[int, float, str]"
)
assert _type_name(Tuple[Unpack[Tuple[()]]]) == "Tuple[()]"
assert _type_name(Tuple[int, Unpack[Tuple[()]]]) == "Tuple[int]"
assert (
_type_name(Tuple[str, Unpack[Tuple[int, ...]], int])
== "Tuple[str, *Tuple[int, ...], int]"
)
assert (
_type_name(Tuple[str, Unpack[Tuple[Tuple[int], ...]], int])
== "Tuple[str, *Tuple[Tuple[int], ...], int]"
)
assert (
_type_name(
Tuple[str, Unpack[Tuple[Tuple[Unpack[Tuple[int]], ...]]], int]
)
== "Tuple[str, Tuple[int, ...], int]"
)
assert _type_name(Tuple[Unpack[Ts]]) == "Tuple[*Ts]"
assert _type_name(Tuple[int, Unpack[Ts], int]) == "Tuple[int, *Ts, int]"
assert _type_name(Generic[Unpack[Ts]]) == "Generic[*Ts]"
assert _type_name(Generic[K, Unpack[Ts], V]) == "Generic[Any, *Ts, Any]"
assert _type_name(Unpack[Tuple[int]]) == "int"
assert _type_name(Unpack[Tuple[int, float]]) == "int, float"
assert _type_name(Unpack[Ts]) == "*Ts"
assert _type_name(Ts) == "Ts"
assert (
_type_name(Tuple[Unpack[Ts], K][Unpack[Tuple[int, ...]]])
== "Tuple[*Tuple[int, ...], int]"
)
assert (
_type_name(Tuple[Unpack[Ts], K][Unpack[Tuple[int, ...]], date])
== "Tuple[*Tuple[int, ...], date]"
)
assert (
_type_name(Tuple[Unpack[Ts], K][date, Unpack[Tuple[int, ...]]])
== "Tuple[date, *Tuple[int, ...], int]"
)
@pytest.mark.skipif(PY_311_MIN, reason="requires python<3.11")
def test_type_name_for_unpacks_py_less_than_311():
assert _type_name(Tuple[Unpack[Tuple[int, ...]]]) == "Tuple[int, ...]"
assert _type_name(Tuple[Unpack[Tuple[int, float]]]) == "Tuple[int, float]"
assert (
_type_name(Tuple[int, Unpack[Tuple[float, ...]]])
== "Tuple[int, Unpack[Tuple[float, ...]]]"
)
assert (
_type_name(Tuple[int, Unpack[Tuple[float, str]]])
== "Tuple[int, float, str]"
)
assert _type_name(Tuple[Unpack[Tuple[()]]]) == "Tuple[()]"
assert _type_name(Tuple[int, Unpack[Tuple[()]]]) == "Tuple[int]"
assert (
_type_name(Tuple[str, Unpack[Tuple[int, ...]], int])
== "Tuple[str, Unpack[Tuple[int, ...]], int]"
)
assert (
_type_name(Tuple[str, Unpack[Tuple[Tuple[int], ...]], int])
== "Tuple[str, Unpack[Tuple[Tuple[int], ...]], int]"
)
assert (
_type_name(
Tuple[str, Unpack[Tuple[Tuple[Unpack[Tuple[int]], ...]]], int]
)
== "Tuple[str, Tuple[int, ...], int]"
)
assert _type_name(Tuple[Unpack[Ts]]) == "Tuple[Unpack[Ts]]"
assert (
_type_name(Tuple[int, Unpack[Ts], int])
== "Tuple[int, Unpack[Ts], int]"
)
assert _type_name(Generic[Unpack[Ts]]) == "Generic[Unpack[Ts]]"
assert (
_type_name(Generic[K, Unpack[Ts], V])
== "Generic[Any, Unpack[Ts], Any]"
)
assert _type_name(Unpack[Tuple[int]]) == "int"
assert _type_name(Unpack[Tuple[int, float]]) == "int, float"
assert _type_name(Unpack[Ts]) == "Unpack[Ts]"
assert _type_name(Ts) == "Ts"
# this doesn't work on python<3.11
# assert (
# _type_name(Tuple[Unpack[Ts], K][Unpack[Tuple[int, ...]]])
# == "Tuple[Unpack[Tuple[int, ...]], int]"
# )
# assert (
# _type_name(Tuple[Unpack[Ts], K][Unpack[Tuple[int, ...]], date])
# == "Tuple[Unpack[Tuple[int, ...]], date]"
# )
# assert (
# _type_name(Tuple[Unpack[Ts], K][date, Unpack[Tuple[int, ...]]])
# == "Tuple[date, Unpack[Tuple[int, ...]], int]"
# )
def test_concrete_generic_with_empty_tuple():
@dataclass
# I tried MyGenericDataClassTs[()] but [()] becomes just (). Impossible.
class ConcreteDataClass(MyGenericDataClassTs[Unpack[Tuple[()]]]):
pass
obj = ConcreteDataClass((1,))
assert obj.to_dict() == {"x": [1]}
assert ConcreteDataClass.from_dict({"x": [1]}) == obj
assert ConcreteDataClass.from_dict({"x": [1, 2, 3]}) == obj
def test_concrete_generic_with_variable_length_tuple_any():
@dataclass
class ConcreteDataClass(MyGenericDataClassTs):
pass
obj = ConcreteDataClass((1, "a", date(2022, 12, 17)))
assert obj.to_dict() == {"x": [1, "a", date(2022, 12, 17)]}
assert (
ConcreteDataClass.from_dict({"x": ["1", "a", date(2022, 12, 17)]})
== obj
)
def test_concrete_generic_with_replaced_tuple_with_one_arg():
@dataclass
class ConcreteDataClass(MyGenericDataClassTs[Unpack[Tuple[K]]]):
pass
obj = ConcreteDataClass((1, date(2022, 12, 17)))
assert obj.to_dict() == {"x": [1, date(2022, 12, 17)]}
assert (
ConcreteDataClass.from_dict({"x": ["1", date(2022, 12, 17), "a"]})
== obj
)
def test_concrete_generic_with_replaced_tuple_with_multiple_args():
@dataclass
class ConcreteDataClass(MyGenericDataClassTs[Unpack[Tuple[float, float]]]):
pass
obj = ConcreteDataClass((1, 2.2, 3.3))
assert obj.to_dict() == {"x": [1, 2.2, 3.3]}
assert ConcreteDataClass.from_dict({"x": ["1", "2.2", "3.3"]}) == obj
@pytest.mark.skipif(not PY_311_MIN, reason="doesn't work on py<3.11")
def test_with_int_float_tuple_and_any_at_the_end():
Ts1 = TypeVarTuple("Ts1")
Ts2 = TypeVarTuple("Ts2")
IntTuple = Tuple[int, Unpack[Ts1]]
IntFloatTuple = IntTuple[float, Unpack[Ts2]]
@dataclass
class DataClass(DataClassDictMixin):
x: IntFloatTuple
obj = DataClass((1, 2.2, "3", date(2022, 12, 17)))
assert obj.to_dict() == {"x": [1, 2.2, "3", date(2022, 12, 17)]}
assert (
DataClass.from_dict({"x": ["1", "2.2", "3", date(2022, 12, 17)]})
== obj
)
@pytest.mark.skipif(not PY_311_MIN, reason="doesn't work on py<3.11")
def test_with_int_floats_tuple():
Ts1 = TypeVarTuple("Ts1")
IntTuple = Tuple[int, Unpack[Ts1]]
IntFloatsTuple = IntTuple[Unpack[Tuple[float, ...]]]
@dataclass
class DataClass(DataClassDictMixin):
x: IntFloatsTuple
obj = DataClass((1, 2.2, 3.3, 4.4))
assert obj.to_dict() == {"x": [1, 2.2, 3.3, 4.4]}
assert DataClass.from_dict({"x": ["1", "2.2", "3.3", "4.4"]}) == obj
@pytest.mark.skipif(not PY_311_MIN, reason="doesn't work on py<3.11")
def test_splitting_arbitrary_length_tuples_1():
Elderberries = Tuple[Unpack[Ts], K]
@dataclass
class DataClass(DataClassDictMixin):
x: Elderberries[Unpack[Tuple[int, ...]]]
obj = DataClass((1, 2, 3))
assert obj.to_dict() == {"x": [1, 2, 3]}
assert DataClass.from_dict({"x": ["1", "2", "3"]}) == obj
obj = DataClass((1,))
assert obj.to_dict() == {"x": [1]}
assert DataClass.from_dict({"x": [1]}) == obj
def test_dataclass_with_splitting_arbitrary_length_tuples_1():
@dataclass
class GenericDataClass(Generic[Unpack[Ts], K], DataClassDictMixin):
x: Tuple[Unpack[Ts], K]
@dataclass
class ConcreteDataClass(GenericDataClass[Unpack[Tuple[int, ...]]]):
pass
obj = ConcreteDataClass((1, 2, 3))
assert obj.to_dict() == {"x": [1, 2, 3]}
assert ConcreteDataClass.from_dict({"x": ["1", "2", "3"]}) == obj
obj = ConcreteDataClass((1,))
assert obj.to_dict() == {"x": [1]}
assert ConcreteDataClass.from_dict({"x": [1]}) == obj
@pytest.mark.skipif(not PY_311_MIN, reason="doesn't work on py<3.11")
def test_splitting_arbitrary_length_tuples_2():
Elderberries = Tuple[Unpack[Ts], K]
@dataclass
class DataClass(DataClassDictMixin):
x: Elderberries[Unpack[Tuple[int, ...]], date]
obj = DataClass((1, 2, date(2022, 12, 17)))
assert obj.to_dict() == {"x": [1, 2, "2022-12-17"]}
assert DataClass.from_dict({"x": ["1", "2", "2022-12-17"]}) == obj
obj = DataClass((date(2022, 12, 17),))
assert obj.to_dict() == {"x": ["2022-12-17"]}
assert DataClass.from_dict({"x": ["2022-12-17"]}) == obj
def test_dataclass_with_splitting_arbitrary_length_tuples_2():
@dataclass
class GenericDataClass(Generic[Unpack[Ts], K], DataClassDictMixin):
x: Tuple[Unpack[Ts], K]
@dataclass
class ConcreteDataClass(GenericDataClass[Unpack[Tuple[int, ...]], date]):
pass
obj = ConcreteDataClass((1, 2, date(2022, 12, 17)))
assert obj.to_dict() == {"x": [1, 2, "2022-12-17"]}
assert ConcreteDataClass.from_dict({"x": ["1", "2", "2022-12-17"]}) == obj
obj = ConcreteDataClass((date(2022, 12, 17),))
assert obj.to_dict() == {"x": ["2022-12-17"]}
assert ConcreteDataClass.from_dict({"x": ["2022-12-17"]}) == obj
@pytest.mark.skipif(not PY_311_MIN, reason="doesn't work on py<3.11")
def test_splitting_arbitrary_length_tuples_3():
Elderberries = Tuple[Unpack[Ts], K]
@dataclass
class DataClass(DataClassDictMixin):
x: Elderberries[date, Unpack[Tuple[int, ...]]]
obj = DataClass((date(2022, 12, 17), 1, 2, 3))
assert obj.to_dict() == {"x": ["2022-12-17", 1, 2, 3]}
assert DataClass.from_dict({"x": ["2022-12-17", "1", "2", "3"]}) == obj
obj = DataClass((date(2022, 12, 17), 1))
assert obj.to_dict() == {"x": ["2022-12-17", 1]}
assert DataClass.from_dict({"x": ["2022-12-17", "1"]}) == obj
def test_dataclass_with_splitting_arbitrary_length_tuples_3():
@dataclass
class GenericDataClass(Generic[Unpack[Ts], K], DataClassDictMixin):
x: Tuple[Unpack[Ts], K]
@dataclass
class ConcreteDataClass(GenericDataClass[date, Unpack[Tuple[int, ...]]]):
pass
class Config:
debug = True
obj = ConcreteDataClass((date(2022, 12, 17), 1, 2))
assert obj.to_dict() == {"x": ["2022-12-17", 1, 2]}
assert ConcreteDataClass.from_dict({"x": ["2022-12-17", "1", "2"]}) == obj
obj = ConcreteDataClass((date(2022, 12, 17), 1))
assert obj.to_dict() == {"x": ["2022-12-17", 1]}
assert ConcreteDataClass.from_dict({"x": ["2022-12-17", 1]}) == obj
def test_resolve_type_params_with_unpacks():
assert resolve_type_params(MyGenericTsK, [int, float], False) == {
MyGenericTsK: {K: float, Unpack[Ts]: Unpack[Tuple[int]]}
}
assert resolve_type_params(MyGenericTsK, [int, str, float], False) == {
MyGenericTsK: {K: float, Unpack[Ts]: Unpack[Tuple[int, str]]}
}
assert resolve_type_params(
MyGenericTsK, [Unpack[Tuple[int, str]], float], False
) == {MyGenericTsK: {K: float, Unpack[Ts]: Unpack[Tuple[int, str]]}}
assert resolve_type_params(
MyGenericTsK, [Unpack[Tuple[int, ...]]], False
) == {MyGenericTsK: {K: int, Unpack[Ts]: Unpack[Tuple[int, ...]]}}
assert resolve_type_params(
MyGenericTsK, [str, Unpack[Tuple[int, ...]]], False
) == {
MyGenericTsK: {
K: int,
Unpack[Ts]: Unpack[Tuple[str, Unpack[Tuple[int, ...]]]],
}
}
assert resolve_type_params(MyGenericTs, [()], False) == {
MyGenericTs: {Unpack[Ts]: Unpack[Tuple[()]]}
}
def test_dataclass_with_tuple_int_and_empty():
@dataclass
class ConcreteDataClass(MyGenericDataClassTs[Unpack[Tuple[()]]]):
pass
obj = ConcreteDataClass((1,))
assert obj.to_dict() == {"x": [1]}
assert ConcreteDataClass.from_dict({"x": [1]}) == obj
assert ConcreteDataClass.from_dict({"x": [1, 2, 3]}) == obj
with pytest.raises(MissingField):
ConcreteDataClass.from_dict({})
def test_unpack_tuple_with_multiple_unpacks():
spec = ValueSpec(
type=Tuple,
expression="value",
builder=object,
field_ctx=FieldContext("x", {}),
)
with pytest.raises(TypeError):
unpack_tuple(spec, (Unpack[Tuple[int]], Unpack[Tuple[float]]))
with pytest.raises(TypeError):
pack_tuple(spec, (Unpack[Tuple[int]], Unpack[Tuple[float]]))
def test_flatten_type_args_with_empty_tuple():
assert _flatten_type_args([Unpack[Tuple[()]]]) == [()]
assert _flatten_type_args([int, Unpack[Tuple[()]]]) == [int]
assert _flatten_type_args([Unpack[tuple[()]]]) == [()]
assert _flatten_type_args([int, Unpack[tuple[()]]]) == [int]
|