-
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
Conversation
ddf56de
to
efa6715
Compare
340b235
to
8472d86
Compare
8472d86
to
a590758
Compare
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.
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.
pygmt/exceptions.py
Outdated
""" | ||
|
||
|
||
class GMTValueError(GMTError): |
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.
Should this be subclassed from both GMTError
and ValueError
?
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.
Done in 16c15c9.
value: Any, | ||
description: str = "value", |
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.
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'?
value: Any, | |
description: str = "value", | |
value: Any, | |
/, | |
description: str = "value", |
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
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}" |
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.
>>> 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>. |
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 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.
I've updated the PR title to indicate this is a breaking change. |
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.
I've updated the PR title to indicate this is a breaking change.
Breaking, not breadking 😆
…y raise GMTInvalidInput (#3985) Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Add new exception GMTValueError. The error message will be like
Address #3984 and #3707.