You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add new exceptions for invalid inputs and deprecate GMTInvalidInput exception #3707
Most exceptions are about the GMT C library, and is only used once or a few times. But the GMTInvalidInput exception is used 87 times and will definitely increase as we wrap more modules.
The GMTInvalidInput exception is general and doesn't tell exactly what's happening. I haven't checked the source codes carefully, but I feel the exceptions can be grouped into a few categories:
A required parameter is not given
Given parameters have conflicts
A parameter is given with an incorrect type
A parameter is given with invalid values
Maybe more
I think we should have more accurate exceptions, e.g.,
GMTParameterError: for cases 1 and 2, maybe having more fine-grain exceptions like GMTRequiredParameterError/GMTConflictParameterError
changed the title [-]Add new exceptions for invalid inputs and deprecated GMTInvalidInput exception[/-][+]Add new exceptions for invalid inputs and deprecate GMTInvalidInput exception[/+]on Dec 23, 2024
I'm thinking that at the pygmt.clib level, we should have thin wrappers to GMT C error enums, and then we can group those into higher level error types like TypeError, ValueError, etc as you've described.
These are all low-level error codes used in GMT's low-level APIs. When passing an incorrect value/type to a GMT module, GMT just returns the GMT_PARSE_ERROR error.
@Chuan1937 and I are working on the issue in PRs #4370 and #4003, but we're taking different approaches.
PR #4370 implements the GMTRequiredParameterError/GMTConflictParameterError exceptions, while PR #4003 implements the more general GMTParameterError exception
I prefer the general GMTParameterError exception, mainly because:
It's much shorter, so we don't have to wrap long lines when all the exceptions are imported. E.g.,
from pygmt.exceptions import GMTValueError, GMTParameterError, GMTTypeError
vs
from pygmt.exceptions import (
GMTValueError,
GMTTypeError,
GMTRequiredParameterError,
GMTConflictParameterError,
)
Consistent with the existing exceptions GMTValueError/GMTTypeError
More flexible. New parameter errors can be added without introducing new exception classes
What do @GenericMappingTools/pygmt-maintainers think?
I think stick with just a single GMTParameterError, but I do like @Chuan1937's clear messaging around conflicting parameters in #4370. Could maybe pull some aspects of that into #4003?
OK. Let's go with the single GMTParameterError exception.
PR #4003 was opened to explore the possible types of errors related to parameters. From what I have learnt, the parameter errors can be grouped into the following categories:
One or a few parameters are required
At least one of the parameters par1, par2, par3 is required
Only one of the parameters can be specified: par1, par2, par3
Parameter par1 conflicts with par2 [I feel it's same as catgeory 3]
Parameter par1 conflicts with par2, par3, par4
Maybe more
In PR #4003, the error message for each category is:
Required parameter(s) are missing: 'par1', 'par2', 'par3'.
At least one of the following parameters must be specified: 'par1', 'par2', 'par3'.
Mutually exclusive parameter(s) are specified: 'par1', 'par2'.
Conflicting parameters: 'par1' and other parameters
We should have clear and standardized error messages for each category.
Please note that PR #4003 was opened for proof of concept. It contains too many changes and is difficult to review. I was planning to split it into smaller PRs, with each PR working on one specific type of parameter errors. I won't work on it in the following days, but I can review it. In other words, @Chuan1937, you can split PR #4003 into smaller PRs if you're interested.
Here is a summary table of the (preferred and alternative) parameter names and error message templates for each parameter error type. They're not final decisions and are open for comments and suggestions.
In PR #4374, #4380, #4383, #4387, we have implemented four subttypes for GMTParameteError and updated most files: required, at_least_one, at_most_one, conflicts_with. There are still a few files that use GMTInvalidInput.
Case 1
In the following files, errors are raised under certain conditions. E.g.,
msg="Parameter 'dpi' can only be used when 'surftype' is 'image'."
raiseGMTInvalidInput(msg)
pygmt/src/grdview.py Line 84
pygmt/src/grdview.py Line 87
pygmt/src/grdview.py Line 93
pygmt/src/grdhisteq.py Line 239
Our options are
Use the existing conflicts_with subtype, with custom reason.
Introduce a new error subtype for this case.
Case 2
In the following files, errors are raised for deprecated parameters.
pygmt/helpers/decorators.py Line 571
pygmt/helpers/decorators.py Line 579
pygmt/helpers/decorators.py Line 587
Our options are:
Use the current GMTParameter exception with custom reasons
Introduce a new subtype deprecated
Allow X/Y/U/xshift/yshift/timestamp in normal plotting methods, and instead raise an error. Just like what we did to the map_scale parameter in Figure.basemap?
Case 3
In the following files, errors are raised mostly due to missing input data, invalid combinations of x/y/z/data.
pygmt/helpers/utils.py Line 125
pygmt/helpers/utils.py Line 128
pygmt/helpers/utils.py Line 131
pygmt/helpers/utils.py Line 135
pygmt/helpers/utils.py Line 140
pygmt/helpers/utils.py Line 146
pygmt/helpers/utils.py Line 148
pygmt/helpers/utils.py Line 154
pygmt/helpers/utils.py Line 157
pygmt/helpers/utils.py Line 565
pygmt/helpers/utils.py Line 870
pygmt/helpers/utils.py Line 878
pygmt/helpers/utils.py Line 900
pygmt/helpers/utils.py Line 903
I feel GMTInvalidInput makes sense in this case, so maybe keep it but rename to GMTInputError?
“Invalid” distinguishes between input that exists but is formatted incorrectly and input that may be entirely missing. I believe it is preferable to GMTInputError.
Activity
[-]Add new exceptions for invalid inputs and deprecated GMTInvalidInput exception[/-][+]Add new exceptions for invalid inputs and deprecate GMTInvalidInput exception[/+]weiji14 commentedon Jun 23, 2025
Does GMT C have different Error types or enum variants that we should try to match? I found https://github.com/GenericMappingTools/gmt/blob/1ed5e1bf4be46714e4124f9c1935e5bb14a6fb99/src/gmt_error_codes.h#L32, but unsure if there are some other ones.
I'm thinking that at the
pygmt.cliblevel, we should have thin wrappers to GMT C error enums, and then we can group those into higher level error types likeTypeError,ValueError, etc as you've described.seisman commentedon Jun 24, 2025
These are all low-level error codes used in GMT's low-level APIs. When passing an incorrect value/type to a GMT module, GMT just returns the
GMT_PARSE_ERRORerror.seisman commentedon Jan 26, 2026
@Chuan1937 and I are working on the issue in PRs #4370 and #4003, but we're taking different approaches.
PR #4370 implements the
GMTRequiredParameterError/GMTConflictParameterErrorexceptions, while PR #4003 implements the more generalGMTParameterErrorexceptionI prefer the general
GMTParameterErrorexception, mainly because:vs
GMTValueError/GMTTypeErrorWhat do @GenericMappingTools/pygmt-maintainers think?
weiji14 commentedon Jan 27, 2026
I think stick with just a single
GMTParameterError, but I do like @Chuan1937's clear messaging around conflicting parameters in #4370. Could maybe pull some aspects of that into #4003?seisman commentedon Jan 27, 2026
OK. Let's go with the single
GMTParameterErrorexception.PR #4003 was opened to explore the possible types of errors related to parameters. From what I have learnt, the parameter errors can be grouped into the following categories:
In PR #4003, the error message for each category is:
Required parameter(s) are missing: 'par1', 'par2', 'par3'.At least one of the following parameters must be specified: 'par1', 'par2', 'par3'.Mutually exclusive parameter(s) are specified: 'par1', 'par2'.In PR #4370, the error messages look like:
Missing required parameters: 'par1' and 'par2'.Missing required parameters: 'par1' or 'par3'.Conflicting parameters: 'par1' and 'par2'.Conflicting parameters: 'par1' and other parametersWe should have clear and standardized error messages for each category.
Please note that PR #4003 was opened for proof of concept. It contains too many changes and is difficult to review. I was planning to split it into smaller PRs, with each PR working on one specific type of parameter errors. I won't work on it in the following days, but I can review it. In other words, @Chuan1937, you can split PR #4003 into smaller PRs if you're interested.
Chuan1937 commentedon Jan 27, 2026
Ok,I'll open some new PRs to slove it based on single exception
GMTParameterErrorseisman commentedon Jan 29, 2026
Here is a summary table of the (preferred and alternative) parameter names and error message templates for each parameter error type. They're not final decisions and are open for comments and suggestions.
requiredmust_have,neededMissing required parameter: '{param}'Missing required parameters: '{a}', '{b}'at_least_oneneed_one_of,any_of,one_of,required_anyMissing required parameters: '{a}', '{b}', '{c}'. Must specify at least one.Must specify at least one of the parameters: '{a}', '{b}', '{c}'Exactly one is requiredexactly_onemutually_exclusiveMutually exclusive parameters: '{a}', '{b}', '{c}'. Specify exactly one.at_most_onemutually_exclusiveMutually exclusive parameters: '{a}', '{b}', '{c}'. Specify at most oneconflicts_withconflicts,incompatible_withConflicting parameters: '{a}' cannot be used with '{b}', '{c}'seisman commentedon Feb 4, 2026
seisman commentedon Feb 4, 2026
@Chuan1937 I think the following lines should also be updated with the
at_most_oneparameter. Please open a PR when you have time:seisman commentedon Feb 7, 2026
In PR #4374, #4380, #4383, #4387, we have implemented four subttypes for GMTParameteError and updated most files:
required,at_least_one,at_most_one,conflicts_with. There are still a few files that useGMTInvalidInput.Case 1
In the following files, errors are raised under certain conditions. E.g.,
pygmt/pygmt/src/grdview.py
Lines 82 to 84 in 5e7b702
Our options are
conflicts_withsubtype, with customreason.Case 2
In the following files, errors are raised for deprecated parameters.
Our options are:
GMTParameterexception with custom reasonsdeprecatedX/Y/U/xshift/yshift/timestampin normal plotting methods, and instead raise an error. Just like what we did to themap_scaleparameter inFigure.basemap?Case 3
In the following files, errors are raised mostly due to missing input data, invalid combinations of x/y/z/data.
I feel
GMTInvalidInputmakes sense in this case, so maybe keep it but rename toGMTInputError?Chuan1937 commentedon Feb 7, 2026
“Invalid” distinguishes between input that exists but is formatted incorrectly and input that may be entirely missing. I believe it is preferable to GMTInputError.
seisman commentedon May 23, 2026
Here is an update: