2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2020-06-02 21:38:41 +08:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2020-07-09 00:04:56 +08:00
|
|
|
class S:
|
|
|
|
a: int
|
|
|
|
b: str
|
2020-06-02 21:38:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2020-07-09 00:04:56 +08:00
|
|
|
class C:
|
|
|
|
c: S
|
|
|
|
d: S
|
2020-06-02 21:38:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2020-07-09 00:04:56 +08:00
|
|
|
class C2:
|
|
|
|
e: C
|
|
|
|
f: S
|
2020-06-02 21:38:41 +08:00
|
|
|
|
|
|
|
|
2020-07-09 00:04:56 +08:00
|
|
|
@dataclass
|
|
|
|
class C3:
|
|
|
|
g: S
|
|
|
|
h: C2
|
|
|
|
i: str
|
|
|
|
j: str
|
2020-06-02 21:38:41 +08:00
|
|
|
|
2020-07-09 00:04:56 +08:00
|
|
|
|
|
|
|
def test_recursive_dataclasses():
|
|
|
|
left = C3(
|
2020-12-30 17:56:09 +08:00
|
|
|
S(10, "ten"),
|
|
|
|
C2(C(S(1, "one"), S(2, "two")), S(2, "three")),
|
|
|
|
"equal",
|
|
|
|
"left",
|
2020-06-02 21:38:41 +08:00
|
|
|
)
|
2020-07-09 00:04:56 +08:00
|
|
|
right = C3(
|
2020-12-30 17:56:09 +08:00
|
|
|
S(20, "xxx"),
|
|
|
|
C2(C(S(1, "one"), S(2, "yyy")), S(3, "three")),
|
|
|
|
"equal",
|
|
|
|
"right",
|
2020-06-02 21:38:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
assert left == right
|