diff --git a/doc/api/index.rst b/doc/api/index.rst index 7dd3d35afd3..258e2b40070 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -293,14 +293,15 @@ All custom exceptions are derived from :class:`pygmt.exceptions.GMTError`. :toctree: generated exceptions.GMTError - exceptions.GMTInvalidInput - exceptions.GMTVersionError - exceptions.GMTOSError exceptions.GMTCLibError exceptions.GMTCLibNoSessionError exceptions.GMTCLibNotFoundError + exceptions.GMTInvalidInput + exceptions.GMTOSError + exceptions.GMTParameterError exceptions.GMTTypeError exceptions.GMTValueError + exceptions.GMTVersionError .. currentmodule:: pygmt diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index 3318602a247..1accad95d9d 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -7,7 +7,7 @@ from typing import Any, Literal, NamedTuple import xarray as xr -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError with contextlib.suppress(ImportError): # rioxarray is needed to register the rio accessor @@ -564,11 +564,10 @@ def _load_remote_dataset( reg = registration[0] if resinfo.tiled and region is None: - msg = ( - f"The 'region' parameter is required for {dataset.description} " - f"resolution '{resolution}'." + raise GMTParameterError( + required="region", + reason=f"Required for {dataset.description} resolution {resolution!r}.", ) - raise GMTInvalidInput(msg) fname = f"@{prefix}_{resolution}_{reg}" grid = xr.load_dataarray( diff --git a/pygmt/exceptions.py b/pygmt/exceptions.py index dedb64db7fa..93b80bb082b 100644 --- a/pygmt/exceptions.py +++ b/pygmt/exceptions.py @@ -130,3 +130,35 @@ def __init__(self, dtype: object, /, reason: str | None = None): if reason: msg += f" {reason}" super().__init__(msg) + + +class GMTParameterError(GMTError): + """ + Raised when parameters are missing or invalid. + + Parameters + ---------- + required + Name or a set of names of required parameters. + reason + Detailed reason why the parameters are invalid. + """ + + def __init__( + self, + *, + required: str | set[str] | None = None, + reason: str | None = None, + ): + msg = [] + if required: + if isinstance(required, str): + msg.append(f"Missing required parameter: {required!r}.") + else: + msg.append( + "Missing required parameters: " + f"{', '.join(repr(par) for par in required)}." + ) + if reason: + msg.append(reason) + super().__init__(" ".join(msg)) diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 34a8e85e1b7..e0044a51dc0 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -6,7 +6,7 @@ from typing import Literal from pygmt._typing import PathLike -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError def validate_output_table_type( @@ -43,7 +43,7 @@ def validate_output_table_type( >>> validate_output_table_type("file", outfile=None) Traceback (most recent call last): ... - pygmt.exceptions.GMTInvalidInput: Must specify 'outfile' for output_type='file'. + pygmt.exceptions.GMTParameterError: Missing required parameter: 'outfile'. ... >>> with warnings.catch_warnings(record=True) as w: ... validate_output_table_type("pandas", outfile="not-none.txt") ... assert len(w) == 1 @@ -57,8 +57,9 @@ def validate_output_table_type( choices=_valids, ) if output_type == "file" and outfile is None: - msg = "Must specify 'outfile' for output_type='file'." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="outfile", reason="Required when output_type='file'." + ) if output_type != "file" and outfile is not None: msg = ( f"Changing 'output_type' from '{output_type}' to 'file' since 'outfile' " diff --git a/pygmt/params/box.py b/pygmt/params/box.py index 33b335d25d7..0cf23e4b562 100644 --- a/pygmt/params/box.py +++ b/pygmt/params/box.py @@ -6,7 +6,7 @@ from collections.abc import Sequence from pygmt.alias import Alias -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError from pygmt.helpers import is_nonstr_iter from pygmt.params.base import BaseParam @@ -65,8 +65,9 @@ def _validate(self): """ # inner_pen is required when inner_gap is set. if self.inner_gap is not None and self.inner_pen is None: - msg = "Parameter 'inner_pen' is required when 'inner_gap' is set." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="inner_pen", reason="Required when 'inner_gap' is set." + ) # shade_offset must be a sequence of two values or None. if self.shade_offset and not ( diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index edad413c990..92aa8b49cdf 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -9,7 +9,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import ( build_arg_list, fmt_docstring, @@ -112,8 +112,7 @@ def filter1d( (depends on ``output_type``) """ if kwargs.get("F") is None: - msg = "Pass a required argument to 'filter_type'." - raise GMTInvalidInput(msg) + raise GMTParameterError(required="filter_type") output_type = validate_output_table_type(output_type, outfile=outfile) diff --git a/pygmt/src/grdgradient.py b/pygmt/src/grdgradient.py index 7f90fec60af..6036cc54f56 100644 --- a/pygmt/src/grdgradient.py +++ b/pygmt/src/grdgradient.py @@ -9,7 +9,7 @@ from pygmt._typing import PathLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import build_arg_list, fmt_docstring, use_alias __doctest_skip__ = ["grdgradient"] @@ -166,8 +166,9 @@ def grdgradient( >>> new_grid = pygmt.grdgradient(grid=grid, azimuth=10) """ if kwargs.get("Q") is not None and kwargs.get("N") is None: - msg = "Must specify normalize if tiles is specified." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="normalize", reason="Required when 'tiles' is set." + ) if ( kwargs.get("A", azimuth) is None and kwargs.get("D") is None diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index 1f3b2921d39..c373a14721a 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -9,7 +9,7 @@ from pygmt._typing import PathLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import build_arg_list, deprecate_parameter, fmt_docstring, use_alias __doctest_skip__ = ["grdlandmask"] @@ -119,8 +119,7 @@ def grdlandmask( >>> landmask = pygmt.grdlandmask(spacing=1, region=[125, 130, 30, 35]) """ if kwargs.get("I", spacing) is None or kwargs.get("R", region) is None: - msg = "Both 'region' and 'spacing' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required={"region", "spacing"}) aliasdict = AliasSystem( D=Alias( diff --git a/pygmt/src/grdproject.py b/pygmt/src/grdproject.py index cf4c7514eb4..6a5290ef5e8 100644 --- a/pygmt/src/grdproject.py +++ b/pygmt/src/grdproject.py @@ -9,7 +9,7 @@ from pygmt._typing import PathLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import build_arg_list, fmt_docstring, use_alias __doctest_skip__ = ["grdproject"] @@ -118,8 +118,7 @@ def grdproject( # noqa: PLR0913 >>> new_grid = pygmt.grdproject(grid=grid, projection="M10c", region=region) """ if kwargs.get("J", projection) is None: - msg = "Parameter 'projection' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required="projection") if kwargs.get("M", unit) is not None and kwargs.get("F", scaling) is not False: msg = "Cannot use both 'unit' and 'scaling'." diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 236cff1da8e..ed4c04bcb94 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -11,7 +11,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import ( build_arg_list, fmt_docstring, @@ -307,8 +307,9 @@ def grdtrack( raise GMTInvalidInput(msg) if hasattr(points, "columns") and newcolname is None: - msg = "Please pass in a str to 'newcolname'." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="newcolname", reason="Pass in a string to 'newcolname'." + ) output_type = validate_output_table_type(output_type, outfile=outfile) diff --git a/pygmt/src/histogram.py b/pygmt/src/histogram.py index 349bf23d62a..61660b1f538 100644 --- a/pygmt/src/histogram.py +++ b/pygmt/src/histogram.py @@ -8,7 +8,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import ( build_arg_list, deprecate_parameter, @@ -163,8 +163,9 @@ def histogram( # noqa: PLR0913 self._activate_figure() if bar_offset is not None and bar_width is None: - msg = "Setting 'bar_offset' requires setting 'bar_width'." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="bar_width", reason="Required when 'bar_offset' is set." + ) aliasdict = AliasSystem( E=[ diff --git a/pygmt/src/inset.py b/pygmt/src/inset.py index 1b7d9367bcb..d8d83a85f77 100644 --- a/pygmt/src/inset.py +++ b/pygmt/src/inset.py @@ -9,7 +9,7 @@ from pygmt._typing import AnchorCode from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import ( build_arg_list, deprecate_parameter, @@ -139,8 +139,10 @@ def inset( and width is None and (projection is None or region is None) ): - msg = "Parameter 'width' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="width", + reason="Required unless 'projection' and 'region' are set.", + ) aliasdict = AliasSystem( D=[ diff --git a/pygmt/src/magnetic_rose.py b/pygmt/src/magnetic_rose.py index e91ef24c1af..10c1b173668 100644 --- a/pygmt/src/magnetic_rose.py +++ b/pygmt/src/magnetic_rose.py @@ -8,7 +8,7 @@ from pygmt._typing import AnchorCode from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import build_arg_list, fmt_docstring from pygmt.params import Box, Position from pygmt.src._common import _parse_position @@ -111,8 +111,10 @@ def magnetic_rose( # noqa: PLR0913 position = _parse_position(position, default=Position("BL", cstype="inside")) if declination_label is not None and declination is None: - msg = "Parameter 'declination' must be set when 'declination_label' is set." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="declination", + reason="Required when 'declination_label' is set.", + ) aliasdict = AliasSystem( F=Alias(box, name="box"), diff --git a/pygmt/src/meca.py b/pygmt/src/meca.py index 56c63037286..661082adc74 100644 --- a/pygmt/src/meca.py +++ b/pygmt/src/meca.py @@ -10,7 +10,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError from pygmt.helpers import ( build_arg_list, data_kind, @@ -31,8 +31,7 @@ def _get_focal_convention(spec, convention, component) -> _FocalMechanismConvent # Determine the convention from the 'convention' parameter. if convention is None: - msg = "Parameter 'convention' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required="convention") return _FocalMechanismConvention(convention=convention, component=component) diff --git a/pygmt/src/project.py b/pygmt/src/project.py index ad88345d1cb..2dc069b644a 100644 --- a/pygmt/src/project.py +++ b/pygmt/src/project.py @@ -10,7 +10,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import ( build_arg_list, fmt_docstring, @@ -221,11 +221,11 @@ def project( # noqa: PLR0913 (depends on ``output_type``) """ if kwargs.get("C", center) is None: - msg = "Parameter 'center' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required="center") if kwargs.get("G") is None and data is None: - msg = "The 'data' parameter must be specified unless 'generate' is used." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="data", reason="Required unless 'generate' is set." + ) if kwargs.get("G") is not None and kwargs.get("F") is not None: msg = "The 'convention' parameter is not allowed with 'generate'." raise GMTInvalidInput(msg) diff --git a/pygmt/src/sphdistance.py b/pygmt/src/sphdistance.py index c26e661c381..60510c30f3d 100644 --- a/pygmt/src/sphdistance.py +++ b/pygmt/src/sphdistance.py @@ -10,7 +10,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import build_arg_list, fmt_docstring, use_alias __doctest_skip__ = ["sphdistance"] @@ -123,8 +123,7 @@ def sphdistance( ... ) """ if kwargs.get("I", spacing) is None or kwargs.get("R", region) is None: - msg = "Both 'region' and 'spacing' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required={"region", "spacing"}) aliasdict = AliasSystem( I=Alias(spacing, name="spacing", sep="/", size=2), diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 156dd616a92..7f3f2b2b9c8 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -9,7 +9,7 @@ from pygmt._typing import AnchorCode, PathLike, StringArrayTypes, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput, GMTTypeError +from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTTypeError from pygmt.helpers import ( _check_encoding, build_arg_list, @@ -199,8 +199,9 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915 if position is not None: if text is None: - msg = "'text' can't be None when 'position' is given." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="text", reason="Required when 'position' is set." + ) if is_nonstr_iter(text): raise GMTTypeError( type(text), @@ -211,8 +212,9 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915 msg = "'text' can't be specified when 'textfiles' is given." raise GMTInvalidInput(msg) if kind == "empty" and text is None: - msg = "Must provide text with x/y pairs." - raise GMTInvalidInput(msg) + raise GMTParameterError( + required="text", reason="Required when 'x' and 'y' are set." + ) # Arguments that can accept arrays. array_args = [ diff --git a/pygmt/src/velo.py b/pygmt/src/velo.py index d1013f582e9..dbdd37a8c26 100644 --- a/pygmt/src/velo.py +++ b/pygmt/src/velo.py @@ -10,7 +10,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput, GMTTypeError +from pygmt.exceptions import GMTParameterError, GMTTypeError from pygmt.helpers import build_arg_list, deprecate_parameter, fmt_docstring, use_alias @@ -256,11 +256,13 @@ def velo( # noqa : PLR0913 """ self._activate_figure() - if kwargs.get("S") is None or ( - kwargs.get("S") is not None and not isinstance(kwargs["S"], str) - ): - msg = "The parameter 'spec' is required and has to be a string." - raise GMTInvalidInput(msg) + if kwargs.get("S") is None: + raise GMTParameterError(required="spec") + if not isinstance(kwargs["S"], str): + raise GMTTypeError( + type(kwargs["S"]), + reason="Parameter 'spec' must be in string type.", + ) if isinstance(data, np.ndarray) and not pd.api.types.is_numeric_dtype(data): raise GMTTypeError( diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index ba07f3ad0b9..6e21d795641 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -9,7 +9,7 @@ from pygmt._typing import PathLike, TableLike from pygmt.alias import Alias, AliasSystem from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import build_arg_list, fmt_docstring, use_alias __doctest_skip__ = ["xyz2grd"] @@ -159,8 +159,7 @@ def xyz2grd( ... ) """ if kwargs.get("I", spacing) is None or kwargs.get("R", region) is None: - msg = "Both 'region' and 'spacing' must be specified." - raise GMTInvalidInput(msg) + raise GMTParameterError(required={"region", "spacing"}) aliasdict = AliasSystem( I=Alias(spacing, name="spacing", sep="/", size=2), diff --git a/pygmt/tests/test_datasets_load_remote_datasets.py b/pygmt/tests/test_datasets_load_remote_datasets.py index e2cef8f8ac2..dfa7fc5bf75 100644 --- a/pygmt/tests/test_datasets_load_remote_datasets.py +++ b/pygmt/tests/test_datasets_load_remote_datasets.py @@ -5,7 +5,7 @@ import pytest from pygmt.datasets.load_remote_dataset import _load_remote_dataset from pygmt.enums import GridRegistration -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError def load_remote_dataset_wrapper(resolution="01d", region=None, registration=None): @@ -61,7 +61,7 @@ def test_load_remote_dataset_tiled_grid_without_region(): Make sure _load_remote_dataset fails when trying to load a tiled grid without specifying a region. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): load_remote_dataset_wrapper(resolution="01m") diff --git a/pygmt/tests/test_grdgradient.py b/pygmt/tests/test_grdgradient.py index 5749b237aea..130ce64ccac 100644 --- a/pygmt/tests/test_grdgradient.py +++ b/pygmt/tests/test_grdgradient.py @@ -8,7 +8,7 @@ import xarray as xr from pygmt import grdgradient from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -82,6 +82,6 @@ def test_grdgradient_fails(grid): """ with pytest.raises(GMTInvalidInput): grdgradient(grid=grid) # fails without required arguments - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): # fails when tiles is specified but not normalize grdgradient(grid=grid, azimuth=10, direction="c", tiles="c") diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 21b45d8d8c5..9155890c414 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -8,7 +8,7 @@ import xarray as xr from pygmt import grdlandmask from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import GMTTempFile @@ -64,5 +64,5 @@ def test_grdlandmask_fails(): """ Check that grdlandmask fails correctly when region and spacing are not given. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): grdlandmask() diff --git a/pygmt/tests/test_grdproject.py b/pygmt/tests/test_grdproject.py index 8a5560448c5..4207b4a4521 100644 --- a/pygmt/tests/test_grdproject.py +++ b/pygmt/tests/test_grdproject.py @@ -8,7 +8,7 @@ import xarray as xr from pygmt import grdproject from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -101,5 +101,5 @@ def test_grdproject_fails(grid): """ Check that grdproject fails correctly. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): grdproject(grid=grid) diff --git a/pygmt/tests/test_grdtrack.py b/pygmt/tests/test_grdtrack.py index 1b63dae041a..c6d45958094 100644 --- a/pygmt/tests/test_grdtrack.py +++ b/pygmt/tests/test_grdtrack.py @@ -9,7 +9,7 @@ import pandas as pd import pytest from pygmt import grdtrack -from pygmt.exceptions import GMTInvalidInput, GMTTypeError +from pygmt.exceptions import GMTInvalidInput, GMTParameterError, GMTTypeError from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import load_static_earth_relief @@ -146,7 +146,7 @@ def test_grdtrack_without_newcolname_setting(dataarray, dataframe): """ Run grdtrack by not passing in newcolname parameter setting. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): grdtrack(points=dataframe, grid=dataarray) @@ -154,7 +154,7 @@ def test_grdtrack_without_outfile_setting(dataarray, dataframe): """ Run grdtrack by not passing in outfile parameter setting. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): grdtrack(points=dataframe, grid=dataarray) diff --git a/pygmt/tests/test_histogram.py b/pygmt/tests/test_histogram.py index 3f1a78f04b0..cdf2ecbe814 100644 --- a/pygmt/tests/test_histogram.py +++ b/pygmt/tests/test_histogram.py @@ -5,7 +5,7 @@ import pandas as pd import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError @pytest.fixture(scope="module", name="data", params=[list, pd.Series]) @@ -40,7 +40,7 @@ def test_histogram_baroffset(data): Test passing bar_offset requires bar_width. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.histogram( data=data, projection="X10c/10c", diff --git a/pygmt/tests/test_inset.py b/pygmt/tests/test_inset.py index 63c99fe5d90..a5c219bf02a 100644 --- a/pygmt/tests/test_inset.py +++ b/pygmt/tests/test_inset.py @@ -4,7 +4,7 @@ import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.params import Box, Position @@ -86,11 +86,11 @@ def test_inset_invalid_inputs(): fig = Figure() fig.basemap(region="MG+r2", frame="afg") # Width is not given - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): with fig.inset(position=Position("TL")): pass # Height is given but width is not given - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): with fig.inset(position=Position("TL"), height="5c"): pass # Old position syntax conflicts with width/height diff --git a/pygmt/tests/test_magnetic_rose.py b/pygmt/tests/test_magnetic_rose.py index 6102c1b0054..d6d5cf1bddf 100644 --- a/pygmt/tests/test_magnetic_rose.py +++ b/pygmt/tests/test_magnetic_rose.py @@ -4,7 +4,7 @@ import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.params import Position @@ -46,5 +46,5 @@ def test_magnetic_rose_invalid_declination_label(): """ fig = Figure() fig.basemap(region=[-10, 10, -10, 10], projection="M10c", frame=True) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.magnetic_rose(declination_label="11.5°E") diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index 618b1cfbbed..2b1b41218f3 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -8,7 +8,7 @@ import pandas as pd import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError from pygmt.helpers import GMTTempFile @@ -283,7 +283,7 @@ def test_meca_spec_ndarray_no_convention(): """ fig = Figure() fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.meca(spec=np.array([[-124, 48, 12.0, 330, 30, 90, 3]]), scale="1c") diff --git a/pygmt/tests/test_params_box.py b/pygmt/tests/test_params_box.py index 0bdd70adefe..ddea880aa28 100644 --- a/pygmt/tests/test_params_box.py +++ b/pygmt/tests/test_params_box.py @@ -3,7 +3,7 @@ """ import pytest -from pygmt.exceptions import GMTInvalidInput, GMTValueError +from pygmt.exceptions import GMTParameterError, GMTValueError from pygmt.params import Box @@ -55,5 +55,5 @@ def test_params_box_invalid_innerborder(): """ Test that inner_pen is required when inner_gap is set. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): _ = str(Box(inner_gap="2p")) diff --git a/pygmt/tests/test_project.py b/pygmt/tests/test_project.py index 36917368c63..cdf71a236f5 100644 --- a/pygmt/tests/test_project.py +++ b/pygmt/tests/test_project.py @@ -10,7 +10,7 @@ import pytest import xarray as xr from pygmt import project -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTInvalidInput, GMTParameterError from pygmt.helpers import GMTTempFile @@ -81,10 +81,10 @@ def test_project_incorrect_parameters(): Run project by providing incorrect parameters such as 1) no `center`; 2) no `data` or `generate`; and 3) `generate` with `convention`. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): # No `center` project(azimuth=45) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): # No `data` or `generate` project(center=[0, -1], azimuth=45, flat_earth=True) with pytest.raises(GMTInvalidInput): diff --git a/pygmt/tests/test_sphdistance.py b/pygmt/tests/test_sphdistance.py index f96f14530e5..a4ad9fb2cdf 100644 --- a/pygmt/tests/test_sphdistance.py +++ b/pygmt/tests/test_sphdistance.py @@ -9,7 +9,7 @@ import pytest from pygmt import sphdistance from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import GMTTempFile @@ -69,5 +69,5 @@ def test_sphdistance_fails(array): """ Check that sphdistance fails correctly when neither increment nor region is given. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): sphdistance(data=array) diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 75003409ce0..a3fcdf05b0f 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -7,7 +7,12 @@ import numpy as np import pytest from pygmt import Figure, config -from pygmt.exceptions import GMTCLibError, GMTInvalidInput, GMTTypeError +from pygmt.exceptions import ( + GMTCLibError, + GMTInvalidInput, + GMTParameterError, + GMTTypeError, +) from pygmt.helpers import GMTTempFile from pygmt.helpers.testing import skip_if_no @@ -85,7 +90,7 @@ def test_text_without_text_input(region, projection): Run text by passing in x and y, but no text. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.text(region=region, projection=projection, x=1.2, y=2.4) @@ -152,7 +157,7 @@ def test_text_invalid_inputs(region): ) with pytest.raises(GMTInvalidInput): fig.text(region=region, projection="x1c", textfiles="file.txt", text="text") - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.text(region=region, projection="x1c", position="MC", text=None) with pytest.raises(GMTTypeError): fig.text( diff --git a/pygmt/tests/test_velo.py b/pygmt/tests/test_velo.py index de2cb20e50c..0b264bfbc9e 100644 --- a/pygmt/tests/test_velo.py +++ b/pygmt/tests/test_velo.py @@ -5,7 +5,7 @@ import pandas as pd import pytest from pygmt import Figure -from pygmt.exceptions import GMTInvalidInput, GMTTypeError +from pygmt.exceptions import GMTParameterError, GMTTypeError @pytest.fixture(scope="module", name="dataframe") @@ -60,7 +60,7 @@ def test_velo_without_spec(dataframe): Check that velo fails when the spec parameter is not given. """ fig = Figure() - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): fig.velo(data=dataframe) diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 9f70c73cf8c..bf7fe854233 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -10,7 +10,7 @@ from pygmt import xyz2grd from pygmt.datasets import load_sample_data from pygmt.enums import GridRegistration, GridType -from pygmt.exceptions import GMTInvalidInput +from pygmt.exceptions import GMTParameterError from pygmt.helpers import GMTTempFile @@ -75,9 +75,9 @@ def test_xyz2grd_missing_region_spacing(ship_data): """ Test xyz2grd raise an exception if region or spacing is missing. """ - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): xyz2grd(data=ship_data) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): xyz2grd(data=ship_data, region=[245, 255, 20, 30]) - with pytest.raises(GMTInvalidInput): + with pytest.raises(GMTParameterError): xyz2grd(data=ship_data, spacing=5)