Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow HREF when opening files #1234

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- More permissive schema_uri matching to allow future versions of extension schemas ([#1231](https://github.com/stac-utils/pystac/pull/1231))

### Fixed

- Typing of `href` arguments ([#1234](https://github.com/stac-utils/pystac/pull/1234))

## [v1.8.4] - 2023-09-22

### Added
Expand Down
3 changes: 2 additions & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)
from pystac.stac_object import STACObject, STACObjectType
from pystac.utils import (
HREF,
StringEnum,
is_absolute_href,
make_absolute_href,
Expand Down Expand Up @@ -1245,7 +1246,7 @@ def full_copy(

@classmethod
def from_file(
cls: Type[C], href: str, stac_io: Optional[pystac.StacIO] = None
cls: Type[C], href: HREF, stac_io: Optional[pystac.StacIO] = None
) -> C:
if stac_io is None:
stac_io = pystac.StacIO.default()
Expand Down
5 changes: 3 additions & 2 deletions pystac/item_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pystac.errors import STACTypeError
from pystac.html.jinja_env import get_jinja_env
from pystac.serialization.identify import identify_stac_object_type
from pystac.utils import is_absolute_href, make_absolute_href
from pystac.utils import HREF, is_absolute_href, make_absolute_href, make_posix_style

ItemLike = Union[pystac.Item, Dict[str, Any]]

Expand Down Expand Up @@ -195,7 +195,7 @@ def from_dict(

@classmethod
def from_file(
cls: Type[C], href: str, stac_io: Optional[pystac.StacIO] = None
cls: Type[C], href: HREF, stac_io: Optional[pystac.StacIO] = None
) -> C:
"""Reads a :class:`ItemCollection` from a JSON file.

Expand All @@ -206,6 +206,7 @@ def from_file(
if stac_io is None:
stac_io = pystac.StacIO.default()

href = make_posix_style(href)
if not is_absolute_href(href):
href = make_absolute_href(href)

Expand Down
3 changes: 2 additions & 1 deletion pystac/stac_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pystac.html.jinja_env import get_jinja_env
from pystac.link import Link
from pystac.utils import (
HREF,
StringEnum,
is_absolute_href,
make_absolute_href,
Expand Down Expand Up @@ -603,7 +604,7 @@ def clone(self) -> STACObject:

@classmethod
def from_file(
cls: Type[S], href: str, stac_io: Optional[pystac.StacIO] = None
cls: Type[S], href: HREF, stac_io: Optional[pystac.StacIO] = None
) -> S:
"""Reads a STACObject implementation from a file.

Expand Down
2 changes: 1 addition & 1 deletion tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ def test_set_parent_false_stores_in_proper_place_on_save(
nested_catalog.normalize_and_save(
root_href=str(tmp_path), catalog_type=pystac.CatalogType.ABSOLUTE_PUBLISHED
)
root = pystac.Catalog.from_file(str(tmp_path / "catalog.json"))
root = pystac.Catalog.from_file(tmp_path / "catalog.json")
product_a = next(root.get_child("products").get_children()) # type: ignore
variable_a = next(root.get_child("variables").get_children()) # type: ignore

Expand Down
8 changes: 7 additions & 1 deletion tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def test_duplicate_self_links(tmp_path: Path, sample_item: pystac.Item) -> None:
assert len(sample_item.get_links(rel="self")) == 1
path = tmp_path / "item.json"
sample_item.save_object(include_self_link=True, dest_href=str(path))
sample_item = Item.from_file(str(path))
sample_item = Item.from_file(path)
assert len(sample_item.get_links(rel="self")) == 1


Expand Down Expand Up @@ -630,3 +630,9 @@ def test_non_hierarchical_relative_link() -> None:
assert a.target_in_hierarchy(b)
assert root.target_in_hierarchy(next(b.get_items()))
assert root.target_in_hierarchy(root)


def test_pathlib() -> None:
# This works, but breaks mypy until we fix
# https://github.com/stac-utils/pystac/issues/1216
Item.from_file(Path(TestCases.get_path("data-files/item/sample-item.json")))
2 changes: 1 addition & 1 deletion tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def test_relative_self_link(tmp_path: Path) -> None:
collection.save_object(
include_self_link=False, dest_href=str(tmp_path / "collection.json")
)
collection = Collection.from_file(str(tmp_path / "collection.json"))
collection = Collection.from_file(tmp_path / "collection.json")
read_item = collection.get_item("an-id")
assert read_item
asset_href = read_item.assets["data"].get_absolute_href()
Expand Down