77
88
99class ImportAlarmError (ImportError ):
10- """To be raised in addition to warning under test conditions """
10+ """To be raised instead of a warning when a package is missing. """
1111
1212
1313class ImportAlarm :
@@ -45,19 +45,26 @@ class ImportAlarm:
4545 >>> import_alarm.warn_if_failed()
4646 """
4747
48- def __init__ (self , message = None , _fail_on_warning : bool = False ):
48+ def __init__ (
49+ self ,
50+ message = None ,
51+ raise_exception : bool = False ,
52+ ):
4953 """
5054 Initialize message value.
5155
56+ If a `raise_exception` is `True`, raise a :class:`.ImportAlarmError`, which is a subclass of `ImportError`.
57+
5258 Args:
5359 message (str): What to say alongside your ImportError when the decorated
5460 function is called. (Default is None, which says nothing and raises no
5561 error.)
62+ raise_exception (bool, optional): raise an exception instead of issuing a warning
5663 """
5764 self .message = message
5865 # Catching warnings in tests can be janky, so instead open a flag for failing
5966 # instead.
60- self ._fail_on_warning = _fail_on_warning
67+ self .raise_exception = raise_exception
6168
6269 def __call__ (self , func ):
6370 return self .wrapper (func )
@@ -78,7 +85,7 @@ def warn_if_failed(self):
7885 """
7986 if self .message is not None :
8087 warnings .warn (self .message , category = ImportWarning , stacklevel = 2 )
81- if self ._fail_on_warning :
88+ if self .raise_exception :
8289 raise ImportAlarmError (self .message )
8390
8491 def __enter__ (self ):
0 commit comments