Skip to content

Commit

Permalink
fix: allow HREF when opening files
Browse files Browse the repository at this point in the history
Functionally this worked, but the typing needed a fix.
  • Loading branch information
gadomski committed Sep 27, 2023
1 parent 9d6b146 commit 3f91056
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
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
6 changes: 6 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
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")))

0 comments on commit 3f91056

Please sign in to comment.