From f78622d6ea9682b7e0b0550246cf2dcee4e6c50b Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Wed, 20 Mar 2024 10:46:28 +0100 Subject: [PATCH 1/3] clean up warnings config --- pyaerocom/__init__.py | 3 ++- pyaerocom/_warnings.py | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pyaerocom/__init__.py b/pyaerocom/__init__.py index a4bf68188..4397e400b 100644 --- a/pyaerocom/__init__.py +++ b/pyaerocom/__init__.py @@ -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__) @@ -11,6 +11,7 @@ # Instantiate default configuration const = Config() ignore_basemap_warning() +ignore_earth_radius_warning() # Sub-packages from . import io diff --git a/pyaerocom/_warnings.py b/pyaerocom/_warnings.py index 8abf838b3..ebeee08fd 100644 --- a/pyaerocom/_warnings.py +++ b/pyaerocom/_warnings.py @@ -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 + ) From 9856ab3be59b621198d49669d9c71811a7d015d5 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Wed, 20 Mar 2024 11:03:18 +0100 Subject: [PATCH 2/3] ingore warnings on tests, close #1065 --- pyproject.toml | 6 +++++- tests/io/mscw_ctm/test_reader.py | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8c1e5e9c7..ceca17527 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,7 +99,11 @@ exclude = [ minversion = "7.4" log_cli = false log_cli_level = "WARNING" -addopts = ["--failed-first", "--import-mode=importlib"] +addopts = [ + "--failed-first", + "--import-mode=importlib", + "--pythonwarnings=ignore", # comment to raise errors from warnings, see filterwarnings +] xfail_strict = true testpaths = ["tests"] filterwarnings = [ diff --git a/tests/io/mscw_ctm/test_reader.py b/tests/io/mscw_ctm/test_reader.py index 7c7babeec..8478ee923 100644 --- a/tests/io/mscw_ctm/test_reader.py +++ b/tests/io/mscw_ctm/test_reader.py @@ -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: From d714b101e1ce52f14b22a0b51b2ec4b8e09bfaf6 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Thu, 21 Mar 2024 15:50:33 +0100 Subject: [PATCH 3/3] comment pytest filterwarnings config --- pyproject.toml | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ceca17527..c1f8a25e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,25 +99,22 @@ exclude = [ minversion = "7.4" log_cli = false log_cli_level = "WARNING" -addopts = [ - "--failed-first", - "--import-mode=importlib", - "--pythonwarnings=ignore", # comment to raise errors from warnings, see filterwarnings -] +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"]