-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Griesfeller
committed
Apr 15, 2024
1 parent
dee072b
commit 421c5dc
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import warnings | ||
from contextlib import contextmanager | ||
|
||
|
||
@contextmanager | ||
def ignore_warnings(category: type[Warning], *messages: str): | ||
""" | ||
Ignore particular warnings with a decorator or context manager | ||
Parameters | ||
---------- | ||
category : subclass of Warning | ||
warning category to be ignored. E.g. UserWarning, DeprecationWarning. | ||
The default is Warning. | ||
*messages : str, optional | ||
warning messages to be ignored. E.g. | ||
ignore_warnings(Warning, 'Warning that can safely be ignored', 'Other warning to ignore'). | ||
For each | ||
`<entry>` :func:`warnigns.filterwarnings('ignore', Warning, message=<entry>)` | ||
is called. | ||
Example | ||
------- | ||
@ignore_warnings(UserWarning) | ||
@ignore_warnings(DeprecationWarning) | ||
@ignore_warnings(Warning, 'I REALLY') | ||
def warn_randomly_and_add_numbers(num1, num2): | ||
warnings.warn(UserWarning('Harmless user warning')) | ||
warnings.warn(DeprecationWarning('This function is deprecated')) | ||
warnings.warn(Warning('I REALLY NEED TO REACH YOU')) | ||
return num1+num2 | ||
""" | ||
if not issubclass(category, Warning): | ||
raise ValueError("category must be a Warning subclass") | ||
|
||
if not messages: | ||
message = "" | ||
elif all(isinstance(msg, str) for msg in messages): | ||
message = "|".join(messages) | ||
else: | ||
raise ValueError("messages must be list of strings") | ||
|
||
with warnings.catch_warnings(): | ||
warnings.filterwarnings("ignore", category=category, message=message) | ||
yield | ||
|
||
|
||
def ignore_basemap_warning(): # pragma: no cover | ||
warnings.filterwarnings( | ||
"ignore", r".*install Basemap$", UserWarning, "geonum", append=True | ||
) | ||
|
||
|
||
def ignore_earth_radius_warning(): # pragma: no cover | ||
warnings.filterwarnings( | ||
"ignore", | ||
"Using DEFAULT_SPHERICAL_EARTH_RADIUS", | ||
UserWarning, | ||
"iris.*", | ||
append=True, | ||
) |