-
Notifications
You must be signed in to change notification settings - Fork 229
BREAKING: Raise GMTValueError exception for invalid values. Previously raise GMTInvalidInput #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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
78590bc
Add custom exception GMTValueError
seisman a590758
Use the new exception
seisman d9e5a64
Merge branch 'main' into exception/valueerror
seisman dc305d3
Fix
seisman 37b1b84
Use GMTValueError for invalid transparent value
seisman a370d6b
Use GMTValueError in earth_relief
seisman c68cc9e
Fix tests
seisman b4daeb7
Fix a typo
seisman b101930
Merge branch 'main' into exception/valueerror
seisman 866e45a
Let value to a postional-only parameter
seisman ffcbd28
Add a new test for choices from enums
seisman e66cdc5
Use full error messages in docstrings
seisman e367043
value is position-only
seisman 16c15c9
Subclass from both GMTError and ValueError
seisman 56cbf54
Merge branch 'main' into exception/valueerror
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
All exceptions derive from GMTError. | ||
""" | ||
|
||
from collections.abc import Iterable | ||
from typing import Any | ||
|
||
|
||
class GMTError(Exception): | ||
""" | ||
|
@@ -51,3 +54,64 @@ class GMTImageComparisonFailure(AssertionError): # noqa: N818 | |
""" | ||
Raised when a comparison between two images fails. | ||
""" | ||
|
||
|
||
class GMTValueError(GMTError, ValueError): | ||
""" | ||
Raised when an invalid value is passed to a function/method. | ||
|
||
Parameters | ||
---------- | ||
value | ||
The invalid value. | ||
description | ||
The description of the value. | ||
choices | ||
The valid choices for the value. | ||
reason | ||
The detailed reason why the value is invalid. | ||
|
||
Examples | ||
-------- | ||
>>> raise GMTValueError("invalid") | ||
Traceback (most recent call last): | ||
... | ||
pygmt.exceptions.GMTValueError: Invalid value: 'invalid'. | ||
>>> raise GMTValueError("invalid", description="constant name") | ||
Traceback (most recent call last): | ||
... | ||
pygmt.exceptions.GMTValueError: Invalid constant name: 'invalid'. | ||
>>> raise GMTValueError("invalid", choices=["a", "b", 1, 2]) | ||
Traceback (most recent call last): | ||
... | ||
pygmt.exceptions.GMTValueError: Invalid value: 'invalid'. Expected one of: 'a', 'b', 1, 2. | ||
>>> raise GMTValueError("invalid", choices=["a", 0, True, False, None]) | ||
Traceback (most recent call last): | ||
... | ||
pygmt.exceptions.GMTValueError: Invalid value: 'invalid'. Expected one of: 'a', 0, True, False, None. | ||
|
||
>>> from pygmt.enums import GridType | ||
>>> 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>. | ||
>>> raise GMTValueError("invalid", reason="Explain why it's invalid.") | ||
Traceback (most recent call last): | ||
... | ||
pygmt.exceptions.GMTValueError: Invalid value: 'invalid'. Explain why it's invalid. | ||
""" # noqa: W505 | ||
|
||
def __init__( | ||
self, | ||
value: Any, | ||
/, | ||
description: str = "value", | ||
choices: Iterable[Any] | None = None, | ||
reason: str | None = None, | ||
): | ||
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}" | ||
Comment on lines
+112
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
super().__init__(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 therepr()
output:So I think it's better than previously custom string format.