Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

neutrinoceros
Copy link
Contributor

@neutrinoceros neutrinoceros commented Apr 29, 2024

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

try:
    import x
except ImportError:
    ...
else:
    ...

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

if HAS_X:
    ...
else:
    ...

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.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

Approvals by sub-package

Copy link

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@@ -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"):
Copy link
Member

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

Copy link
Member

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_?

Copy link
Contributor Author

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

Copy link
Member

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done !

Copy link
Member

@taldcroft taldcroft left a 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.

Copy link
Member

@larrybradley larrybradley left a 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.

@pllim pllim added this to the v7.0.0 milestone Apr 29, 2024
except ImportError:
raise ImportError(
if not HAS_JPLEPHEM:
raise ModuleNotFoundError(
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

@eerovaher eerovaher left a 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.

Copy link
Member

@pllim pllim left a 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.)

Copy link
Contributor

@mhvk mhvk left a 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!

astropy/utils/xml/writer.py Show resolved Hide resolved
@neutrinoceros
Copy link
Contributor Author

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?

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"
Copy link
Member

@pllim pllim Apr 29, 2024

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.

Copy link
Member

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.

Copy link
Member

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!

@pllim
Copy link
Member

pllim commented Apr 29, 2024

There are already 4 approvals. Are we still waiting for someone in particular? If not, a maintainer can merge. Thanks!

@neutrinoceros
Copy link
Contributor Author

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.

@pllim
Copy link
Member

pllim commented Apr 30, 2024

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.

@matteobachetti
Copy link
Contributor

FWIW, changes in io.misc are fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants