Skip to content

Commit

Permalink
avoid "object of type generator has no len()" by checking len of type…
Browse files Browse the repository at this point in the history
…s instead
  • Loading branch information
veenstrajelmer committed Aug 14, 2024
1 parent dce6dd7 commit 0158927
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions tests/test_partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ def test_labels_to_indices():
assert np.array_equal(indices[2], [3, 4])


def test_merge_partitions_empty_list():
with pytest.raises(
ValueError, message="Received empty partitions list, cannot be merged."
):
xu.merge_partitions([])


class TestGridPartitioning:
@pytest.fixture(autouse=True)
def setup(self):
Expand Down Expand Up @@ -169,6 +162,11 @@ def test_merge_partitions__errors(self):
):
pt.merge_partitions(partitions)

with pytest.raises(
ValueError, match="Received empty partitions list, cannot be merged."
):
xu.merge_partitions([])

def test_merge_partitions_no_duplicates(self):
part1 = self.uds.isel(mesh2d_nFaces=[0, 1, 2, 3])
part2 = self.uds.isel(mesh2d_nFaces=[2, 3, 4, 5])
Expand Down
4 changes: 2 additions & 2 deletions xugrid/ugrid/partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ def merge_partitions(partitions, merge_ugrid_chunks: bool = True):
-------
merged : UgridDataset
"""
if len(partitions) == 0:
raise ValueError("Received empty partitions list, cannot be merged.")
types = {type(obj) for obj in partitions}
msg = "Expected UgridDataArray or UgridDataset, received: {}"
if len(types) == 0:
raise ValueError("Received empty partitions list, cannot be merged.")
if len(types) > 1:
type_names = [t.__name__ for t in types]
raise TypeError(msg.format(type_names))
Expand Down

0 comments on commit 0158927

Please sign in to comment.