Skip to content

Make list_chunkmanagers more resilient to broken entrypoints #8736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,10 @@ def find_stack_level(test_mode=False) -> int:
return n


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


def consolidate_dask_from_array_kwargs(
Expand Down
13 changes: 10 additions & 3 deletions xarray/namedarray/parallelcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import numpy as np

from xarray.core.utils import emit_user_level_warning
from xarray.namedarray.pycompat import is_chunked_array

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

loaded_entrypoints = {
entrypoint.name: entrypoint.load() for entrypoint in entrypoints
}
loaded_entrypoints = {}
for entrypoint in entrypoints:
try:
loaded_entrypoints[entrypoint.name] = entrypoint.load()
except ModuleNotFoundError as e:
emit_user_level_warning(
f"Failed to load chunk manager entrypoint {entrypoint.name} due to {e}. Skipping.",
)
pass

available_chunkmanagers = {
name: chunkmanager()
Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_parallelcompat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from importlib.metadata import EntryPoint
from typing import Any

import numpy as np
Expand All @@ -13,6 +14,7 @@
get_chunked_array_type,
guess_chunkmanager,
list_chunkmanagers,
load_chunkmanagers,
)
from xarray.tests import has_dask, requires_dask

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

with pytest.raises(TypeError, match="received multiple types"):
get_chunked_array_type(*[dask_arr, dummy_arr])


def test_bogus_entrypoint() -> None:
# Create a bogus entry-point as if the user broke their setup.cfg
# or is actively developing their new chunk manager
entry_point = EntryPoint(
"bogus", "xarray.bogus.doesnotwork", "xarray.chunkmanagers"
)
with pytest.warns(UserWarning, match="Failed to load chunk manager"):
assert len(load_chunkmanagers([entry_point])) == 0