Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent warning repeats #5506

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
prevent warning repeats
  • Loading branch information
stephenworsley committed Feb 11, 2024
commit a74fb835564d6c5b0b8f7f74e244e06da6c40a98
19 changes: 19 additions & 0 deletions lib/iris/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Exceptions specific to the Iris package."""
from functools import lru_cache
import traceback
import warnings


class IrisError(Exception):
Expand Down Expand Up @@ -344,3 +347,19 @@ class IrisSaverFillValueWarning(IrisMaskValueMatchWarning, IrisSaveWarning):
"""

pass


@lru_cache(None)
def warn_once(msg, type, stacklevel, frame):
"""Raise a warning only if a similar one has not been raised before."""
warnings.warn(msg, type, stacklevel=stacklevel)


def warn_once_at_level(msg, type, stacklevel):
"""
Raise a warning only if a similar one hasn't been raised from the same line
for a given stack level.
"""
stacklevel += 1
frame = traceback.format_stack()[-stacklevel]
warn_once(msg, type, stacklevel + 1, frame)
trexfeathers marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion lib/iris/fileformats/_nc_load_rules/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def _get_ellipsoid(cf_grid_var):
"applied. To apply the datum when loading, use the "
"iris.FUTURE.datum_support flag."
)
warnings.warn(wmsg, category=FutureWarning, stacklevel=14)
iris.exceptions.warn_once_at_level(wmsg, FutureWarning, category=FutureWarning, stacklevel=14)
datum = None

if datum is not None:
Expand Down
9 changes: 8 additions & 1 deletion lib/iris/tests/integration/netcdf/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def test_datum_once(self):
fnames = [
"false_east_north_merc.nc",
"non_unit_scale_factor_merc.nc",
# toa_brightness_temperature.nc,
"toa_brightness_temperature.nc",
]
fpaths = [
tests.get_data_path(("NetCDF", "mercator", fname)) for fname in fnames
Expand All @@ -518,6 +518,13 @@ def test_datum_once(self):
warnings.warn("Dummy warning", category=iris.exceptions.IrisUserWarning)
assert len(record) == 2

with warnings.catch_warnings(record=True) as record:
warnings.simplefilter("default")
iris.load_cube(fpaths[0])
iris.load_cube(fpaths[1])
iris.load_cube(fpaths[2])
assert len(record) == 3


if __name__ == "__main__":
tests.main()