ENH: replace try/except ImportError blocks with if HAS_X#16353
ENH: replace try/except ImportError blocks with if HAS_X#16353pllim merged 1 commit intoastropy:mainfrom
try/except ImportError blocks with if HAS_X#16353Conversation
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
| 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.
See above comment as choice was deliberate
There was a problem hiding this comment.
Or does this PR avoid importing the optional deps to define HAS_?
There was a problem hiding this comment.
Not this PR, but I did exactly that a couple months back in #15771
There was a problem hiding this comment.
Ok then the comment above this can probably be removed/updated - thanks!
7c4dabc to
3245469
Compare
taldcroft
left a comment
There was a problem hiding this comment.
Good for io.ascii, table, time.
larrybradley
left a comment
There was a problem hiding this comment.
LGTM for convolution, stats, and visualization.
| except ImportError: | ||
| raise ImportError( | ||
| if not HAS_JPLEPHEM: | ||
| raise ModuleNotFoundError( |
There was a problem hiding this comment.
Can we maybe keep the error type the same?
There was a problem hiding this comment.
ModuleNotFoundError is a subclass of ImportError, so this change is backwards compatible.
eerovaher
left a comment
There was a problem hiding this comment.
I can approve for coordinates.
pllim
left a comment
There was a problem hiding this comment.
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.)
mhvk
left a comment
There was a problem hiding this comment.
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.
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.
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.
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 |
|
Do we have enough approvals / has it been long enough waiting / can we merge this in? |
|
It's been so long... does it need a rebase? |
e539b5f to
7893fe2
Compare
|
Dunno, but better safe than sorry: rebased ! |
|
Can you please rebase one more time? Thanks for your patience! |
7893fe2 to
c6aafdf
Compare
|
done |
|
Thanks! |
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 ImportErrorlike the one causing #16351 easier to spot.Approvals by sub-package
convolutionENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)coordinatesENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)io.asciiENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)io.fitsio.miscmodelingnddatastatsENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)tableENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)timeENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)timeseriesutilsvisualizationENH: replacetry/except ImportErrorblocks withif HAS_X#16353 (review)