Skip to content

Commit a3f7774

Browse files
hmaarrfkTomNicholasIllviljan
authored
Make list_chunkmanagers more resilient to broken entrypoints (#8736)
* Make list_chunkmanagers more resilient to broken entrypoints As I'm a developing my custom chunk manager, I'm often checking out between my development branch and production branch breaking the entrypoint. This made xarray impossible to import unless I re-ran `pip install -e . -vv` which is somewhat tiring. * Type hint untyped test function to appease mypy * Try to return something to help mypy --------- Co-authored-by: Tom Nicholas <[email protected]> Co-authored-by: Illviljan <[email protected]>
1 parent 11f89ec commit a3f7774

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

xarray/core/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,10 @@ def find_stack_level(test_mode=False) -> int:
11061106
return n
11071107

11081108

1109-
def emit_user_level_warning(message, category=None):
1109+
def emit_user_level_warning(message, category=None) -> None:
11101110
"""Emit a warning at the user level by inspecting the stack trace."""
11111111
stacklevel = find_stack_level()
1112-
warnings.warn(message, category=category, stacklevel=stacklevel)
1112+
return warnings.warn(message, category=category, stacklevel=stacklevel)
11131113

11141114

11151115
def consolidate_dask_from_array_kwargs(

xarray/namedarray/parallelcompat.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import numpy as np
1717

18+
from xarray.core.utils import emit_user_level_warning
1819
from xarray.namedarray.pycompat import is_chunked_array
1920

2021
if TYPE_CHECKING:
@@ -73,9 +74,15 @@ def load_chunkmanagers(
7374
) -> dict[str, ChunkManagerEntrypoint[Any]]:
7475
"""Load entrypoints and instantiate chunkmanagers only once."""
7576

76-
loaded_entrypoints = {
77-
entrypoint.name: entrypoint.load() for entrypoint in entrypoints
78-
}
77+
loaded_entrypoints = {}
78+
for entrypoint in entrypoints:
79+
try:
80+
loaded_entrypoints[entrypoint.name] = entrypoint.load()
81+
except ModuleNotFoundError as e:
82+
emit_user_level_warning(
83+
f"Failed to load chunk manager entrypoint {entrypoint.name} due to {e}. Skipping.",
84+
)
85+
pass
7986

8087
available_chunkmanagers = {
8188
name: chunkmanager()

xarray/tests/test_parallelcompat.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from importlib.metadata import EntryPoint
34
from typing import Any
45

56
import numpy as np
@@ -13,6 +14,7 @@
1314
get_chunked_array_type,
1415
guess_chunkmanager,
1516
list_chunkmanagers,
17+
load_chunkmanagers,
1618
)
1719
from xarray.tests import has_dask, requires_dask
1820

@@ -218,3 +220,13 @@ def test_raise_on_mixed_array_types(self, register_dummy_chunkmanager) -> None:
218220

219221
with pytest.raises(TypeError, match="received multiple types"):
220222
get_chunked_array_type(*[dask_arr, dummy_arr])
223+
224+
225+
def test_bogus_entrypoint() -> None:
226+
# Create a bogus entry-point as if the user broke their setup.cfg
227+
# or is actively developing their new chunk manager
228+
entry_point = EntryPoint(
229+
"bogus", "xarray.bogus.doesnotwork", "xarray.chunkmanagers"
230+
)
231+
with pytest.warns(UserWarning, match="Failed to load chunk manager"):
232+
assert len(load_chunkmanagers([entry_point])) == 0

0 commit comments

Comments
 (0)