Skip to content

Commit 0a9712f

Browse files
authored
Merge pull request #61 from pyiron/fail
ImportAlarm: Expose _fail_on_warning and rename
2 parents ee314f9 + 38775f2 commit 0a9712f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

pyiron_snippets/import_alarm.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class 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

1313
class 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):

tests/unit/test_import_alarm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
class TestImportAlarm(unittest.TestCase):
77

88
def test_instance(self):
9-
no_alarm = ImportAlarm(_fail_on_warning=True)
9+
no_alarm = ImportAlarm(raise_exception=True)
1010

1111
@no_alarm
1212
def add_one(x):
1313
return x + 1
1414

15-
yes_alarm = ImportAlarm("Here is a message", _fail_on_warning=True)
15+
yes_alarm = ImportAlarm("Here is a message", raise_exception=True)
1616

1717
@yes_alarm
1818
def subtract_one(x):
@@ -37,11 +37,11 @@ def subtract_one(x):
3737
subtract_one(0)
3838

3939
def test_context(self):
40-
with ImportAlarm("Working import", _fail_on_warning=True) as alarm_working:
40+
with ImportAlarm("Working import", raise_exception=True) as alarm_working:
4141
# Suppose all the imports here pass fine
4242
pass
4343

44-
with ImportAlarm("Broken import", _fail_on_warning=True) as alarm_broken:
44+
with ImportAlarm("Broken import", raise_exception=True) as alarm_broken:
4545
raise ImportError("Suppose a package imported here is not available")
4646

4747
@alarm_working

0 commit comments

Comments
 (0)