Skip to content

Add new exceptions for invalid inputs and deprecate GMTInvalidInput exception #3707

Open

Description

@seisman

Currently, we have several exceptions defined in https://github.com/GenericMappingTools/pygmt/blob/main/pygmt/exceptions.py.

Below is a simple command to count how often each exception is raised in the codebase (The tests directory and the exceptions.py file are excluded).

for e in \
    GMTError \
    GMTOSError \
    GMTCLibError \
    GMTCLibNotFoundError \
    GMTCLibNoSessionError \
    GMTVersionError \
    GMTInvalidInput \
    GMTImageComparisonFailure; 
do
    n=$(grep -R --exclude-dir=tests --exclude="exceptions.py" "raise $e" pygmt | wc -l)
    echo "$e: $n" 
done

the output is:

GMTError: 1
GMTOSError: 1
GMTCLibError: 16
GMTCLibNotFoundError: 1
GMTCLibNoSessionError: 1
GMTVersionError: 1
GMTInvalidInput: 87
GMTImageComparisonFailure: 1

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:

  1. A required parameter is not given
  2. Given parameters have conflicts
  3. A parameter is given with an incorrect type
  4. A parameter is given with invalid values
  5. Maybe more

I think we should have more accurate exceptions, e.g.,

The exception may even start with PYGMT, not GMT, since these are not directly related to GMT.

Activity

  1. added
    discussions
    on Dec 23, 2024
  2. 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
  3. weiji14 commented on Jun 23, 2025

    @weiji14
    Member

    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.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.

  4. seisman commented on Jun 24, 2025

    @seisman
    MemberAuthor

    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.

    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.

  5. added
    enhancement
    and removed
    discussions
    on Jul 10, 2025
  6. added this to the 0.17.0 milestone on Jul 10, 2025
  7. modified the milestones: 0.17.0, 0.18.0 on Sep 18, 2025
  8. removed this from the 0.18.0 milestone on Dec 16, 2025
  9. seisman commented on Jan 26, 2026

    @seisman
    MemberAuthor

    @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:

    1. 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,
    )
    
    1. Consistent with the existing exceptions GMTValueError/GMTTypeError
    2. More flexible. New parameter errors can be added without introducing new exception classes

    What do @GenericMappingTools/pygmt-maintainers think?

  10. weiji14 commented on Jan 27, 2026

    @weiji14
    Member

    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?

  11. seisman commented on Jan 27, 2026

    @seisman
    MemberAuthor

    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:

    1. One or a few parameters are required
    2. At least one of the parameters par1, par2, par3 is required
    3. Only one of the parameters can be specified: par1, par2, par3
    4. Parameter par1 conflicts with par2 [I feel it's same as catgeory 3]
    5. Parameter par1 conflicts with par2, par3, par4
    6. Maybe more

    In PR #4003, the error message for each category is:

    1. Required parameter(s) are missing: 'par1', 'par2', 'par3'.
    2. At least one of the following parameters must be specified: 'par1', 'par2', 'par3'.
    3. Mutually exclusive parameter(s) are specified: 'par1', 'par2'.
    4. Same as category 3
    5. Not implemented yet

    In PR #4370, the error messages look like:

    1. Missing required parameters: 'par1' and 'par2'.
    2. Missing required parameters: 'par1' or 'par3'.
    3. Conflicting parameters: 'par1' and 'par2'.
    4. Same as category 3
    5. 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.

  12. Chuan1937 commented on Jan 27, 2026

    @Chuan1937
    Member

    Ok,I'll open some new PRs to slove it based on single exception GMTParameterError

  13. seisman commented on Jan 29, 2026

    @seisman
    MemberAuthor

    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.

    Error types Preferred Parameter Name Alternative Parameter Names Error Message Template
    Required required must_have, needed Missing required parameter: '{param}'
    Missing required parameters: '{a}', '{b}'
    At least one is required at_least_one need_one_of, any_of, one_of, required_any Missing required parameters: '{a}', '{b}', '{c}'. Must specify at least one.
    Must specify at least one of the parameters: '{a}', '{b}', '{c}'
    Exactly one is required exactly_one mutually_exclusive Mutually exclusive parameters: '{a}', '{b}', '{c}'. Specify exactly one.
    At most one at_most_one mutually_exclusive Mutually exclusive parameters: '{a}', '{b}', '{c}'. Specify at most one
    Conflicting conflicts_with conflicts, incompatible_with Conflicting parameters: '{a}' cannot be used with '{b}', '{c}'
  14. seisman commented on Feb 4, 2026

    @seisman
    Author
  15. seisman commented on Feb 4, 2026

    @seisman
    MemberAuthor

    @Chuan1937 I think the following lines should also be updated with the at_most_one parameter. Please open a PR when you have time:

    pygmt/src/project.py:231:        raise GMTInvalidInput(msg)    
    pygmt/src/text.py:195:        raise GMTInvalidInput(msg)    
    pygmt/src/text.py:213:        raise GMTInvalidInput(msg)    
    pygmt/helpers/decorators.py:557:                    raise GMTInvalidInput(msg)    
    pygmt/helpers/decorators.py:573:                raise GMTInvalidInput(msg)    
    pygmt/helpers/decorators.py:581:                raise GMTInvalidInput(msg)    
    pygmt/helpers/decorators.py:589:                raise GMTInvalidInput(msg)    
    pygmt/helpers/decorators.py:825:                    raise GMTInvalidInput(msg)
    
  16. added this to the 0.19.0 milestone on Feb 4, 2026
  17. seisman commented on Feb 7, 2026

    @seisman
    MemberAuthor

    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.,

    if dpi is not None and surftype != "image":
    msg = "Parameter 'dpi' can only be used when 'surftype' is 'image'."
    raise GMTInvalidInput(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?

  18. Chuan1937 commented on Feb 7, 2026

    @Chuan1937
    Member

    “Invalid” distinguishes between input that exists but is formatted incorrectly and input that may be entirely missing. I believe it is preferable to GMTInputError.

  19. seisman commented on May 23, 2026

    @seisman
    MemberAuthor

    Here is an update:

    $ for e in \
        GMTError \
        GMTOSError \
        GMTCLibError \
        GMTCLibNotFoundError \
        GMTCLibNoSessionError \
        GMTVersionError \
        GMTInvalidInput \
        GMTImageComparisonFailure \
        GMTValueError \
        GMTParameterError \
        GMTTypeError;
    do
        n=$(grep -R --exclude-dir=tests --exclude="exceptions.py" "raise $e" pygmt | wc -l)
        echo "$e: $n"
    done
    GMTError: 0
    GMTOSError: 1
    GMTCLibError: 16
    GMTCLibNotFoundError: 1
    GMTCLibNoSessionError: 1
    GMTVersionError: 1
    GMTInvalidInput: 14
    GMTImageComparisonFailure: 1
    GMTValueError: 57
    GMTParameterError: 60
    GMTTypeError: 16
    
  20. modified the milestones: 0.19.0, 0.20.0 on Jun 30, 2026
  21. modified the milestones: 0.20.0, 0.21.0 on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions