Skip to content

Commit cd71138

Browse files
committed
Get link title from target, when possible
1 parent 6cbdd88 commit cd71138

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

pystac/link.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class Link:
5656
"""Optional description of the media type. Registered Media Types are preferred.
5757
See :class:`~pystac.MediaType` for common media types."""
5858

59-
title: Optional[str]
60-
"""Optional title for this link."""
61-
6259
extra_fields: Dict[str, Any]
6360
"""Optional, additional fields for this link. This is used by extensions as a
6461
way to serialize and deserialize properties on link object JSON."""
@@ -70,6 +67,7 @@ class Link:
7067

7168
_target_href: Optional[str]
7269
_target_object: Optional["STACObject_Type"]
70+
_title: Optional[str]
7371

7472
def __init__(
7573
self,
@@ -103,6 +101,22 @@ def set_owner(self, owner: Optional["STACObject_Type"]) -> "Link":
103101
self.owner = owner
104102
return self
105103

104+
@property
105+
def title(self) -> Optional[str]:
106+
"""Optional title for this link. If not provided during instantiation, this will
107+
attempt to get the title from the STAC object that the link references."""
108+
if self._title is not None:
109+
return self._title
110+
if self._target_object is not None and isinstance(
111+
self._target_object, pystac.Catalog
112+
):
113+
return self._target_object.title
114+
return None
115+
116+
@title.setter
117+
def title(self, v: Optional[str]) -> None:
118+
self._title = v
119+
106120
@property
107121
def href(self) -> str:
108122
"""Returns the HREF for this link.

tests/test_link.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ def test_relative_self_href(self) -> None:
130130
finally:
131131
os.chdir(previous)
132132

133+
def test_auto_title_when_resolved(self) -> None:
134+
extent = pystac.Extent.from_items([self.item])
135+
collection = pystac.Collection(
136+
id="my_collection",
137+
description="Test Collection",
138+
extent=extent,
139+
title="Collection Title",
140+
)
141+
link = pystac.Link("my rel", target=collection)
142+
143+
self.assertEqual(collection.title, link.title)
144+
145+
def test_auto_title_not_found(self) -> None:
146+
extent = pystac.Extent.from_items([self.item])
147+
collection = pystac.Collection(
148+
id="my_collection",
149+
description="Test Collection",
150+
extent=extent,
151+
)
152+
link = pystac.Link("my rel", target=collection)
153+
154+
self.assertEqual(None, link.title)
155+
133156

134157
class StaticLinkTest(unittest.TestCase):
135158
def setUp(self) -> None:

0 commit comments

Comments
 (0)