Skip to content

Commit 8486774

Browse files
committed
Make FileExtension a concrete class
1 parent 60ec62b commit 8486774

File tree

4 files changed

+19
-60
lines changed

4 files changed

+19
-60
lines changed

docs/api.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,6 @@ FileExtension
310310
:show-inheritance:
311311
:undoc-members:
312312

313-
.. autoclass:: pystac.extensions.file.AssetFileExtension
314-
:members:
315-
:show-inheritance:
316-
:undoc-members:
317-
318-
319313
Label Extension
320314
---------------
321315

pystac/extensions/file.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
from enum import Enum
7-
from typing import Any, Dict, Generic, List, Optional, Set, TypeVar, cast
7+
from typing import Any, Dict, List, Optional, Set
88

99
import pystac
1010
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension
@@ -16,8 +16,6 @@
1616
)
1717
from pystac.utils import get_required
1818

19-
T = TypeVar("T", bound=pystac.Asset)
20-
2119
SCHEMA_URI = "https://stac-extensions.github.io/file/v2.0.0/schema.json"
2220

2321
PREFIX = "file:"
@@ -90,15 +88,11 @@ def summary(self, v: str) -> None:
9088
self.properties["summary"] = v
9189

9290

93-
class FileExtension(
94-
Generic[T], PropertiesExtension, ExtensionManagementMixin[pystac.Item]
95-
):
96-
"""An abstract class that can be used to extend the properties of an
97-
:class:`~pystac.Item` or :class:`~pystac.Asset` with properties from the
98-
:stac-ext:`File Info Extension <file>`. This class is generic over the type of
99-
STAC Object to be extended (e.g. :class:`~pystac.Asset`).
91+
class FileExtension(PropertiesExtension, ExtensionManagementMixin[pystac.Item]):
92+
"""A class that can be used to extend the properties of an :class:`~pystac.Asset`
93+
with properties from the :stac-ext:`File Info Extension <file>`.
10094
101-
To create a concrete instance of :class:`FileExtension`, use the
95+
To create an instance of :class:`FileExtension`, use the
10296
:meth:`FileExtension.ext` method. For example:
10397
10498
.. code-block:: python
@@ -107,6 +101,15 @@ class FileExtension(
107101
>>> file_ext = FileExtension.ext(asset)
108102
"""
109103

104+
def __init__(self, asset: pystac.Asset):
105+
self.asset_href = asset.href
106+
self.properties = asset.properties
107+
if asset.owner and isinstance(asset.owner, pystac.Item):
108+
self.additional_read_properties = [asset.owner.properties]
109+
110+
def __repr__(self) -> str:
111+
return "<AssetFileExtension Asset href={}>".format(self.asset_href)
112+
110113
def apply(
111114
self,
112115
byte_order: Optional[ByteOrder] = None,
@@ -189,42 +192,14 @@ def values(self, v: Optional[List[MappingObject]]) -> None:
189192
def get_schema_uri(cls) -> str:
190193
return SCHEMA_URI
191194

192-
@staticmethod
193-
def ext(obj: T) -> "FileExtension[T]":
195+
@classmethod
196+
def ext(cls, obj: pystac.Asset) -> "FileExtension":
194197
"""Extends the given STAC Object with properties from the :stac-ext:`File Info
195198
Extension <file>`.
196199
197200
This extension can be applied to instances of :class:`~pystac.Asset`.
198-
199-
Raises:
200-
201-
pystac.ExtensionTypeError : If an invalid object type is passed.
202201
"""
203-
if isinstance(obj, pystac.Asset):
204-
return cast(FileExtension[T], AssetFileExtension(obj))
205-
else:
206-
raise pystac.ExtensionTypeError(
207-
f"File extension does not apply to type {type(obj)}"
208-
)
209-
210-
211-
class AssetFileExtension(FileExtension[pystac.Asset]):
212-
"""A concrete implementation of :class:`FileExtension` on an :class:`~pystac.Asset`
213-
that extends the Asset fields to include properties defined in the
214-
:stac-ext:`File Info Extension <info>`.
215-
216-
This class should generally not be instantiated directly. Instead, call
217-
:meth:`FileExtension.ext` on an :class:`~pystac.Asset` to extend it.
218-
"""
219-
220-
def __init__(self, asset: pystac.Asset):
221-
self.asset_href = asset.href
222-
self.properties = asset.properties
223-
if asset.owner and isinstance(asset.owner, pystac.Item):
224-
self.additional_read_properties = [asset.owner.properties]
225-
226-
def __repr__(self) -> str:
227-
return "<AssetFileExtension Asset href={}>".format(self.asset_href)
202+
return cls(obj)
228203

229204

230205
class FileExtensionHooks(ExtensionHooks):

pystac/extensions/label.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ def to_dict(self) -> Dict[str, Any]:
381381

382382

383383
class LabelExtension(ExtensionManagementMixin[pystac.Item]):
384-
"""An abstract class that can be used to extend the properties of an
384+
"""A class that can be used to extend the properties of an
385385
:class:`~pystac.Item` with properties from the :stac-ext:`Label Extension <label>`.
386386
387-
To create a concrete instance of :class:`LabeExtension`, use the
387+
To create an instance of :class:`LabeExtension`, use the
388388
:meth:`LabelExtension.ext` method. For example:
389389
390390
.. code-block:: python
@@ -644,10 +644,6 @@ def ext(cls, obj: pystac.Item) -> "LabelExtension":
644644
Extension <label>`.
645645
646646
This extension can be applied to instances of :class:`~pystac.Item`.
647-
648-
Raises:
649-
650-
pystac.ExtensionTypeError : If an invalid object type is passed.
651647
"""
652648
return cls(obj)
653649

tests/extensions/test_file.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ def test_repr(self) -> None:
176176
file_ext.__repr__(), f"<AssetFileExtension Asset href={asset.href}>"
177177
)
178178

179-
def test_extend_invalid_object(self) -> None:
180-
item = pystac.Item.from_file(self.FILE_ITEM_EXAMPLE_URI)
181-
182-
with self.assertRaises(pystac.ExtensionTypeError):
183-
_ = FileExtension.ext(item)
184-
185179
def test_migrates_old_checksum(self) -> None:
186180
example_path = TestCases.get_path(
187181
"data-files/examples/1.0.0-beta.2/"

0 commit comments

Comments
 (0)