Skip to content

Commit 39178b1

Browse files
committed
Return calling class from Link constructor methods
1 parent a4fe4cd commit 39178b1

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

pystac/link.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ def clone(self) -> "Link":
277277
title=self.title,
278278
)
279279

280-
@staticmethod
281-
def from_dict(d: Dict[str, Any]) -> "Link":
280+
@classmethod
281+
def from_dict(cls, d: Dict[str, Any]) -> "Link":
282282
"""Deserializes a Link from a dict.
283283
284284
Args:
@@ -297,55 +297,56 @@ def from_dict(d: Dict[str, Any]) -> "Link":
297297
if any(d):
298298
properties = d
299299

300-
return Link(
300+
return cls(
301301
rel=rel,
302302
target=href,
303303
media_type=media_type,
304304
title=title,
305305
properties=properties,
306306
)
307307

308-
@staticmethod
309-
def root(c: "Catalog_Type") -> "Link":
308+
@classmethod
309+
def root(cls, c: "Catalog_Type") -> "Link":
310310
"""Creates a link to a root Catalog or Collection."""
311-
return Link(pystac.RelType.ROOT, c, media_type=pystac.MediaType.JSON)
311+
return cls(pystac.RelType.ROOT, c, media_type=pystac.MediaType.JSON)
312312

313-
@staticmethod
314-
def parent(c: "Catalog_Type") -> "Link":
313+
@classmethod
314+
def parent(cls, c: "Catalog_Type") -> "Link":
315315
"""Creates a link to a parent Catalog or Collection."""
316-
return Link(pystac.RelType.PARENT, c, media_type=pystac.MediaType.JSON)
316+
return cls(pystac.RelType.PARENT, c, media_type=pystac.MediaType.JSON)
317317

318-
@staticmethod
319-
def collection(c: "Collection_Type") -> "Link":
318+
@classmethod
319+
def collection(cls, c: "Collection_Type") -> "Link":
320320
"""Creates a link to an item's Collection."""
321-
return Link(pystac.RelType.COLLECTION, c, media_type=pystac.MediaType.JSON)
321+
return cls(pystac.RelType.COLLECTION, c, media_type=pystac.MediaType.JSON)
322322

323-
@staticmethod
324-
def self_href(href: str) -> "Link":
323+
@classmethod
324+
def self_href(cls, href: str) -> "Link":
325325
"""Creates a self link to a file's location."""
326-
return Link(pystac.RelType.SELF, href, media_type=pystac.MediaType.JSON)
326+
return cls(pystac.RelType.SELF, href, media_type=pystac.MediaType.JSON)
327327

328-
@staticmethod
329-
def child(c: "Catalog_Type", title: Optional[str] = None) -> "Link":
328+
@classmethod
329+
def child(cls, c: "Catalog_Type", title: Optional[str] = None) -> "Link":
330330
"""Creates a link to a child Catalog or Collection."""
331-
return Link(
331+
return cls(
332332
pystac.RelType.CHILD, c, title=title, media_type=pystac.MediaType.JSON
333333
)
334334

335-
@staticmethod
336-
def item(item: "Item_Type", title: Optional[str] = None) -> "Link":
335+
@classmethod
336+
def item(cls, item: "Item_Type", title: Optional[str] = None) -> "Link":
337337
"""Creates a link to an Item."""
338-
return Link(
338+
return cls(
339339
pystac.RelType.ITEM, item, title=title, media_type=pystac.MediaType.JSON
340340
)
341341

342-
@staticmethod
342+
@classmethod
343343
def canonical(
344+
cls,
344345
item_or_collection: Union["Item_Type", "Collection_Type"],
345346
title: Optional[str] = None,
346347
) -> "Link":
347348
"""Creates a canonical link to an Item or Collection."""
348-
return Link(
349+
return cls(
349350
pystac.RelType.CANONICAL,
350351
item_or_collection,
351352
title=title,

tests/test_link.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,43 @@ def test_canonical_collection(self) -> None:
135135
link = pystac.Link.canonical(self.collection)
136136
expected = {"rel": "canonical", "href": None, "type": "application/json"}
137137
self.assertEqual(expected, link.to_dict())
138+
139+
140+
class LinkInheritanceTest(unittest.TestCase):
141+
def setUp(self) -> None:
142+
self.maxDiff = None
143+
self.collection = pystac.Collection(
144+
"collection id", "desc", extent=ARBITRARY_EXTENT
145+
)
146+
self.item = pystac.Item(
147+
id="test-item",
148+
geometry=None,
149+
bbox=None,
150+
datetime=TEST_DATETIME,
151+
properties={},
152+
)
153+
154+
class CustomLink(pystac.Link):
155+
pass
156+
157+
def test_from_dict(self) -> None:
158+
link = self.CustomLink.from_dict(
159+
{"rel": "r", "href": "t", "type": "a/b", "title": "t", "c": "d", "1": 2}
160+
)
161+
self.assertIsInstance(link, self.CustomLink)
162+
163+
def test_collection(self) -> None:
164+
link = self.CustomLink.collection(self.collection)
165+
self.assertIsInstance(link, self.CustomLink)
166+
167+
def test_child(self) -> None:
168+
link = self.CustomLink.child(self.collection)
169+
self.assertIsInstance(link, self.CustomLink)
170+
171+
def test_canonical_item(self) -> None:
172+
link = self.CustomLink.canonical(self.item)
173+
self.assertIsInstance(link, self.CustomLink)
174+
175+
def test_canonical_collection(self) -> None:
176+
link = self.CustomLink.canonical(self.collection)
177+
self.assertIsInstance(link, self.CustomLink)

0 commit comments

Comments
 (0)