Skip to content

Commit 01c2795

Browse files
committed
feat: add assets mixin
1 parent bc290a5 commit 01c2795

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ classifiers = [
2323
"Programming Language :: Python :: 3.13",
2424
]
2525
requires-python = ">=3.10"
26-
dependencies = [
27-
"python-dateutil>=2.9.0.post0",
28-
"typing-extensions>=4.12.2",
29-
]
26+
dependencies = ["python-dateutil>=2.9.0.post0", "typing-extensions>=4.12.2"]
3027

3128
[project.optional-dependencies]
3229
validate = ["jsonschema>=4.23.0", "referencing>=0.36.2"]
@@ -38,6 +35,7 @@ dev = [
3835
"pytest>=8.3.4",
3936
"ruff>=0.9.6",
4037
"types-jsonschema>=4.23.0.20241208",
38+
"types-python-dateutil>=2.9.0.20241206",
4139
]
4240
bench = ["asv>=0.6.4"]
4341
docs = ["mike>=2.1.3", "mkdocs-material>=9.6.3", "mkdocstrings-python>=1.14.6"]

src/pystac/asset.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,12 @@ def to_dict(self) -> dict[str, Any]:
7575
d = {"href": self.href}
7676
d.update(super().to_dict())
7777
return d
78+
79+
80+
class AssetsMixin:
81+
"""A mixin for things that have assets (Collections and Items)"""
82+
83+
assets: dict[str, Asset]
84+
85+
def add_asset(self, key: str, asset: Asset) -> None:
86+
raise NotImplementedError

src/pystac/item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import warnings
66
from typing import Any, Sequence
77

8-
from .asset import Asset
8+
from .asset import Asset, AssetsMixin
99
from .constants import ITEM_TYPE
1010
from .errors import StacWarning
1111
from .link import Link
1212
from .stac_object import STACObject
1313

1414

15-
class Item(STACObject):
15+
class Item(STACObject, AssetsMixin):
1616
"""An Item is a GeoJSON Feature augmented with foreign members relevant to a
1717
STAC object.
1818

src/pystac/stac_object.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def from_file(
7878

7979
@classmethod
8080
def from_dict(
81-
cls: type[STACObject],
81+
cls: type[Self],
8282
d: dict[str, Any],
8383
*,
8484
href: str | None = None,
@@ -87,7 +87,7 @@ def from_dict(
8787
preserve_dict: bool = True, # TODO deprecation warning
8888
reader: Read | None = None,
8989
writer: Write | None = None,
90-
) -> STACObject:
90+
) -> Self:
9191
"""Creates a STAC object from a dictionary.
9292
9393
If you already know what type of STAC object your dictionary represents,
@@ -112,17 +112,24 @@ def from_dict(
112112
if type_value == CATALOG_TYPE:
113113
from .catalog import Catalog
114114

115-
return Catalog(**d, href=href, reader=reader, writer=writer)
115+
stac_object: STACObject = Catalog(
116+
**d, href=href, reader=reader, writer=writer
117+
)
116118
elif type_value == COLLECTION_TYPE:
117119
from .collection import Collection
118120

119-
return Collection(**d, href=href, reader=reader, writer=writer)
121+
stac_object = Collection(**d, href=href, reader=reader, writer=writer)
120122
elif type_value == ITEM_TYPE:
121123
from .item import Item
122124

123-
return Item(**d, href=href, reader=reader, writer=writer)
125+
stac_object = Item(**d, href=href, reader=reader, writer=writer)
124126
else:
125127
raise StacError(f"unknown type field: {type_value}")
128+
129+
if isinstance(stac_object, cls):
130+
return stac_object
131+
else:
132+
raise PystacError(f"Expected {cls} but got a {type(stac_object)}")
126133
else:
127134
raise StacError("missing type field on dictionary")
128135

@@ -140,6 +147,8 @@ def __init__(
140147
"""Creates a new STAC object."""
141148
from .extensions import Extensions
142149

150+
super().__init__()
151+
143152
self.id: str = id
144153
"""The object's id."""
145154

tests/v1/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def all_test_catalogs() -> list[Catalog]:
164164
TestCases.case_4(),
165165
TestCases.case_5(),
166166
TestCases.case_7(),
167-
TestCases.case_8(),
167+
TestCases.case_8(), # type: ignore
168168
]
169169

170170
@staticmethod

uv.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)