From 92ea700b4f1c510fb2c9cd5b52f181ef59ebcb54 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Thu, 3 Nov 2022 20:25:49 -0500 Subject: [PATCH] Fixed optional dependency on azure-storage-blob --- CHANGELOG.md | 6 ++++++ planetary_computer/_adlfs.py | 12 ++++++++++-- planetary_computer/version.py | 2 +- scripts/cibuild | 2 +- setup.cfg | 3 ++- tests/test_adlfs.py | 6 ++++++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd2c2da..5e18f8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.4.9 + +## Bug fixes + +* Fixed `ImportError` when the optional dependency `azure-storage-blob` isn't installed. + # 0.4.8 ## New Features diff --git a/planetary_computer/_adlfs.py b/planetary_computer/_adlfs.py index f485947..45c4355 100644 --- a/planetary_computer/_adlfs.py +++ b/planetary_computer/_adlfs.py @@ -1,15 +1,15 @@ import typing -import azure.storage.blob from planetary_computer.sas import get_token if typing.TYPE_CHECKING: import adlfs + import azure.storage.blob def get_container_client( account_name: str, container_name: str -) -> azure.storage.blob.ContainerClient: +) -> "azure.storage.blob.ContainerClient": """ Get a :class:`azure.storage.blob.ContainerClient` with credentials. @@ -20,6 +20,14 @@ def get_container_client( The :class:`azure.storage.blob.ContainerClient` with the short-lived SAS token set as the credential. """ + try: + import azure.storage.blob + except ImportError as e: + raise ImportError( + "'planetary_computer.get_container_clinent' requires " + "the optional dependency 'azure-storage-blob'." + ) from e + token = get_token(account_name, container_name).token return azure.storage.blob.ContainerClient( f"https://{account_name}.blob.core.windows.net", diff --git a/planetary_computer/version.py b/planetary_computer/version.py index 2c237f2..f2efaf4 100644 --- a/planetary_computer/version.py +++ b/planetary_computer/version.py @@ -1,3 +1,3 @@ """Library version""" -__version__ = "0.4.8" +__version__ = "0.4.9" diff --git a/scripts/cibuild b/scripts/cibuild index edaeec5..b8a3ef6 100755 --- a/scripts/cibuild +++ b/scripts/cibuild @@ -20,7 +20,7 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then # Install/upgrade dependencies python -m pip install --upgrade pip pip install -r requirements-dev.txt - pip install -e .[adlfs] + pip install -e .[adlfs,azure] ./scripts/test fi diff --git a/setup.cfg b/setup.cfg index b85a129..32e14a1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = planetary-computer -version = 0.4.8 +version = 0.4.9 license_file = LICENSE author = microsoft author_email = planetarycomputer@microsoft.com @@ -21,6 +21,7 @@ install_requires = [options.extras_require] adlfs = adlfs +azure = azure-storage-blob [options.entry_points] console_scripts = diff --git a/tests/test_adlfs.py b/tests/test_adlfs.py index 978e4a2..ce3443f 100644 --- a/tests/test_adlfs.py +++ b/tests/test_adlfs.py @@ -13,6 +13,12 @@ def test_get_adlfs_filesystem_raises(monkeypatch: Any) -> None: planetary_computer.get_adlfs_filesystem("nrel", "oedi") +def test_get_container_client_raises(monkeypatch: Any) -> None: + monkeypatch.setitem(sys.modules, "azure", None) + with pytest.raises(ImportError): + planetary_computer.get_container_client("nrel", "oedi") + + def test_get_adlfs_filesystem() -> None: fs = planetary_computer.get_adlfs_filesystem("nrel", "oedi") assert fs.account_url == "https://nrel.blob.core.windows.net"