Skip to content

Commit 9c323c4

Browse files
authored
Allow HREF when opening files (#1234)
* fix: allow HREF when opening files Functionally this worked, but the typing needed a fix. * fix: don't coerce to str in tests
1 parent 9d6b146 commit 9c323c4

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

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

9+
### Fixed
10+
11+
- Typing of `href` arguments ([#1234](https://github.com/stac-utils/pystac/pull/1234))
12+
913
## [v1.8.4] - 2023-09-22
1014

1115
### Added

pystac/catalog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from pystac.stac_object import STACObject, STACObjectType
3838
from pystac.utils import (
39+
HREF,
3940
StringEnum,
4041
is_absolute_href,
4142
make_absolute_href,
@@ -1245,7 +1246,7 @@ def full_copy(
12451246

12461247
@classmethod
12471248
def from_file(
1248-
cls: Type[C], href: str, stac_io: Optional[pystac.StacIO] = None
1249+
cls: Type[C], href: HREF, stac_io: Optional[pystac.StacIO] = None
12491250
) -> C:
12501251
if stac_io is None:
12511252
stac_io = pystac.StacIO.default()

pystac/item_collection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pystac.errors import STACTypeError
2020
from pystac.html.jinja_env import get_jinja_env
2121
from pystac.serialization.identify import identify_stac_object_type
22-
from pystac.utils import is_absolute_href, make_absolute_href
22+
from pystac.utils import HREF, is_absolute_href, make_absolute_href, make_posix_style
2323

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

@@ -195,7 +195,7 @@ def from_dict(
195195

196196
@classmethod
197197
def from_file(
198-
cls: Type[C], href: str, stac_io: Optional[pystac.StacIO] = None
198+
cls: Type[C], href: HREF, stac_io: Optional[pystac.StacIO] = None
199199
) -> C:
200200
"""Reads a :class:`ItemCollection` from a JSON file.
201201
@@ -206,6 +206,7 @@ def from_file(
206206
if stac_io is None:
207207
stac_io = pystac.StacIO.default()
208208

209+
href = make_posix_style(href)
209210
if not is_absolute_href(href):
210211
href = make_absolute_href(href)
211212

pystac/stac_object.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pystac.html.jinja_env import get_jinja_env
2323
from pystac.link import Link
2424
from pystac.utils import (
25+
HREF,
2526
StringEnum,
2627
is_absolute_href,
2728
make_absolute_href,
@@ -603,7 +604,7 @@ def clone(self) -> STACObject:
603604

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

tests/test_catalog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ def test_set_parent_false_stores_in_proper_place_on_save(
17201720
nested_catalog.normalize_and_save(
17211721
root_href=str(tmp_path), catalog_type=pystac.CatalogType.ABSOLUTE_PUBLISHED
17221722
)
1723-
root = pystac.Catalog.from_file(str(tmp_path / "catalog.json"))
1723+
root = pystac.Catalog.from_file(tmp_path / "catalog.json")
17241724
product_a = next(root.get_child("products").get_children()) # type: ignore
17251725
variable_a = next(root.get_child("variables").get_children()) # type: ignore
17261726

tests/test_item.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def test_duplicate_self_links(tmp_path: Path, sample_item: pystac.Item) -> None:
488488
assert len(sample_item.get_links(rel="self")) == 1
489489
path = tmp_path / "item.json"
490490
sample_item.save_object(include_self_link=True, dest_href=str(path))
491-
sample_item = Item.from_file(str(path))
491+
sample_item = Item.from_file(path)
492492
assert len(sample_item.get_links(rel="self")) == 1
493493

494494

@@ -630,3 +630,9 @@ def test_non_hierarchical_relative_link() -> None:
630630
assert a.target_in_hierarchy(b)
631631
assert root.target_in_hierarchy(next(b.get_items()))
632632
assert root.target_in_hierarchy(root)
633+
634+
635+
def test_pathlib() -> None:
636+
# This works, but breaks mypy until we fix
637+
# https://github.com/stac-utils/pystac/issues/1216
638+
Item.from_file(Path(TestCases.get_path("data-files/item/sample-item.json")))

tests/test_link.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_relative_self_link(tmp_path: Path) -> None:
328328
collection.save_object(
329329
include_self_link=False, dest_href=str(tmp_path / "collection.json")
330330
)
331-
collection = Collection.from_file(str(tmp_path / "collection.json"))
331+
collection = Collection.from_file(tmp_path / "collection.json")
332332
read_item = collection.get_item("an-id")
333333
assert read_item
334334
asset_href = read_item.assets["data"].get_absolute_href()

0 commit comments

Comments
 (0)