From 1261e6058e8e9137d5c7c7f16e0d18e888a6d9cf Mon Sep 17 00:00:00 2001 From: Jon Duckworth Date: Tue, 17 May 2022 19:56:06 -0400 Subject: [PATCH] Return None from Asset.get_absolute_href when owning Item self HREF is NOne (#808) * Return None from Asset.get_absolute_href for owner with no self HREF * Remove unused mypy config * Update docstring --- mypy.ini | 3 --- pystac/asset.py | 10 ++++++---- tests/test_item.py | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mypy.ini b/mypy.ini index 90581decb..5fcd1391e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -10,6 +10,3 @@ ignore_missing_imports = True [mypy-setuptools.*] ignore_missing_imports = True - -[mypy-sphinx.util] -ignore_missing_imports = True diff --git a/pystac/asset.py b/pystac/asset.py index d027721f8..7402054c9 100644 --- a/pystac/asset.py +++ b/pystac/asset.py @@ -92,7 +92,8 @@ def get_absolute_href(self) -> Optional[str]: """Gets the absolute href for this asset, if possible. If this Asset has no associated Item, and the asset HREF is a relative path, - this method will return None. + this method will return ``None``. If the Item that owns the Asset has no + self HREF, this will also return ``None``. Returns: str: The absolute HREF of this asset, or None if an absolute HREF could not @@ -102,9 +103,10 @@ def get_absolute_href(self) -> Optional[str]: return self.href else: if self.owner is not None: - return utils.make_absolute_href(self.href, self.owner.get_self_href()) - else: - return None + item_self = self.owner.get_self_href() + if item_self is not None: + return utils.make_absolute_href(self.href, item_self) + return None def to_dict(self) -> Dict[str, Any]: """Generate a dictionary representing the JSON of this Asset. diff --git a/tests/test_item.py b/tests/test_item.py index 904ac0be6..7788dbc4e 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -72,14 +72,28 @@ def test_set_self_href_none_ignores_relative_asset_hrefs(self) -> None: self.assertFalse(is_absolute_href(asset.href)) def test_asset_absolute_href(self) -> None: + item_path = TestCases.get_path("data-files/item/sample-item.json") item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) + item.set_self_href(item_path) rel_asset = Asset("./data.geojson") rel_asset.set_owner(item) - expected_href = os.path.abspath("./data.geojson") + expected_href = os.path.abspath( + os.path.join(os.path.dirname(item_path), "./data.geojson") + ) actual_href = rel_asset.get_absolute_href() self.assertEqual(expected_href, actual_href) + def test_asset_absolute_href_no_item_self(self) -> None: + item_dict = self.get_example_item_dict() + item = Item.from_dict(item_dict) + assert item.get_self_href() is None + + rel_asset = Asset("./data.geojson") + rel_asset.set_owner(item) + actual_href = rel_asset.get_absolute_href() + self.assertEqual(None, actual_href) + def test_extra_fields(self) -> None: item = pystac.Item.from_file( TestCases.get_path("data-files/item/sample-item.json")