Skip to content

Refactor optional dependency checking - #11490

Merged
pllim merged 11 commits into
astropy:mainfrom
adrn:utils/optional-deps
Apr 7, 2021
Merged

pllim merged 11 commits into
astropy:mainfrom
adrn:utils/optional-deps

Conversation

@adrn

@adrn adrn commented Apr 7, 2021 •

Copy link
Copy Markdown
Member

This PR centralizes the definition of HAS_x optional dependency package import checking, such as HAS_matplotlib. Now that astropy requires Python 3.7+, we can make use of lazy imports as implemented in PEP 0562 and as demonstrated by @pllim in this comment. This implementation is similar to what @pllim suggested, but with some extra flexibility to support flags that check subpackage imports that have different behavior than the top-level package import (e.g., matplotlib.pyplot). The actual implementation is in the new file astropy/utils/compat/optional_deps.py and was added in the first two commits.

I decided to go with using the actual package names in the flag names (e.g., HAS_matplotlib not HAS_MATPLOTLIB) because there are cases where the case matters for a package name, like PIL or IPython.

This fixes #4630, and is relevant for a comment in #11361

cc @nstarman

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! Just a couple of comments on the actual code (I didn't review the rest pending one of my comments to use upper case).

Comment thread astropy/utils/compat/optional_deps.py Outdated
Comment thread astropy/utils/compat/optional_deps.py Outdated
@adrn
adrn force-pushed the utils/optional-deps branch from 910cf6b to 22f5c26 Compare April 7, 2021 12:19

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, I went through the whole list and didn't see any problems. The only thing left are the docs: https://docs.astropy.org/en/stable/development/testguide.html#tests-requiring-optional-dependencies

@adrn

adrn commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

Great, thanks @taldcroft for having the patience to look through all of the changes! I updated the docs, but currently astropy.utils.compat is not automodapi'd, so I don't link explicitly to the module with the list of supported flags.

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good from my perspective, thanks!!

@adrn

adrn commented Apr 7, 2021 •

Copy link
Copy Markdown
Member Author

Hm, did RTD fail because I used [ci skip]? Maybe @pllim knows?

@pllim

pllim commented Apr 7, 2021

Copy link
Copy Markdown
Member

RTD failure might be transient... try another push (forced or not)

@pllim pllim removed the Affects-dev PRs and issues that do not impact an existing Astropy release label Apr 7, 2021
@mhvk

mhvk commented Apr 7, 2021

Copy link
Copy Markdown
Contributor

Just thinking that if we are going to change things around, doing it once only might be better... But agreed that it really is out of scope and we might as well stick with this nice improvement! After all, it is not as if this precludes the scheme I suggested.

@adrn
adrn force-pushed the utils/optional-deps branch from 5929e85 to 621b32a Compare April 7, 2021 18:18
@adrn

adrn commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

OK, just made a small change based on a suggestion from @mhvk - assuming the relevant tests pass, I think this is ready!

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great! Certainly seems more sane than before.

Just curious: Did you time the astropy import to make sure this does not have bad performance side effects? Sometimes we get complains that importing astropy is too slow as it is.

Some minor comments below.

Comment thread astropy/io/ascii/ui.py
import yaml # noqa
HAS_YAML = True
except ImportError:
HAS_YAML = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm... What was the original purpose of this import? Was it to let people use astropy.io.ascii.ui.HAS_YAML?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's not documented anywhere that I could find as public API -- possible vestigial?

Comment thread astropy/modeling/fitting.py Outdated
Comment thread astropy/modeling/tests/test_fitters.py Outdated
Comment thread astropy/table/soco.py
from astropy.utils.compat.optional_deps import HAS_SORTEDCONTAINERS

try:
if HAS_SORTEDCONTAINERS:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will renaming HAS_SOCO to HAS_SORTEDCONTAINERS break anything downstream? Maybe @taldcroft knows.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think these things were ever exposed as public API... Also don't see any reference to this in docs. Also don't see any uses of this in other github projects https://github.com/search?p=1&q=HAS_SOCO&type=Code

Comment thread docs/development/testguide.rst Outdated
test if the dependencies are not present. The following example shows
how this should be done::
For tests that test functions or methods that require optional dependencies
(e.g. Scipy), pytest should be instructed to skip the test if the dependencies

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick.

Suggested change
(e.g. Scipy), pytest should be instructed to skip the test if the dependencies
(e.g., Scipy), pytest should be instructed to skip the test if the dependencies

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, but I'm skipping CI on this!

Comment thread docs/development/testguide.rst Outdated
Comment on lines +650 to +651
are not present, as the Astropy tests should succeed even if an optional
dependency is not present. Astropy provides a list of boolean flags that test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Capitalized "Astropy" is for the project, not the package.

Suggested change
are not present, as the Astropy tests should succeed even if an optional
dependency is not present. Astropy provides a list of boolean flags that test
are not present, as the ``astropy`` tests should succeed even if an optional
dependency is not present. ``astropy`` provides a list of boolean flags that test

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, yes, @lglattly should shake her finger at me :) -- skipping CI for this too...but I'll wait for the last Cron test to finish.

