Skip to content

Commit

Permalink
Use importlib.util.find_spec for modules that are not used (ruff F401)
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Nov 14, 2023
1 parent c98c103 commit b1c40b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
11 changes: 3 additions & 8 deletions pygmt/src/tilemap.py
@@ -1,17 +1,12 @@
"""
tilemap - Plot XYZ tile maps.
"""
import importlib

from pygmt.clib import Session
from pygmt.datasets.tile_map import load_tile_map
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias

try:
import rioxarray

_has_rioxarray = True
except ImportError:
_has_rioxarray = False


@fmt_docstring
@use_alias(
Expand Down Expand Up @@ -116,7 +111,7 @@ def tilemap(
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

if not _has_rioxarray:
if importlib.util.find_spec("rioxarray") is None:

Check warning on line 114 in pygmt/src/tilemap.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/tilemap.py#L114

Added line #L114 was not covered by tests
raise ImportError(
"Package `rioxarray` is required to be installed to use this function. "
"Please use `python -m pip install rioxarray` or "
Expand Down
15 changes: 5 additions & 10 deletions pygmt/tests/test_figure.py
Expand Up @@ -3,24 +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

_has_ipython = True
except ImportError:
_has_ipython = False


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():
"""
Expand Down Expand Up @@ -315,7 +310,7 @@ def test_figure_savefig_worldfile():
fig.savefig(fname=imgfile.name, worldfile=True)


@pytest.mark.skipif(not _has_ipython, 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.
Expand Down Expand Up @@ -356,7 +351,7 @@ def test_figure_show_invalid_method():
fig.show(method="test")


@pytest.mark.skipif(_has_ipython, 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
Expand Down

0 comments on commit b1c40b0

Please sign in to comment.