Deltares / xugrid

Xarray and unstructured grids
https://deltares.github.io/xugrid/
MIT License
61 stars 8 forks source link

catch error when passing empty list to `xu.merge_partitions()` #277

Closed veenstrajelmer closed 1 month ago

veenstrajelmer commented 1 month ago

When passing [] to xu.merge_partitions() it raises KeyError: 'pop from an empty set':

>> xu.merge_partitions() with 0 partition(s): Traceback (most recent call last):

  File ~\Anaconda3\envs\dfm_tools_env\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\data\dfm_tools\dfm_tools\untitled0.py:29
    uds = xu.merge_partitions(partitions)

  File ~\Anaconda3\envs\dfm_tools_env\Lib\site-packages\xugrid\ugrid\partitioning.py:336 in merge_partitions
    obj_type = types.pop()

KeyError: 'pop from an empty set'

This is a bit of a silly issue, but the usecase is using a file pattern that is expanded to a list of files. If there is no connection to P, or there is a typo in the file pattern, glob will generate an empty list. This is not catched in xu.merge_partitions(). Might be useful to add it, although I think this is the first time in a few years I have encountered it. So feel free to close this as "won't do".

Huite commented 1 month ago

Raising a ValueError is perfectly justified here, since the KeyError is difficult to understand for a typical user.