@adrn

adrn commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

Failures in ppc64le and s390x look unrelated. Other tests (except link check) pass. I think it's overkill to run the cron tests again, and I have some doc changes to push from @pllim, so I will [ci skip] - sound ok?

@adrn

adrn commented Apr 7, 2021

Copy link
Copy Markdown
Member Author

Well, I guess #11091 got merged, so never mind!

@pllim

pllim commented Apr 7, 2021

Copy link
Copy Markdown
Member

Sorry... 😬

@pllim pllim removed the Extra CI Run cron CI as part of PR label Apr 7, 2021
@pllim

pllim commented Apr 7, 2021

Copy link
Copy Markdown
Member

I don't think a rebase warrants more exotic tests, so I removed the Extra CI label.

adrn added 10 commits April 7, 2021 17:00
matplotlib.pyplot

bleach

bottleneck

bs4

bz2

h5py

preserve extreme laziness of imports in connect.py

jplephem

mpmath, some scipy

pandas

pkg_resources

pytz

scipy

sortedcontainers

yaml

fix some accidentally removed imports

one more removed import

revert cased variable names using actual package names

revert error message to standard attribute error

oops, typo
@adrn
adrn force-pushed the utils/optional-deps branch from 621b32a to 5945357 Compare April 7, 2021 21:04

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am still curious about the import timing though I don't expect anything shocking, so approving!

@pllim pllim added the zzz 💤 merge-when-ci-passes Do not use: We have auto-merge option now. label Apr 7, 2021
@pllim

pllim commented Apr 7, 2021

Copy link
Copy Markdown
Member

Wut...? I saw this error in a different PR too. @saimn , do you know what's going on? Maybe we should just merge #10877 and be done with it...

>       assert is_unedited_config_file(content)
E       AssertionError: assert False
E        +  where False = <function is_unedited_config_file at 0x7f282632ec10>('\n# Use Unicode characters when outputting values, and writing widgets to the\n# console.\nunicode_output = False\n[u...non-critical\nnoncrit_warnings = W03, W06, W07, W09, W10, W15, W17, W20, W21, W22, W27, W28, W29, W41, W42, W48, W50\n')

@pllim

pllim commented Apr 7, 2021

Copy link
Copy Markdown
Member

Well, now master has the same failure too. Let's just merge this. There is no reason to delay.

@pllim
pllim merged commit b478b55 into astropy:main Apr 7, 2021
@saimn

saimn commented Apr 7, 2021

Copy link
Copy Markdown
Contributor

That was the changes in the astropy.0.3.cfg test files, before the config PR the code was checking hashes for those files.

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.

Refactor optional dependencies flags into utils module

5 participants