-
Notifications
You must be signed in to change notification settings - Fork 245
Improve the way to import optional modules #2809
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
Changes from 5 commits
5deebf8
c98c103
b1c40b0
cdf162c
0ec058f
3230ec4
ef63776
75ea9a4
b37b18c
7da73b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,22 +3,19 @@ | |
|
|
||
| Doesn't include the plotting commands which have their own test files. | ||
| """ | ||
| import importlib | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| try: | ||
| import IPython | ||
| except ImportError: | ||
| IPython = None # pylint: disable=invalid-name | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option looking at https://stackoverflow.com/questions/61384752/how-to-type-hint-with-an-optional-import/77341843#77341843 seems to be something like: IPython: Any = None
try:
import IPython
except ImportError:
passThough not sure if this is recommended.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unclear what the recommended way is for importing an optional module. It seems sometimes pandas and xarray use the but sometimes they also use the old way, which mypy should complain, e.g.,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at mypy (e.g. python/mypy#1297 (comment)), they also seem to recommend a try-except-else pattern? Something like: try:
import IPython as _IPython
except ImportError:
IPython = None
else:
IPython = _IPython
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's officially recommended and it's also more difficult to understand than
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let's go with That said, we should probably decide first if adding
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| import numpy as np | ||
| import numpy.testing as npt | ||
| import pytest | ||
| from pygmt import Figure, set_display | ||
| from pygmt.exceptions import GMTError, GMTInvalidInput | ||
| from pygmt.helpers import GMTTempFile | ||
|
|
||
| HAS_IPYTHON = bool(importlib.util.find_spec("IPython")) | ||
|
|
||
|
|
||
| def test_figure_region(): | ||
| """ | ||
|
|
@@ -313,7 +310,7 @@ def test_figure_savefig_worldfile(): | |
| fig.savefig(fname=imgfile.name, worldfile=True) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(IPython is None, reason="run when IPython is installed") | ||
| @pytest.mark.skipif(not HAS_IPYTHON, reason="run when IPython is installed") | ||
| def test_figure_show(): | ||
| """ | ||
| Test that show creates the correct file name and deletes the temp dir. | ||
|
|
@@ -354,7 +351,7 @@ def test_figure_show_invalid_method(): | |
| fig.show(method="test") | ||
|
|
||
|
|
||
| @pytest.mark.skipif(IPython is not None, reason="run without IPython installed") | ||
| @pytest.mark.skipif(HAS_IPYTHON, reason="run without IPython installed") | ||
| def test_figure_show_notebook_error_without_ipython(): | ||
| """ | ||
| Test to check if an error is raised when display method is 'notebook', but | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.