/sunpy/util/exceptions.py

https://github.com/ayshih/sunpy · Python · 105 lines · 19 code · 19 blank · 67 comment · 0 complexity · 243e136ee0ab8aa3c350ee6529f52c7e MD5 · raw file

  1. """
  2. This module provides errors/exceptions and warnings of general use for SunPy.
  3. Exceptions that are specific to a given package should **not** be here,
  4. but rather in the particular package.
  5. """
  6. import warnings
  7. __all__ = ["NoMapsInFileError",
  8. "SunpyWarning", "SunpyUserWarning", "SunpyDeprecationWarning",
  9. "SunpyPendingDeprecationWarning", "SunpyMetadataWarning",
  10. "warn_user", "warn_deprecated", "warn_metadata"]
  11. class NoMapsInFileError(Exception):
  12. """
  13. An error raised when a file is opened and no maps are found.
  14. """
  15. class SunpyWarning(Warning):
  16. """
  17. The base warning class from which all Sunpy warnings should inherit.
  18. Any warning inheriting from this class is handled by the Sunpy
  19. logger. This warning should not be issued in normal code. Use
  20. "SunpyUserWarning" instead or a specific sub-class.
  21. """
  22. class SunpyUserWarning(UserWarning, SunpyWarning):
  23. """
  24. The primary warning class for Sunpy.
  25. Use this if you do not need a specific type of warning.
  26. """
  27. class SunpyMetadataWarning(UserWarning, SunpyWarning):
  28. """
  29. Warning class for cases metadata is missing.
  30. This does not inherit from SunpyWarning because we want to use
  31. stacklevel=3 to show the user where the issue occurred in their code.
  32. """
  33. class SunpyDeprecationWarning(FutureWarning, SunpyWarning):
  34. """
  35. A warning class to indicate a deprecated feature.
  36. """
  37. class SunpyPendingDeprecationWarning(PendingDeprecationWarning, SunpyWarning):
  38. """
  39. A warning class to indicate a soon-to-be deprecated feature.
  40. """
  41. def warn_metadata(msg, stacklevel=1):
  42. """
  43. Raise a `SunpyMetadataWarning`.
  44. Parameters
  45. ----------
  46. msg : str
  47. Warning message.
  48. stacklevel : int
  49. This is interpreted relative to the call to this function,
  50. e.g. ``stacklevel=1`` (the default) sets the stack level in the
  51. code that calls this function.
  52. """
  53. warnings.warn(msg, SunpyMetadataWarning, stacklevel + 1)
  54. def warn_user(msg, stacklevel=1):
  55. """
  56. Raise a `SunpyUserWarning`.
  57. Parameters
  58. ----------
  59. msg : str
  60. Warning message.
  61. stacklevel : int
  62. This is interpreted relative to the call to this function,
  63. e.g. ``stacklevel=1`` (the default) sets the stack level in the
  64. code that calls this function.
  65. """
  66. warnings.warn(msg, SunpyUserWarning, stacklevel + 1)
  67. def warn_deprecated(msg, stacklevel=1):
  68. """
  69. Raise a `SunpyDeprecationWarning`.
  70. Parameters
  71. ----------
  72. msg : str
  73. Warning message.
  74. stacklevel : int
  75. This is interpreted relative to the call to this function,
  76. e.g. ``stacklevel=1`` (the default) sets the stack level in the
  77. code that calls this function.
  78. """
  79. warnings.warn(msg, SunpyDeprecationWarning, stacklevel + 1)