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

Raise ModuleNotFoundError when unavailable #290

Merged
merged 10 commits into from
Oct 22, 2020

Conversation

akihironitta
Copy link
Contributor

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

What does this PR do?

Fixes #265.

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Oct 20, 2020

Hello @akihironitta! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-10-22 16:14:00 UTC

@mergify mergify bot requested a review from Borda October 20, 2020 12:00
@codecov
Copy link

codecov bot commented Oct 20, 2020

Codecov Report

Merging #290 into master will decrease coverage by 0.11%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #290      +/-   ##
==========================================
- Coverage   82.06%   81.94%   -0.12%     
==========================================
  Files          97       98       +1     
  Lines        5441     5501      +60     
==========================================
+ Hits         4465     4508      +43     
- Misses        976      993      +17     
Flag Coverage Δ
#cpu 23.46% <16.66%> (+0.03%) ⬆️
#pytest 23.46% <16.66%> (+0.03%) ⬆️
#unittests 81.40% <77.77%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pl_bolts/datasets/imagenet_dataset.py 20.11% <50.00%> (+0.95%) ⬆️
pl_bolts/datasets/ssl_amdim_datasets.py 76.31% <100.00%> (+1.99%) ⬆️
pl_bolts/utils/semi_supervised.py 96.82% <100.00%> (+0.10%) ⬆️
pl_bolts/models/__init__.py 100.00% <0.00%> (ø)
pl_bolts/models/vision/unet.py 98.03% <0.00%> (ø)
pl_bolts/models/vision/__init__.py 100.00% <0.00%> (ø)
pl_bolts/models/vision/segmentation.py 65.38% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d61e211...1e60395. Read the comment docs.

@Borda Borda added the enhancement New feature or request label Oct 20, 2020
@akihironitta akihironitta changed the title [WIP] Raise ModuleNotFoundError when modules are unavailable Raise ModuleNotFoundError when unavailable Oct 20, 2020
@Borda
Copy link
Member

Borda commented Oct 20, 2020

@akihironitta could you please explore @awaelchli recommendation about importing without try/catch

@akihironitta
Copy link
Contributor Author

@Borda Thanks for reviewing as always!
As for optional imports, Lightning-AI/pytorch-lightning#2266 (comment) suggests:

if importlib.util.find_spec("some_package") is not None:
    from some_package import my_function

and after that, Lightning-AI/pytorch-lightning#2877 tried implementing pytorch_lightning.utilities.is_some_package_available(), but it's closed without getting merged.

Is there a preferred way of importing optional packages without try/catch? Should I wait for Lightning-AI/pytorch-lightning#2877 to get merged or directly use Lightning-AI/pytorch-lightning#2266 (comment)?

@awaelchli
Copy link
Contributor

@akihironitta
You could do this:

SKLEARN_AVAILABLE = importlib.util.find_spec("some_package") is not None

and then wherever you need it:

if SKLEARN_AVAILABLE:
    # import something, warn or raise

This is my recommendation when you need a variable telling you if the package is available without the need to actually import it.
You can also use ModuleNotFoundError, nothing wrong with that, but when a variable like IS_MODULE_AVAILABLE is required anyway, I think using importlib is cleaner.

Hope this helps.

@akihironitta
Copy link
Contributor Author

@awaelchli Thank you for the suggestion! I'll start using importlib here.

@akihironitta akihironitta marked this pull request as ready for review October 22, 2020 16:44
@Borda Borda merged commit eee7684 into Lightning-Universe:master Oct 22, 2020
@akihironitta akihironitta deleted the raise-when-unavailable branch October 22, 2020 22:51
@Borda Borda added this to the v0.3 milestone Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NameError: name 'shuffle' is not defined
4 participants