Skip to content

Commit 1a0c89c

Browse files
authored
Add convenience method for accessing pystac_client (#1365)
* Add convenience method for accessing pystac_client * Write test that mocks missing import * Ignore mypy * Add to docs * Update changelog
1 parent a88906b commit 1a0c89c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Add netCDF to pystac.media_type ([#1386](https://github.com/stac-utils/pystac/pull/1386))
8+
- Add convenience method for accessing pystac_client ([#1365](https://github.com/stac-utils/pystac/pull/1365))
89

910
### Changed
1011

docs/api.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ as to serialize and deserialize STAC object to and from JSON.
8686
* :class:`pystac.stac_io.DefaultStacIO`: The default :class:`pystac.StacIO`
8787
implementation used throughout the library.
8888

89+
Client
90+
------
91+
92+
A convenience method for accessing `pystac-client <https://github.com/stac-utils/pystac-client>`__
93+
94+
**Example:**
95+
96+
.. code-block:: python
97+
98+
from pystac.client import Client
99+
100+
89101
Extensions
90102
----------
91103

pystac/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# mypy: ignore-errors
2+
3+
_import_error_message = (
4+
"pystac-client is not installed.\n\n"
5+
"Please install pystac-client:\n\n"
6+
" pip install pystac-client"
7+
)
8+
9+
try:
10+
from pystac_client import * # noqa: F403
11+
except ImportError as e:
12+
if e.msg == "No module named 'pystac_client'":
13+
raise ImportError(_import_error_message) from e
14+
else:
15+
raise
16+
17+
18+
def __getattr__(value: str):
19+
try:
20+
import pystac_client
21+
except ImportError as e:
22+
raise ImportError(_import_error_message) from e
23+
return getattr(pystac_client, value)

tests/test_pystac_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# mypy: ignore-errors
2+
import builtins
3+
import sys
4+
from types import ModuleType
5+
from typing import Any
6+
7+
import pytest
8+
9+
real_import = builtins.__import__
10+
11+
try:
12+
import pystac_client # noqa:F401
13+
14+
HAS_PYSTAC_CLIENT = True
15+
except ImportError:
16+
HAS_PYSTAC_CLIENT = False
17+
18+
19+
@pytest.mark.skipif(not HAS_PYSTAC_CLIENT, reason="requires pystac_client")
20+
def test_import_pystac_client_when_available() -> None:
21+
from pystac.client import Client
22+
23+
assert Client.__doc__
24+
25+
26+
# copied from
27+
# http://materials-scientist.com/blog/2021/02/11/mocking-failing-module-import-python/
28+
def monkey_import_importerror(name: str, *args: Any, **kwargs: Any) -> ModuleType:
29+
if name == "pystac_client":
30+
raise ImportError(f"No module named '{name}'")
31+
return real_import(name, *args, **kwargs)
32+
33+
34+
def test_import_pystac_client_when_not_available(monkeypatch: Any) -> None:
35+
if HAS_PYSTAC_CLIENT:
36+
monkeypatch.delitem(sys.modules, "pystac_client", raising=False)
37+
monkeypatch.setattr(builtins, "__import__", monkey_import_importerror)
38+
39+
with pytest.raises(ImportError):
40+
from pystac_client import Client # noqa:F401
41+
42+
with pytest.raises(ImportError, match="Please install pystac-client:"):
43+
from pystac.client import Client # noqa:F401,F811

0 commit comments

Comments
 (0)