From bd9fb90f3b02221f1b01b7f49135df426cca012d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 9 Feb 2026 16:55:37 +0800 Subject: [PATCH] Figure.grdview: Raise GMTParameterError when parameter conflicts with specified surftype --- pygmt/src/grdview.py | 21 ++++++++++++--------- pygmt/tests/test_grdview.py | 8 ++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/pygmt/src/grdview.py b/pygmt/src/grdview.py index d67cb1b7659..f9cf5a3a93a 100644 --- a/pygmt/src/grdview.py +++ b/pygmt/src/grdview.py @@ -10,7 +10,7 @@ from pygmt._typing import PathLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session, __gmt_version__ -from pygmt.exceptions import GMTInvalidInput, GMTParameterError +from pygmt.exceptions import GMTParameterError from pygmt.helpers import build_arg_list, deprecate_parameter, fmt_docstring, use_alias from pygmt.src.grdinfo import grdinfo @@ -80,17 +80,20 @@ def _alias_option_Q( # noqa: N802 ) if dpi is not None and surftype != "image": - msg = "Parameter 'dpi' can only be used when 'surftype' is 'image'." - raise GMTInvalidInput(msg) + raise GMTParameterError( + conflicts_with=("dpi", [f"surftype={surftype!r}"]), + reason="'dpi' is allowed only when 'surftype' is 'image'.", + ) if nan_transparent and surftype != "image": - msg = "Parameter 'nan_transparent' can only be used when 'surftype' is 'image'." - raise GMTInvalidInput(msg) + raise GMTParameterError( + conflicts_with=("nan_transparent", [f"surftype={surftype!r}"]), + reason="'nan_transparent' is allowed only when 'surftype' is 'image'.", + ) if mesh_fill is not None and surftype not in {"mesh", "waterfall_x", "waterfall_y"}: - msg = ( - "Parameter 'mesh_fill' can only be used when 'surftype' is 'mesh', " - "'waterfall_x', or 'waterfall_y'." + raise GMTParameterError( + conflicts_with=("mesh_fill", [f"surftype={surftype!r}"]), + reason="'mesh_fill' is allowed only when 'surftype' is 'mesh', 'waterfall_x', or 'waterfall_y'.", ) - raise GMTInvalidInput(msg) return [ Alias( diff --git a/pygmt/tests/test_grdview.py b/pygmt/tests/test_grdview.py index 6bb5e5b6594..ede0fdb41f1 100644 --- a/pygmt/tests/test_grdview.py +++ b/pygmt/tests/test_grdview.py @@ -4,7 +4,7 @@ import pytest from pygmt import Figure, grdcut -from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTTypeError +from pygmt.exceptions import GMTParameterError, GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -330,11 +330,11 @@ def test_grdview_invalid_surftype(gridfile): parameters. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.grdview(grid=gridfile, surftype="surface", dpi=300) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.grdview(grid=gridfile, surftype="surface", nan_transparent=True) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.grdview(grid=gridfile, surftype="surface", mesh_fill="red")