Skip to content

Commit 95aa480

Browse files
authored
fix: migrate by default (#1509)
* fix: migrate by default * chore: update changelog
1 parent 14a4626 commit 95aa480

15 files changed

+37
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- `migrate=True` is now the default in `from_dict` ([#1509](https://github.com/stac-utils/pystac/pull/1509))
8+
59
### Fixed
610

711
- Fall back to `epsg` when `code` is not present in the Projection extension ([#1505](https://github.com/stac-utils/pystac/pull/1505), [#1510](https://github.com/stac-utils/pystac/pull/1510))

pystac/catalog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ def from_dict(
12241224
d: dict[str, Any],
12251225
href: str | None = None,
12261226
root: Catalog | None = None,
1227-
migrate: bool = False,
1227+
migrate: bool = True,
12281228
preserve_dict: bool = True,
12291229
) -> C:
12301230
if migrate:

pystac/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def from_dict(
632632
d: dict[str, Any],
633633
href: str | None = None,
634634
root: Catalog | None = None,
635-
migrate: bool = False,
635+
migrate: bool = True,
636636
preserve_dict: bool = True,
637637
) -> C:
638638
from pystac.extensions.version import CollectionVersionExtension

pystac/item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def from_dict(
416416
d: dict[str, Any],
417417
href: str | None = None,
418418
root: Catalog | None = None,
419-
migrate: bool = False,
419+
migrate: bool = True,
420420
preserve_dict: bool = True,
421421
) -> T:
422422
from pystac.extensions.version import ItemVersionExtension

pystac/stac_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,19 @@ def stac_object_from_dict(
169169

170170
if info.object_type == pystac.STACObjectType.CATALOG:
171171
result = pystac.Catalog.from_dict(
172-
d, href=href_str, root=root, migrate=False, preserve_dict=preserve_dict
172+
d, href=href_str, root=root, migrate=True, preserve_dict=preserve_dict
173173
)
174174
result._stac_io = self
175175
return result
176176

177177
if info.object_type == pystac.STACObjectType.COLLECTION:
178178
return pystac.Collection.from_dict(
179-
d, href=href_str, root=root, migrate=False, preserve_dict=preserve_dict
179+
d, href=href_str, root=root, migrate=True, preserve_dict=preserve_dict
180180
)
181181

182182
if info.object_type == pystac.STACObjectType.ITEM:
183183
return pystac.Item.from_dict(
184-
d, href=href_str, root=root, migrate=False, preserve_dict=preserve_dict
184+
d, href=href_str, root=root, migrate=True, preserve_dict=preserve_dict
185185
)
186186

187187
raise ValueError(f"Unknown STAC object type {info.object_type}")

pystac/stac_object.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def from_dict(
647647
d: dict[str, Any],
648648
href: str | None = None,
649649
root: Catalog | None = None,
650-
migrate: bool = False,
650+
migrate: bool = True,
651651
preserve_dict: bool = True,
652652
) -> S:
653653
"""Parses this STACObject from the passed in dictionary.
@@ -659,8 +659,9 @@ def from_dict(
659659
root : Optional root catalog for this object.
660660
If provided, the root of the returned STACObject will be set
661661
to this parameter.
662-
migrate: Use True if this dict represents JSON from an older STAC object,
663-
so that migrations are run against it.
662+
migrate: By default, STAC objects and extensions are migrated to
663+
their latest supported version. Set this to False to disable
664+
this behavior.
664665
preserve_dict: If False, the dict parameter ``d`` may be modified
665666
during this method call. Otherwise the dict is not mutated.
666667
Defaults to True, which results results in a deepcopy of the

tests/extensions/test_classification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def _parse_times(a_dict: dict[str, Any]) -> None:
260260
a_dict[k] = a_dict[k].replace(microsecond=0)
261261

262262
d1 = deepcopy(item_dict)
263-
d2 = Item.from_dict(item_dict).to_dict()
263+
d2 = Item.from_dict(item_dict, migrate=False).to_dict()
264264
_parse_times(d1)
265265
_parse_times(d2)
266266
assert d1 == d2, f"Mismatch between dictionaries: \n{d1}\n{d2}"
@@ -343,7 +343,7 @@ def test_older_extension_version(landsat_item: Item) -> None:
343343
stac_extensions.add(old)
344344
item_as_dict = landsat_item.to_dict(include_self_link=False, transform_hrefs=False)
345345
item_as_dict["stac_extensions"] = list(stac_extensions)
346-
item = Item.from_dict(item_as_dict)
346+
item = Item.from_dict(item_as_dict, migrate=False)
347347
assert ClassificationExtension.has_extension(item)
348348
assert old in item.stac_extensions
349349

tests/extensions/test_eo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def test_older_extension_version(ext_item: Item) -> None:
439439
stac_extensions.add(old)
440440
item_as_dict = ext_item.to_dict(include_self_link=False, transform_hrefs=False)
441441
item_as_dict["stac_extensions"] = list(stac_extensions)
442-
item = Item.from_dict(item_as_dict)
442+
item = Item.from_dict(item_as_dict, migrate=False)
443443
assert EOExtension.has_extension(item)
444444
assert old in item.stac_extensions
445445

tests/extensions/test_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_older_extension_version(ext_item: pystac.Item) -> None:
160160
stac_extensions.add(old)
161161
item_as_dict = ext_item.to_dict(include_self_link=False, transform_hrefs=False)
162162
item_as_dict["stac_extensions"] = list(stac_extensions)
163-
item = pystac.Item.from_dict(item_as_dict)
163+
item = pystac.Item.from_dict(item_as_dict, migrate=False)
164164
assert GridExtension.has_extension(item)
165165
assert old in item.stac_extensions
166166

tests/extensions/test_projection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def test_older_extension_version(projection_landsat8_item: Item) -> None:
602602
include_self_link=False, transform_hrefs=False
603603
)
604604
item_as_dict["stac_extensions"] = list(stac_extensions)
605-
item = Item.from_dict(item_as_dict)
605+
item = Item.from_dict(item_as_dict, migrate=False)
606606
assert ProjectionExtension.has_extension(item)
607607
assert old in item.stac_extensions
608608

tests/extensions/test_raster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def test_older_extension_version(ext_item: pystac.Item) -> None:
305305
stac_extensions.add(old)
306306
item_as_dict = ext_item.to_dict(include_self_link=False, transform_hrefs=False)
307307
item_as_dict["stac_extensions"] = list(stac_extensions)
308-
item = pystac.Item.from_dict(item_as_dict)
308+
item = pystac.Item.from_dict(item_as_dict, migrate=False)
309309
assert RasterExtension.has_extension(item)
310310
assert old in item.stac_extensions
311311

tests/test_catalog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_from_dict_preserves_dict(self) -> None:
115115

116116
# assert that the parameter is not preserved with
117117
# non-default parameter
118-
_ = Catalog.from_dict(param_dict, preserve_dict=False)
118+
_ = Catalog.from_dict(param_dict, preserve_dict=False, migrate=False)
119119
assert param_dict != catalog_dict
120120

121121
def test_from_file_bad_catalog(self) -> None:
@@ -1595,7 +1595,7 @@ def from_dict(
15951595
d: dict[str, Any],
15961596
href: str | None = None,
15971597
root: Catalog | None = None,
1598-
migrate: bool = False,
1598+
migrate: bool = True,
15991599
preserve_dict: bool = True,
16001600
) -> CustomCatalog:
16011601
return super().from_dict(d)

tests/test_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_from_dict_preserves_dict(self) -> None:
306306

307307
# assert that the parameter is not preserved with
308308
# non-default parameter
309-
_ = Collection.from_dict(param_dict, preserve_dict=False)
309+
_ = Collection.from_dict(param_dict, preserve_dict=False, migrate=False)
310310
self.assertNotEqual(param_dict, collection_dict)
311311

312312
def test_from_dict_set_root(self) -> None:
@@ -594,7 +594,7 @@ def from_dict(
594594
d: dict[str, Any],
595595
href: str | None = None,
596596
root: Catalog | None = None,
597-
migrate: bool = False,
597+
migrate: bool = True,
598598
preserve_dict: bool = True,
599599
) -> CustomCollection:
600600
return super().from_dict(d)

tests/test_item.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def from_dict(
464464
d: dict[str, Any],
465465
href: str | None = None,
466466
root: Catalog | None = None,
467-
migrate: bool = False,
467+
migrate: bool = True,
468468
preserve_dict: bool = True,
469469
) -> CustomItem:
470470
return super().from_dict(d)
@@ -481,13 +481,13 @@ def test_item_from_dict_raises_useful_error() -> None:
481481
def test_item_from_dict_with_missing_stac_version_raises_useful_error() -> None:
482482
item_dict = {"type": "Feature", "id": "lalalalala"}
483483
with pytest.raises(pystac.STACTypeError, match="'stac_version' is missing"):
484-
Item.from_dict(item_dict)
484+
Item.from_dict(item_dict, migrate=False)
485485

486486

487487
def test_item_from_dict_with_missing_type_raises_useful_error() -> None:
488488
item_dict = {"stac_version": "0.8.0", "id": "lalalalala"}
489489
with pytest.raises(pystac.STACTypeError, match="'type' is missing"):
490-
Item.from_dict(item_dict)
490+
Item.from_dict(item_dict, migrate=False)
491491

492492

493493
@pytest.mark.parametrize("add_canonical", (True, False))
@@ -721,3 +721,12 @@ def test_copy_with_unresolveable_root(item: Item) -> None:
721721
def test_no_collection(item: Item) -> None:
722722
# https://github.com/stac-utils/stac-api-validator/issues/527
723723
assert item.collection is None
724+
725+
726+
def test_migrate_by_default() -> None:
727+
with open(
728+
TestCases.get_path("data-files/projection/example-with-version-1.1.json")
729+
) as f:
730+
data = json.load(f)
731+
item = pystac.Item.from_dict(data) # default used to be migrate=False
732+
assert item.ext.proj.code == "EPSG:32614"

tests/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _parse_times(a_dict: dict[str, Any]) -> None:
4242
a_dict[k] = a_dict[k].replace(microsecond=0)
4343

4444
d1 = deepcopy(d)
45-
d2 = stac_object_class.from_dict(d).to_dict()
45+
d2 = stac_object_class.from_dict(d, migrate=False).to_dict()
4646
_parse_times(d1)
4747
_parse_times(d2)
4848
assert d1 == d2

0 commit comments

Comments
 (0)