Skip to content

Commit ec67d34

Browse files
committed
Add type filter to STACObject.get_stac_objects
1 parent a0f79a3 commit ec67d34

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pystac/stac_object.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from enum import Enum
3-
from typing import Any, Dict, Iterable, List, Optional, cast, TYPE_CHECKING, Union
3+
from typing import Any, Dict, Iterable, List, Optional, Type, cast, TYPE_CHECKING, Union
44

55
import pystac
66
from pystac import STACError
@@ -272,25 +272,29 @@ def set_parent(self, parent: Optional["Catalog_Type"]) -> None:
272272
self.add_link(Link.parent(parent))
273273

274274
def get_stac_objects(
275-
self, rel: Union[str, pystac.RelType]
275+
self, rel: Union[str, pystac.RelType], typ: Optional[Type["STACObject"]] = None
276276
) -> Iterable["STACObject"]:
277277
"""Gets the :class:`~pystac.STACObject` instances that are linked to
278278
by links with their ``rel`` property matching the passed in argument.
279279
280280
Args:
281281
rel : The relation to match each :class:`~pystac.Link`'s
282282
``rel`` property against.
283+
typ : If not ``None``, objects will only be yielded if they are instances of
284+
``typ``.
283285
284286
Returns:
285287
Iterable[STACObjects]: A possibly empty iterable of STACObjects that are
286-
connected to this object through links with the given ``rel``.
288+
connected to this object through links with the given ``rel`` and are of
289+
type ``typ`` (if given).
287290
"""
288291
links = self.links[:]
289292
for i in range(0, len(links)):
290293
link = links[i]
291294
if link.rel == rel:
292295
link.resolve_stac_object(root=self.get_root())
293-
yield cast("STACObject", link.target)
296+
if typ is None or isinstance(link.target, typ):
297+
yield cast("STACObject", link.target)
294298

295299
def save_object(
296300
self,

0 commit comments

Comments
 (0)