Skip to content

Commit 952a5d2

Browse files
authored
Enhance assert_list_element_type with optional empty list handling (project-chip#37453)
- Add optional `allow_empty` parameter to control empty list validation - Reorder function parameters for better readability - Update function docstring to clarify new behavior - Update test cases
1 parent aea195c commit 952a5d2

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/python_testing/matter_testing_infrastructure/chip/testing/matter_asserts.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,24 @@ def assert_list(value: Any, description: str, min_length: Optional[int] = None,
158158
f"{description} must not exceed {max_length} elements")
159159

160160

161-
def assert_list_element_type(value: List[Any], description: str, expected_type: Type[T]) -> None:
161+
def assert_list_element_type(value: List[Any], expected_type: Type[T], description: str, allow_empty: bool = False) -> None:
162162
"""
163163
Asserts that all elements in the list are of the expected type.
164164
165165
Args:
166166
value: The list to validate
167-
description: User-defined description for error messages
168167
expected_type: The type that all elements should match
168+
description: User-defined description for error messages
169+
allow_empty: If False, raises AssertionError for empty lists (default: False)
169170
170171
Raises:
171-
AssertionError: If value is not a list or contains elements of wrong type
172+
AssertionError: If value is not a list, contains elements of wrong type,
173+
or is empty when allow_empty=False
174+
TypeError: If expected_type is not a valid type
172175
"""
173176
assert_list(value, description)
177+
if not allow_empty and not value:
178+
asserts.fail(f"{description} must not be empty")
174179
for i, item in enumerate(value):
175180
asserts.assert_true(isinstance(item, expected_type),
176181
f"{description}[{i}] must be of type {expected_type.__name__}")

src/python_testing/matter_testing_infrastructure/chip/testing/test_matter_asserts.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,17 @@ def test_assert_list(self):
167167
def test_assert_list_element_type(self):
168168
"""Test assert_list_element_type with valid and invalid values."""
169169
# Valid cases
170-
matter_asserts.assert_list_element_type([], "test_empty", str)
171-
matter_asserts.assert_list_element_type(["a", "b"], "test_strings", str)
172-
matter_asserts.assert_list_element_type([1, 2, 3], "test_ints", int)
170+
matter_asserts.assert_list_element_type(["a", "b"], str, "test_strings")
171+
matter_asserts.assert_list_element_type([1, 2, 3], int, "test_ints")
172+
matter_asserts.assert_list_element_type([], str, "test_empty", allow_empty=True)
173173

174174
# Invalid cases
175175
with self.assertRaises(signals.TestFailure):
176-
matter_asserts.assert_list_element_type("not_a_list", "test_not_list", str)
176+
matter_asserts.assert_list_element_type("not_a_list", str, "test_not_list")
177177
with self.assertRaises(signals.TestFailure):
178-
matter_asserts.assert_list_element_type([1, "2", 3], "test_mixed", int)
178+
matter_asserts.assert_list_element_type([1, "2", 3], int, "test_mixed")
179+
with self.assertRaises(signals.TestFailure):
180+
matter_asserts.assert_list_element_type([], str, "test_empty") # empty list should fail by default
179181

180182
# String assertion tests
181183
def test_assert_is_string(self):

0 commit comments

Comments
 (0)