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
s_instance = S(42, "answer")

C

A dataclass containing two fields, both of type `S`.

@dataclass
class C:
    c: S
    d: S
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
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
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
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


Interaction with Other Parts of the System


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.