Skip to content

Add new exception GMTValueError for invalid values #3985

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Jun 20, 2025

Add new exception GMTValueError. The error message will be like

Invalid value: 'xxx'. Expected one of: "a", "b", 0, 1. Detailed reasons explaining why it's invalid.

Address #3984 and #3707.

@seisman seisman added the enhancement Improving an existing feature label Jun 20, 2025
@seisman seisman force-pushed the exception/valueerror branch 2 times, most recently from ddf56de to efa6715 Compare June 21, 2025 04:15
@seisman seisman changed the title POC: Add custom exception GMTValueError POC: Add new exception GMTValueError for invalid values Jun 21, 2025
@seisman seisman force-pushed the exception/valueerror branch 15 times, most recently from 340b235 to 8472d86 Compare June 23, 2025 05:01
@seisman seisman marked this pull request as ready for review June 23, 2025 05:31
@seisman seisman changed the title POC: Add new exception GMTValueError for invalid values Add new exception GMTValueError for invalid values Jun 23, 2025
@seisman seisman added this to the 0.17.0 milestone Jun 23, 2025
@seisman seisman force-pushed the exception/valueerror branch from 8472d86 to a590758 Compare June 24, 2025 02:27
@seisman seisman added the needs review This PR has higher priority and needs review. label Jun 24, 2025
@seisman seisman requested a review from a team July 5, 2025 05:35
Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking if this should be marked as a breaking change, because the exception type changes from GMTInvalidInput to GMTValueError, and code like try: ... except GMTInvalidInput: ... would break. But unsure if users even have code like that which matches on GMTInvalidInput.

Other than that, some minor suggestions/questions below.

@@ -51,3 +54,57 @@ class GMTImageComparisonFailure(AssertionError): # noqa: N818
"""
Raised when a comparison between two images fails.
"""


class GMTValueError(GMTError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be subclassed from both GMTError and ValueError?

Traceback (most recent call last):
...
pygmt...GMTValueError: Invalid .... Explain why it's invalid.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include another test where the input to choices is an enum like GridRegistration or GridType?

Comment on lines -183 to +184
msg = (
f"Invalid grid registration: '{value}'. Should be either "
"GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1)."
raise GMTValueError(
value, description="grid registration", choices=GridRegistration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highlighting this for the above comment, to check that enums like GridRegistration are formatted properly. I.e. the error should still print "GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1)" per #3696 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature needs review This PR has higher priority and needs review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants