Skip to content

declearn.test_utils.assert_list_equal

Assert that two (possibly nested) lists are equal.

This function is a more complex equivalent of assert list_a == list_b that enables comparing numpy array values, and optionally accepting to cast tuples as lists rather than assert that a tuple and a list are not equal in any case (even when their contents are the same).

Parameters:

Name Type Description Default
list_a Union[Tuple[Any], List[Any]]

First list to compare.

required
list_b Union[Tuple[Any], List[Any]]

Second list to compare.

required
strict_tuple bool

Whether to cast tuples to list prior to comparing them (enabling some tuple-list type differences between the two compared lists).

False
np_tolerance Optional[float]

Optional absolute tolerance to numpy arrays or float values' differences (use np.allclose(a, b, rtol=0, atol=np_tolerance)).

None

Raises:

Type Description
AssertionError

If the two lists are not equal.

Source code in declearn/test_utils/_assertions.py
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
def assert_list_equal(
    list_a: Union[Tuple[Any], List[Any]],
    list_b: Union[Tuple[Any], List[Any]],
    strict_tuple: bool = False,
    np_tolerance: Optional[float] = None,
) -> None:
    """Assert that two (possibly nested) lists are equal.

    This function is a more complex equivalent of `assert list_a == list_b`
    that enables comparing numpy array values, and optionally accepting to
    cast tuples as lists rather than assert that a tuple and a list are not
    equal in any case (even when their contents are the same).

    Parameters
    ----------
    list_a: list
        First list to compare.
    list_b: list
        Second list to compare.
    strict_tuple: bool, default=False
        Whether to cast tuples to list prior to comparing them
        (enabling some tuple-list type differences between the
        two compared lists).
    np_tolerance: float or none, default=None
        Optional absolute tolerance to numpy arrays or float values'
        differences (use `np.allclose(a, b, rtol=0, atol=np_tolerance)`).

    Raises
    ------
    AssertionError
        If the two lists are not equal.
    """
    assert len(list_a) == len(list_b)
    for val_a, val_b in zip(list_a, list_b):
        assert_values_equal(val_a, val_b, strict_tuple, np_tolerance)