-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
ENH: replace try/except ImportError
blocks with if HAS_X
#16353
base: main
Are you sure you want to change the base?
ENH: replace try/except ImportError
blocks with if HAS_X
#16353
Conversation
try/except ImportError
blocks with if HAS_X
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
33cbeae
to
7c4dabc
Compare
@@ -899,14 +900,9 @@ def _is_dask_array(data): | |||
We avoid importing dask unless it is likely it is a dask array, | |||
so that non-dask code is not slowed down. | |||
""" | |||
if not hasattr(data, "compute"): | |||
if not HAS_DASK or not hasattr(data, "compute"): |
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.
See above comment as choice was deliberate
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.
Or does this PR avoid importing the optional deps to define HAS_?
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.
Not this PR, but I did exactly that a couple months back in #15771
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.
Ok then the comment above this can probably be removed/updated - thanks!
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 !
7c4dabc
to
3245469
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.
Good for io.ascii, table, time.
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.
LGTM for convolution
, stats
, and visualization
.
except ImportError: | ||
raise ImportError( | ||
if not HAS_JPLEPHEM: | ||
raise ModuleNotFoundError( |
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.
Can we maybe keep the error type the same?
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.
ModuleNotFoundError
is a subclass of ImportError
, so this change is backwards compatible.
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 can approve for coordinates
.
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.
In the places where ImportError
is now ModuleNotFoundError
, do they need a change log? Even if the new error is backward compatible, it is still a change, right?
Also in stats
, looks like ModuleNotFoundError
is newly introduced (or at least has a different message now). (But from the diff, not sure what it was before since it just assumed scipy was available.)
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.
Thanks, that is a very nice cleanup. I feel the use of ModuleNotFound
is an improvement too - and even suggest we use that in place of a ValueError
in one case.
But good to go as is!
In my opinion it's borderline not user-visible (in the sense that I doubt anyone would notice) so I don't think it's worth a mention in the changelog, but obviously I'm happy to add one still if you guys think otherwise ! |
from scipy.misc import comb, factorial | ||
if not HAS_SCIPY: | ||
raise ModuleNotFoundError( | ||
"scipy is required for kuiper_false_positive_probability" |
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.
This changes the message from:
ImportError: cannot import name 'comb' from 'scipy.<module>'
to:
ModuleNotFoundError: scipy is required for kuiper_false_positive_probability
I'll defer to @larrybradley whether this needs a change log or not.
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.
Technically, it is an API change, but I think it's an inconsequential one. I doubt anyone is running this code and checking for the error type or message.
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.
OK I think everyone else think we do not need a change log, so let's go with the majority. Thanks for the feedback!
There are already 4 approvals. Are we still waiting for someone in particular? If not, a maintainer can merge. Thanks! |
Thanks @eerovaher for adding a bullet list to track approvals, and sorry I didn't anticipate that this PR would ping so many maintainers at once. |
Re: pings -- It follows the rules at https://github.com/astropy/astropy/blob/main/.github/CODEOWNERS (based on opt-in by individual core maintainers). FYI. |
FWIW, changes in io.misc are fine |
Description
Motivation: #16351 showed that we have hidden optional dependencies in a couple places, showing as modules that we attempt to import in a more-or-less recoverable fashion with plain try/except blocks such as
but that may not all be declared as optional dependencies in package metadata.
In this PR, I'm going for the bulk of these blocks (focusing on packages that are already declared as optional deps) and replacing them with our usual idiom
It some places, it leads to a user-visible improvement with how exceptions are chained, but the main idea is to make problematic occurrences of
except ImportError
like the one causing #16351 easier to spot.Approvals by sub-package
convolution
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)coordinates
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)io.ascii
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)io.fits
io.misc
modeling
nddata
stats
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)table
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)time
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)timeseries
utils
visualization
ENH: replacetry/except ImportError
blocks withif HAS_X
#16353 (review)