Skip to content

Commit 4022849

Browse files
committed
fix: item tests
1 parent f40a8df commit 4022849

5 files changed

Lines changed: 41 additions & 40 deletions

File tree

src/pystac/catalog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ def __init__(
4242
def catalog_type(self) -> CatalogType | None: # pyright: ignore[reportDeprecated]
4343
return CatalogType.determine_type(self.to_dict()) # pyright: ignore[reportDeprecated]
4444

45+
@catalog_type.setter
46+
@deprecated("catalog_type is deprecated, and setting it has no effect")
47+
def catalog_type(self, _: CatalogType) -> None: # pyright: ignore[reportDeprecated]
48+
pass
49+
4550
@override
4651
@classmethod
4752
def from_dict(

src/pystac/item.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,6 @@ def __getitem__(self, key: str) -> Any:
235235
def __setitem__(self, name: str, value: Any, /) -> None:
236236
self.extra_fields[name] = value
237237

238-
def __getattr__(self, key: str) -> Any:
239-
return self.extra_fields[key]
240-
241238
def __contains__(self, key: str) -> bool:
242239
return key == "datetime" or key in self.extra_fields
243240

src/pystac/layout.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
from __future__ import annotations
22

3-
import warnings
4-
5-
warnings.warn(
6-
message="pystac.layout is deprecated",
7-
category=DeprecationWarning,
8-
)
9-
103
import posixpath
114
from abc import ABC, abstractmethod
125
from collections import OrderedDict
136
from collections.abc import Callable
147
from typing import TYPE_CHECKING, Any
158

16-
import pystac
17-
from pystac.utils import is_file_path
9+
from .errors import STACError
10+
from .utils import is_file_path
1811

1912
if TYPE_CHECKING:
20-
from pystac.catalog import Catalog
21-
from pystac.collection import Collection
22-
from pystac.item import Item
23-
from pystac.stac_object import STACObject
13+
from .catalog import Catalog
14+
from .collection import Collection
15+
from .item import Item
16+
from .stac_object import STACObject
2417

2518

2619
class TemplateError(Exception):
@@ -132,15 +125,16 @@ def __init__(self, template: str, defaults: dict[str, str] | None = None) -> Non
132125

133126
def _get_template_value(self, stac_object: STACObject, template_var: str) -> Any:
134127
if template_var in self.ITEM_TEMPLATE_VARS:
135-
if isinstance(stac_object, pystac.Item):
128+
if isinstance(stac_object, Item):
136129
# Datetime
137130
dt = stac_object.datetime
138131
if dt is None:
139132
dt = stac_object.common_metadata.start_datetime
140133
if dt is None:
141-
raise pystac.TemplateError(
134+
raise TemplateError(
142135
f"Item {stac_object} does not have a datetime or "
143-
f"datetime range set; cannot template {template_var} in {self.template}"
136+
f"datetime range set; cannot template {template_var} "
137+
f"in {self.template}"
144138
)
145139

146140
if template_var == "year":
@@ -156,20 +150,22 @@ def _get_template_value(self, stac_object: STACObject, template_var: str) -> Any
156150
if template_var == "collection":
157151
if stac_object.collection_id is not None:
158152
return stac_object.collection_id
159-
raise pystac.TemplateError(
153+
raise TemplateError(
160154
f"Item {stac_object} does not have a collection ID set; "
161155
f"cannot template {template_var} in {self.template}"
162156
)
163157
else:
164-
raise pystac.TemplateError(
165-
f'"{template_var}" cannot be used to template non-Item {stac_object} in {self.template}'
158+
raise TemplateError(
159+
f'"{template_var}" cannot be used to template non-Item "'
160+
f"{stac_object} in {self.template}"
166161
)
167162

168163
# Allow dot-notation properties for arbitrary object values.
169164
props = template_var.split(".")
170-
prop_source: pystac.STACObject | dict[str, Any] | None = None
171-
error = pystac.TemplateError(
172-
f"Cannot find property {template_var} on {stac_object} for template {self.template}"
165+
prop_source: STACObject | dict[str, Any] | None = None
166+
error = TemplateError(
167+
f"Cannot find property {template_var} on {stac_object} for template "
168+
f"{self.template}"
173169
)
174170

175171
try:
@@ -199,7 +195,7 @@ def _get_template_value(self, stac_object: STACObject, template_var: str) -> Any
199195
if not hasattr(v, prop):
200196
raise error
201197
v = getattr(v, prop)
202-
except pystac.TemplateError as e:
198+
except TemplateError as e:
203199
if template_var in self.defaults:
204200
return self.defaults[template_var]
205201
raise e
@@ -222,7 +218,7 @@ def get_template_values(self, stac_object: STACObject) -> dict[str, Any]:
222218
stac object.
223219
224220
Raises:
225-
pystac.TemplateError: If a value for a template variable cannot be
221+
TemplateError: If a value for a template variable cannot be
226222
derived from the stac object and there is no default,
227223
this error will be raised.
228224
"""
@@ -268,14 +264,14 @@ def get_href(
268264
if is_file_path(parent_dir):
269265
parent_dir = os.path.dirname(parent_dir)
270266

271-
if isinstance(stac_object, pystac.Item):
267+
if isinstance(stac_object, Item):
272268
return self.get_item_href(stac_object, parent_dir)
273-
elif isinstance(stac_object, pystac.Collection):
269+
elif isinstance(stac_object, Collection):
274270
return self.get_collection_href(stac_object, parent_dir, is_root)
275-
elif isinstance(stac_object, pystac.Catalog):
271+
elif isinstance(stac_object, Catalog):
276272
return self.get_catalog_href(stac_object, parent_dir, is_root)
277273
else:
278-
raise pystac.STACError(f"Unknown STAC object type {stac_object}")
274+
raise STACError(f"Unknown STAC object type {stac_object}")
279275

280276
@abstractmethod
281277
def get_catalog_href(self, cat: Catalog, parent_dir: str, is_root: bool) -> str:

src/pystac/stac_object.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ def __init__(
7171

7272
@override
7373
def __setattr__(self, name: str, value: Any, /) -> None:
74-
if name == "_stac_io":
75-
warnings.warn("_stac_io is a deprecated attribute. Set .reader or .writer")
74+
match name:
75+
case "_stac_io":
76+
warnings.warn(
77+
"_stac_io is a deprecated attribute. Set .reader or .writer"
78+
)
7679

77-
from .stac_io import StacIOReader, StacIOWriter
80+
from .stac_io import StacIOReader, StacIOWriter
7881

79-
self.reader = StacIOReader(value)
80-
self.writer = StacIOWriter(value)
81-
return super().__setattr__(name, value)
82+
self.reader = StacIOReader(value)
83+
self.writer = StacIOWriter(value)
84+
case _:
85+
return super().__setattr__(name, value)
8286

8387
@classmethod
8488
def from_file[T: STACObject](

tests/v1/test_item.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def test_resolve_collection_with_root(
598598
assert root.id == "root"
599599

600600

601-
@pytest.mark.vcr()
601+
@pytest.mark.xfail(reason="Setting the catalog type doesn't change behavior")
602602
def test_non_hierarchical_relative_link() -> None:
603603
root = pystac.Catalog("root", "root")
604604
a = pystac.Catalog("a", "a")
@@ -616,8 +616,7 @@ def test_non_hierarchical_relative_link() -> None:
616616
related_href = [link for link in a.links if link.rel == "related"][0].get_href()
617617

618618
assert related_href is not None and not is_absolute_href(related_href)
619-
# @gadomski thinks this is a bad assertion, and has removed it for v2
620-
# assert a.target_in_hierarchy(b)
619+
assert a.target_in_hierarchy(b)
621620
assert root.target_in_hierarchy(next(b.get_items()))
622621
assert root.target_in_hierarchy(root)
623622

0 commit comments

Comments
 (0)