Context for use in testing to ensure that all warnings are raised. Examples -------- >>> import warnings >>> def foo(): ... warnings.warn(RuntimeWarning("bar")) We raise the warning once, while the warning filter is set to "once". Hereafter, the warning is invis
()
| 15 | |
| 16 | @contextmanager |
| 17 | def all_warnings(): |
| 18 | """ |
| 19 | Context for use in testing to ensure that all warnings are raised. |
| 20 | Examples |
| 21 | -------- |
| 22 | >>> import warnings |
| 23 | >>> def foo(): |
| 24 | ... warnings.warn(RuntimeWarning("bar")) |
| 25 | |
| 26 | We raise the warning once, while the warning filter is set to "once". |
| 27 | Hereafter, the warning is invisible, even with custom filters: |
| 28 | >>> with warnings.catch_warnings(): |
| 29 | ... warnings.simplefilter('once') |
| 30 | ... foo() |
| 31 | |
| 32 | We can now run ``foo()`` without a warning being raised: |
| 33 | >>> from numpy.testing import assert_warns # doctest: +SKIP |
| 34 | >>> foo() # doctest: +SKIP |
| 35 | |
| 36 | To catch the warning, we call in the help of ``all_warnings``: |
| 37 | >>> with all_warnings(): # doctest: +SKIP |
| 38 | ... assert_warns(RuntimeWarning, foo) |
| 39 | """ |
| 40 | |
| 41 | # Whenever a warning is triggered, Python adds a __warningregistry__ |
| 42 | # member to the *calling* module. The exercise here is to find |
| 43 | # and eradicate all those breadcrumbs that were left lying around. |
| 44 | # |
| 45 | # We proceed by first searching all parent calling frames and explicitly |
| 46 | # clearing their warning registries (necessary for the doctests above to |
| 47 | # pass). Then, we search for all submodules of skimage and clear theirs |
| 48 | # as well (necessary for the skimage test suite to pass). |
| 49 | |
| 50 | frame = inspect.currentframe() |
| 51 | if frame: |
| 52 | for f in inspect.getouterframes(frame): |
| 53 | f[0].f_locals["__warningregistry__"] = {} |
| 54 | del frame |
| 55 | |
| 56 | for _, mod in list(sys.modules.items()): |
| 57 | try: |
| 58 | mod.__warningregistry__.clear() |
| 59 | except AttributeError: |
| 60 | pass |
| 61 | |
| 62 | with warnings.catch_warnings(record=True) as w, mock.patch.dict( |
| 63 | os.environ, {"TRAITLETS_ALL_DEPRECATIONS": "1"} |
| 64 | ): |
| 65 | warnings.simplefilter("always") |
| 66 | yield w |
| 67 | |
| 68 | |
| 69 | @contextmanager |
no test coverage detected
searching dependent graphs…