Skip to content

Commit

Permalink
UX: add information about missing optional dependencies as warnings a…
Browse files Browse the repository at this point in the history
…nd error messsages in yt.load
  • Loading branch information
neutrinoceros committed Dec 29, 2022
1 parent 7c81940 commit 64e1df1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
17 changes: 15 additions & 2 deletions yt/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
YTSimulationNotIdentified,
YTUnidentifiedDataType,
)
from yt.utilities.hierarchy_inspection import find_lowest_subclasses
from yt.utilities.hierarchy_inspection import (
find_lowest_subclasses,
get_classes_with_missing_requirements,
)
from yt.utilities.lib.misc_utilities import get_box_grids_level
from yt.utilities.logger import ytLogger as mylog
from yt.utilities.object_registries import (
Expand All @@ -42,6 +45,7 @@
# --- Loaders for known data formats ---


# FUTURE: embedded warnings need to have their stacklevel decremented when this decorator is removed
@future_positional_only({0: "fn"}, since="4.2.0")
def load(
fn: Union[str, "os.PathLike[str]"], *args, hint: Optional[str] = None, **kwargs
Expand Down Expand Up @@ -106,7 +110,16 @@ def load(
candidates = find_lowest_subclasses(candidates, hint=hint)

if len(candidates) == 1:
return candidates[0](fn, *args, **kwargs)
cls = candidates[0]
if any(missing := get_classes_with_missing_requirements().get(cls, [])):
warnings.warn(
f"This dataset appears to be of type {cls.__name__}, "
"but the following requirements are currently missing "
f"from your installation: {', '.join([_ for _ in missing])}\n"
"Please make sure to install these and try again.",
stacklevel=3,
)
return cls(fn, *args, **kwargs)

if len(candidates) > 1:
raise YTAmbiguousDataType(fn, candidates)
Expand Down
18 changes: 18 additions & 0 deletions yt/utilities/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@ def __init__(self, filename, *args, **kwargs):
self.kwargs = kwargs

def __str__(self):
from yt.utilities.hierarchy_inspection import (
get_classes_with_missing_requirements,
)

msg = f"Could not determine input format from {self.filename!r}"
if self.args:
msg += ", " + (", ".join(f"{a!r}" for a in self.args))
if self.kwargs:
msg += ", " + (", ".join(f"{k}={v!r}" for k, v in self.kwargs.items()))

msg += "\n"

if len(unusable_classes := get_classes_with_missing_requirements()) > 0:
msg += (
"The following types could not be thorougly checked against your data because "
"their requirements are missing. "
"You may want to inspect this list and check your installation:"
)
for cls, missing in unusable_classes.items():
requirements_str = ", ".join(missing)
msg += f"\n- {cls.__name__} (requires: {requirements_str})"
msg += "\n\n"
msg += "Please make sure you are running a sufficiently recent version of yt."
return msg


Expand Down

0 comments on commit 64e1df1

Please sign in to comment.