exceptions.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Artifact exceptions."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING, TypeVar
  4. from wandb import errors
  5. from wandb._strutils import nameof
  6. if TYPE_CHECKING:
  7. from wandb.sdk.artifacts.artifact import Artifact
  8. ArtifactT = TypeVar("ArtifactT", bound=Artifact)
  9. class ArtifactStatusError(AttributeError):
  10. """Raised when an artifact is in an invalid state for the requested operation."""
  11. def __init__(
  12. self,
  13. msg: str = "Artifact is in an invalid state for the requested operation.",
  14. name: str | None = None,
  15. obj: ArtifactT | None = None,
  16. ):
  17. # Follow AttributeError (Python 3.10+) by exposing `name` and `obj`.
  18. # See: https://docs.python.org/3/library/exceptions.html#AttributeError
  19. try:
  20. super().__init__(msg, name=name, obj=obj)
  21. except TypeError:
  22. # The `name`/`obj` keywords were only added in Python >= 3.10.
  23. super().__init__(msg)
  24. self.name = name or ""
  25. self.obj = obj
  26. class ArtifactNotLoggedError(ArtifactStatusError):
  27. """Raised for Artifact methods or attributes only available after logging."""
  28. def __init__(self, fullname: str, obj: ArtifactT):
  29. *_, name = fullname.split(".")
  30. msg = (
  31. f"{fullname!r} used prior to logging artifact or while in offline mode. "
  32. f"Call {nameof(obj.wait)}() before accessing logged artifact properties."
  33. )
  34. super().__init__(msg=msg, name=name, obj=obj)
  35. class ArtifactFinalizedError(ArtifactStatusError):
  36. """Raised for Artifact methods or attributes that can't be changed after logging."""
  37. def __init__(self, fullname: str, obj: ArtifactT):
  38. *_, name = fullname.split(".")
  39. msg = f"{fullname!r} used on logged artifact. Can't modify finalized artifact."
  40. super().__init__(msg=msg, name=name, obj=obj)
  41. class WaitTimeoutError(errors.Error):
  42. """Raised when wait() timeout occurs before process is finished."""
  43. class TooFewItemsError(ValueError):
  44. """Raised when there are fewer items than expected in a collection.
  45. Intended for internal use only.
  46. """
  47. class TooManyItemsError(ValueError):
  48. """Raised when there are more items than expected in a collection.
  49. Intended for internal use only.
  50. """