Skip to content

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.

"""


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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 16c15c9.

Comment on lines 100 to 101
value: Any,
description: str = "value",
Copy link
Member

@weiji14 weiji14 Jul 6, 2025

Choose a reason for hiding this comment

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

From your changes, it seems like the value parameter is used in a positional-only way. Do you want to enforce it with https://peps.python.org/pep-0570/#syntax-and-semantics, or still let value be a 'positional_or_keyword_parameters'?

Suggested change
value: Any,
description: str = "value",
value: Any,
/,
description: str = "value",

Comment on lines +112 to +116
msg = f"Invalid {description}: {value!r}."
if choices:
msg += f" Expected one of: {', '.join(repr(c) for c in choices)}."
if reason:
msg += f" {reason}"
Copy link
Member

Choose a reason for hiding this comment

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

These lines would probably be a good use of t-strings (Template strings, PEP 750), but it is for Python 3.14+

>>> raise GMTValueError("invalid", choices=GridType)
Traceback (most recent call last):
...
pygmt.exceptions.GMTValueError: Invalid value: 'invalid'. Expected one of: <GridType.CARTESIAN: 0>, <GridType.GEOGRAPHIC: 1>.
Copy link
Member

Choose a reason for hiding this comment

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

Error message is slightly different from the previous formatting GridRegistration.GRIDLINE (0) or GridRegistration.PIXEL (1), but hopefully ok.

Copy link
Member Author

Choose a reason for hiding this comment

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

<GridType.CARTESIAN: 0> is from the repr() output:

>>> from pygmt.enums import GridType

>>> repr(GridType.CARTESIAN)
<GridType.CARTESIAN: 0>

So I think it's better than previously custom string format.

@seisman seisman changed the title Add new exception GMTValueError for invalid values BREADKING: Raise GMTValueError exception for invalid values. Previously raise GMTInvalidInput Jul 9, 2025
@seisman
Copy link
Member Author

seisman commented Jul 9, 2025

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.

I've updated the PR title to indicate this is a breaking change.

@weiji14 weiji14 changed the title BREADKING: Raise GMTValueError exception for invalid values. Previously raise GMTInvalidInput BREAKING: Raise GMTValueError exception for invalid values. Previously raise GMTInvalidInput Jul 9, 2025
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.

I've updated the PR title to indicate this is a breaking change.

Breaking, not breadking 😆

@seisman seisman removed the needs review This PR has higher priority and needs review. label Jul 9, 2025
@seisman seisman merged commit 4586879 into main Jul 9, 2025
23 of 24 checks passed
@seisman seisman deleted the exception/valueerror branch July 9, 2025 22:23
seisman added a commit that referenced this pull request Jul 16, 2025
…y raise GMTInvalidInput (#3985)

Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants