Skip to content

Commit

Permalink
Return None from Asset.get_absolute_href when owning Item self HREF i…
Browse files Browse the repository at this point in the history
…s NOne (#808)

* Return None from Asset.get_absolute_href for owner with no self HREF

* Remove unused mypy config

* Update docstring
  • Loading branch information
duckontheweb authored May 17, 2022
1 parent 3526b65 commit 1261e60
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ ignore_missing_imports = True

[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-sphinx.util]
ignore_missing_imports = True
10 changes: 6 additions & 4 deletions pystac/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 1261e60

Please sign in to comment.