Skip to content

Commit

Permalink
Merge pull request #1070 from metno/clean-up-deprecations
Browse files Browse the repository at this point in the history
warnings are not test errors
  • Loading branch information
heikoklein authored Mar 21, 2024
2 parents ebbbeee + d714b10 commit 2c33e01
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
3 changes: 2 additions & 1 deletion pyaerocom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from importlib import metadata

from ._logging import change_verbosity
from ._warnings import ignore_basemap_warning
from ._warnings import ignore_basemap_warning, ignore_earth_radius_warning

__version__ = metadata.version(__package__)

Expand All @@ -11,6 +11,7 @@
# Instantiate default configuration
const = Config()
ignore_basemap_warning()
ignore_earth_radius_warning()

# Sub-packages
from . import io
Expand Down
19 changes: 11 additions & 8 deletions pyaerocom/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ def warn_randomly_and_add_numbers(num1, num2):

if not messages:
message = ""
elif all(type(msg) == str for msg in messages):
elif all(isinstance(msg, str) for msg in messages):
message = "|".join(messages)
else:
raise ValueError("messages must be list of strings")

try:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=category, message=message)
yield
finally:
pass
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")
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
)
25 changes: 13 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ log_cli_level = "WARNING"
addopts = ["--failed-first", "--import-mode=importlib"]
xfail_strict = true
testpaths = ["tests"]
filterwarnings = [
# all warnings are errors
"error",
"ignore::pytest.PytestUnraisableExceptionWarning",
# except deprecation and future warnings ouside this packege
'ignore::PendingDeprecationWarning:^(?!pyaerocom|tests).*:',
'ignore::DeprecationWarning:^(?!pyaerocom|tests).*:',
'ignore::FutureWarning:^(?!pyaerocom|tests).*:',
# and not on this list
"ignore:.*please install Basemap:UserWarning:geonum.*:",
"ignore:Using DEFAULT_SPHERICAL_EARTH_RADIUS:UserWarning:iris.*:",
]
## uncomment to raise errors from warnings
#filterwarnings = [
# # all warnings are errors
# "error",
# "ignore::pytest.PytestUnraisableExceptionWarning",
# # except deprecation and future warnings ouside this packege
# 'ignore::PendingDeprecationWarning:^(?!pyaerocom|tests).*:',
# 'ignore::DeprecationWarning:^(?!pyaerocom|tests).*:',
# 'ignore::FutureWarning:^(?!pyaerocom|tests).*:',
# # and not on this list
# "ignore:.*please install Basemap:UserWarning:geonum.*:",
# "ignore:Using DEFAULT_SPHERICAL_EARTH_RADIUS:UserWarning:iris.*:",
#]

[tool.coverage.run]
source = ["pyaerocom"]
Expand Down
3 changes: 1 addition & 2 deletions tests/io/mscw_ctm/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,8 @@ def test_ReadMscwCtm__repr__():


def test_ReadEMEP__init__():
with pytest.raises(DeprecationWarning) as e:
with pytest.warns(DeprecationWarning) as e:
assert isinstance(ReadEMEP(), ReadMscwCtm)
assert "please use ReadMscwCtm instead" in str(e.value)


def emep_data_path(tmp_path: Path, freq: str | list[str], vars_and_units: dict[str, str]) -> Path:
Expand Down

0 comments on commit 2c33e01

Please sign in to comment.