Skip to content

Commit e73c570

Browse files
author
Jon Duckworth
authored
Merge pull request #449 from duckontheweb/deprecate/gh-431-stac-io
More specific STAC_IO deprecation warnings
2 parents e7d4bdc + f832683 commit e73c570

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
### Changed
8383

8484
- API change: The extension API changed significantly. See ([#309](https://github.com/stac-utils/pystac/pull/309)) for more details.
85-
- API change: Refactored the global STAC_IO object to an instance-specific `StacIO` implementation. STAC_IO is deprecated and will be removed next release. ([#309](https://github.com/stac-utils/pystac/pull/309))
85+
- API change: Refactored the global STAC_IO object to an instance-specific `StacIO` implementation. ([#309](https://github.com/stac-utils/pystac/pull/309))
8686
- Asset.get_absolute_href returns None if no absolute href can be inferred (previously the relative href that was passed in was returned) ([#309](https://github.com/stac-utils/pystac/pull/309))
8787

8888
### Removed
@@ -91,6 +91,11 @@
9191
- Removed `LinkMixin`, and implemented those methods on `STACObject` directly. STACObject was the only class using LinkMixin and this should not effect users ([#309](https://github.com/stac-utils/pystac/pull/309)
9292
- Removed `single-file-stac` extension; this extension is being removed in favor of ItemCollection usage ([#309](https://github.com/stac-utils/pystac/pull/309)
9393

94+
# Deprecated
95+
96+
- Deprecated `STAC_IO` in favor of new `StacIO` class. `STAC_IO` will be removed in
97+
v1.0.0. ([#309](https://github.com/stac-utils/pystac/pull/309))
98+
9499
## [v0.5.6]
95100

96101
### Added

docs/api.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,18 @@ RelType
164164
IO
165165
--
166166

167+
StacIO
168+
~~~~~~
169+
170+
.. autoclass:: pystac.StacIO
171+
:members:
172+
:undoc-members:
173+
167174
STAC_IO
168175
~~~~~~~
169176

170-
STAC_IO is the utility mechanism that PySTAC uses for reading and writing. Users of
171-
PySTAC can hook into PySTAC by overriding members to utilize their own IO methods.
177+
.. deprecated:: 1.0.0-beta.1
178+
Use :class:`pystac.StacIO` instead. This class will be removed in v1.0.0.
172179

173180
.. autoclass:: pystac.stac_io.STAC_IO
174181
:members:

pystac/stac_io.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,28 @@ class STAC_IO:
271271
"""
272272

273273
@staticmethod
274-
def read_text_method(uri: str) -> str:
274+
def issue_deprecation_warning() -> None:
275275
warnings.warn(
276-
"STAC_IO is deprecated. "
277-
"Please use instances of StacIO (e.g. StacIO.default()).",
276+
"STAC_IO is deprecated and will be removed in v1.0.0. "
277+
"Please use instances of StacIO (e.g. StacIO.default()) instead.",
278278
DeprecationWarning,
279279
)
280+
281+
def __init__(self) -> None:
282+
STAC_IO.issue_deprecation_warning()
283+
284+
def __init_subclass__(cls) -> None:
285+
STAC_IO.issue_deprecation_warning()
286+
287+
@staticmethod
288+
def read_text_method(uri: str) -> str:
289+
STAC_IO.issue_deprecation_warning()
280290
return StacIO.default().read_text(uri)
281291

282292
@staticmethod
283293
def write_text_method(uri: str, txt: str) -> None:
284294
"""Default method for writing text."""
285-
warnings.warn(
286-
"STAC_IO is deprecated. "
287-
"Please use instances of StacIO (e.g. StacIO.default()).",
288-
DeprecationWarning,
289-
)
295+
STAC_IO.issue_deprecation_warning()
290296
return StacIO.default().write_text(uri, txt)
291297

292298
@staticmethod
@@ -295,11 +301,7 @@ def stac_object_from_dict(
295301
href: Optional[str] = None,
296302
root: Optional["Catalog_Type"] = None,
297303
) -> "STACObject_Type":
298-
warnings.warn(
299-
"STAC_IO is deprecated. "
300-
"Please use instances of StacIO (e.g. StacIO.default()).",
301-
DeprecationWarning,
302-
)
304+
STAC_IO.issue_deprecation_warning()
303305
return pystac.serialization.stac_object_from_dict(d, href, root)
304306

305307
# This is set in __init__.py
@@ -356,6 +358,7 @@ def read_json(cls, uri: str) -> Dict[str, Any]:
356358
STAC_IO in order to enable additional URI types, replace that member
357359
with your own implementation.
358360
"""
361+
STAC_IO.issue_deprecation_warning()
359362
result: Dict[str, Any] = json.loads(STAC_IO.read_text(uri))
360363
return result
361364

tests/test_stac_io.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ def test_stac_io_issues_warnings(self) -> None:
2222
self.assertEqual(len(w), 1)
2323
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
2424

25+
with warnings.catch_warnings(record=True) as w:
26+
# Cause all warnings to always be triggered.
27+
warnings.simplefilter("always")
28+
# Trigger instantiation warning.
29+
_ = STAC_IO()
30+
31+
self.assertEqual(len(w), 1)
32+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
33+
34+
with warnings.catch_warnings(record=True) as w:
35+
# Cause all warnings to always be triggered.
36+
warnings.simplefilter("always")
37+
38+
class CustomSTAC_IO(STAC_IO):
39+
pass
40+
41+
self.assertEqual(len(w), 1)
42+
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
43+
44+
with warnings.catch_warnings(record=True) as w:
45+
# Cause all warnings to always be triggered.
46+
warnings.simplefilter("always")
47+
48+
d = STAC_IO.read_json(
49+
TestCases.get_path("data-files/item/sample-item.json")
50+
)
51+
_ = STAC_IO.stac_object_from_dict(d)
52+
53+
self.assertEqual(len(w), 3)
54+
self.assertTrue(
55+
all(issubclass(wrn.category, DeprecationWarning) for wrn in w)
56+
)
57+
2558
def test_read_write_collection(self) -> None:
2659
collection = pystac.read_file(
2760
TestCases.get_path("data-files/collections/multi-extent.json")

0 commit comments

Comments
 (0)