test_compare_recursive_dataclasses.py
Overview
This file defines a set of nested Python dataclasses and a single test function that compares instances of these recursive dataclasses using the standard equality operator (`==`). The purpose is to demonstrate and verify how Python's built-in dataclass equality comparison works when dealing with nested dataclasses that contain other dataclasses as fields.
The test function `test_recursive_dataclasses` creates two deeply nested dataclass instances with similar but not identical field values and asserts their equality. The result highlights that dataclass equality is strictly structural and field-by-field, but does not perform any custom or fuzzy comparison.
Classes
S
A simple dataclass representing a structure with two fields.
@dataclass
class S:
a: int
b: str
Properties:
a(int): An integer value.b(str): A string value.
Usage example:
s_instance = S(42, "answer")
C
A dataclass containing two fields, both of type `S`.
@dataclass
class C:
c: S
d: S
Properties:
c(S): An instance ofS.d(S): Another instance ofS.
Usage example:
c_instance = C(S(1, "one"), S(2, "two"))
C2
A dataclass containing one `C` instance and one `S` instance.
@dataclass
class C2:
e: C
f: S
Properties:
e(C): An instance ofC.f(S): An instance ofS.
Usage example:
c2_instance = C2(C(S(1, "one"), S(2, "two")), S(3, "three"))
C3
A more complex dataclass containing multiple nested dataclasses and string fields.
@dataclass
class C3:
g: S
h: C2
i: str
j: str
Properties:
g(S): An instance ofS.h(C2): An instance ofC2.i(str): A string field.j(str): Another string field.
Usage example:
c3_instance = C3(S(10, "ten"), C2(C(S(1, "one"), S(2, "two")), S(2, "three")), "equal", "left")
Functions
test_recursive_dataclasses()
A test function that creates two instances of `C3` with nested `S`, `C`, and `C2` objects and asserts their equality.
def test_recursive_dataclasses():
left = C3(
S(10, "ten"),
C2(C(S(1, "one"), S(2, "two")), S(2, "three")),
"equal",
"left",
)
right = C3(
S(20, "xxx"),
C2(C(S(1, "one"), S(2, "yyy")), S(3, "three")),
"equal",
"right",
)
assert left == right
Parameters: None
Returns: None
Behavior:
Constructs two
C3instances (leftandright), differing in some nested field values.Uses the built-in dataclass
__eq__method to compare the two instances.The assertion
left == rightwill raise anAssertionErrorbecause the instances differ in multiple fields (g,h.e.d.b,h.f.a, andj).
Usage example:
test_recursive_dataclasses() # Will raise AssertionError because left != right
*Note:* The provided code as-is will cause an assertion failure since the two instances are not equal. If the intent is to show equality, the test data should be aligned accordingly.
Implementation Details
Dataclass equality: Python dataclasses automatically implement
__eq__by comparing every field recursively. This file leverages that behavior to compare nested dataclasses.Nested structures: Multiple levels of nested dataclasses show how equality checking cascades down through all contained objects.
No custom equality: No
__eq__methods are overridden, so default structural equality is used.Test design: The test creates two complex objects with subtle differences to demonstrate how the equality operator behaves with nested dataclasses.
Interaction with Other Parts of the System
This file appears to be a standalone test or example focused on dataclass comparison.
It does not import or export functionality outside of the standard library (
dataclasses).Could be used as a unit test in a larger codebase to verify dataclass comparison logic or as a learning tool.
No external dependencies or side effects.
Mermaid Class Diagram
classDiagram
class S {
+a: int
+b: str
}
class C {
+c: S
+d: S
}
class C2 {
+e: C
+f: S
}
class C3 {
+g: S
+h: C2
+i: str
+j: str
}
C o-- S : c,d
C2 o-- C : e
C2 o-- S : f
C3 o-- S : g
C3 o-- C2 : h
Summary
This file, `test_compare_recursive_dataclasses.py`, provides a clear demonstration of Python dataclasses' recursive equality checks by defining a hierarchy of nested dataclasses and a test function that compares two complex instances. It highlights the built-in behavior of dataclasses for deep equality without any custom logic, serving as a useful reference or test case for dataclass comparison semantics.