Skip to content

Commit c815552

Browse files
author
Jon Duckworth
authored
Fix string enum serialization (#654)
* Add failing test for RelType serialization * Fix serialization of string Enum classes * Add CHANGELOG entry for 654 * Use assertEqual instead of assert + == * Use StringEnum in remaining classes inheriting from str, Enum
1 parent 2334c7c commit c815552

17 files changed

+65
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- `generate_subcatalogs` can include multiple template values in a single subfolder layer
2727
([#595](https://github.com/stac-utils/pystac/pull/595))
2828
- Avoid implicit re-exports ([#591](https://github.com/stac-utils/pystac/pull/591))
29+
- Regression where string `Enum` values were not serialized properly in methods like `Link.to_dict` ([#654](https://github.com/stac-utils/pystac/pull/654))
2930

3031
### Deprecated
3132

pystac/catalog.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from copy import deepcopy
3-
from enum import Enum
43
from pystac.errors import STACTypeError
54
from typing import (
65
Any,
@@ -29,15 +28,20 @@
2928
identify_stac_object,
3029
migrate_to_latest,
3130
)
32-
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href
31+
from pystac.utils import (
32+
StringEnum,
33+
is_absolute_href,
34+
make_absolute_href,
35+
make_relative_href,
36+
)
3337

3438
if TYPE_CHECKING:
3539
from pystac.asset import Asset as Asset_Type
3640
from pystac.item import Item as Item_Type
3741
from pystac.collection import Collection as Collection_Type
3842

3943

40-
class CatalogType(str, Enum):
44+
class CatalogType(StringEnum):
4145
SELF_CONTAINED = "SELF_CONTAINED"
4246
"""A 'self-contained catalog' is one that is designed for portability.
4347
Users may want to download an online catalog from and be able to use it on their

pystac/extensions/datacube.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
from abc import ABC
7-
from enum import Enum
87
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union, cast
98

109
import pystac
@@ -13,7 +12,7 @@
1312
PropertiesExtension,
1413
)
1514
from pystac.extensions.hooks import ExtensionHooks
16-
from pystac.utils import get_required
15+
from pystac.utils import StringEnum, get_required
1716

1817
T = TypeVar("T", pystac.Collection, pystac.Item, pystac.Asset)
1918

@@ -42,22 +41,22 @@
4241
VAR_UNIT_PROP = "unit"
4342

4443

45-
class DimensionType(str, Enum):
44+
class DimensionType(StringEnum):
4645
"""Dimension object types for spatial and temporal Dimension Objects."""
4746

4847
SPATIAL = "spatial"
4948
TEMPORAL = "temporal"
5049

5150

52-
class HorizontalSpatialDimensionAxis(str, Enum):
51+
class HorizontalSpatialDimensionAxis(StringEnum):
5352
"""Allowed values for ``axis`` field of :class:`HorizontalSpatialDimension`
5453
object."""
5554

5655
X = "x"
5756
Y = "y"
5857

5958

60-
class VerticalSpatialDimensionAxis(str, Enum):
59+
class VerticalSpatialDimensionAxis(StringEnum):
6160
"""Allowed values for ``axis`` field of :class:`VerticalSpatialDimension`
6261
object."""
6362

@@ -407,7 +406,7 @@ def reference_system(self, v: Optional[Union[str, float, Dict[str, Any]]]) -> No
407406
self.properties[DIM_REF_SYS_PROP] = v
408407

409408

410-
class VariableType(str, Enum):
409+
class VariableType(StringEnum):
411410
"""Variable object types"""
412411

413412
DATA = "data"

pystac/extensions/file.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
https://github.com/stac-extensions/file
44
"""
55

6-
from enum import Enum
76
from typing import Any, Dict, List, Optional, Union
87

98
import pystac
@@ -14,7 +13,7 @@
1413
STACJSONDescription,
1514
STACVersionID,
1615
)
17-
from pystac.utils import get_required
16+
from pystac.utils import StringEnum, get_required
1817

1918
SCHEMA_URI = "https://stac-extensions.github.io/file/v2.0.0/schema.json"
2019

@@ -26,7 +25,7 @@
2625
VALUES_PROP = PREFIX + "values"
2726

2827

29-
class ByteOrder(str, Enum):
28+
class ByteOrder(StringEnum):
3029
"""List of allows values for the ``"file:byte_order"`` field defined by the
3130
:stac-ext:`File Info Extension <file>`."""
3231

pystac/extensions/label.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
https://github.com/stac-extensions/label
44
"""
55

6-
from enum import Enum
76
from pystac.extensions.base import ExtensionManagementMixin, SummariesExtension
87
from typing import Any, Dict, Iterable, List, Optional, Sequence, Union, cast
98

109
import pystac
1110
from pystac.serialization.identify import STACJSONDescription, STACVersionID
1211
from pystac.extensions.hooks import ExtensionHooks
13-
from pystac.utils import get_required, map_opt
12+
from pystac.utils import StringEnum, get_required, map_opt
1413

1514
SCHEMA_URI = "https://stac-extensions.github.io/label/v1.0.0/schema.json"
1615

@@ -25,7 +24,7 @@
2524
OVERVIEWS_PROP = PREFIX + "overviews"
2625

2726

28-
class LabelRelType(str, Enum):
27+
class LabelRelType(StringEnum):
2928
"""A list of rel types defined in the Label Extension.
3029
3130
See the :stac-ext:`Label Extension Links <label#links-source-imagery>`
@@ -36,7 +35,7 @@ class LabelRelType(str, Enum):
3635
"""Used to indicate a link to the source item to which a label item applies."""
3736

3837

39-
class LabelType(str, Enum):
38+
class LabelType(StringEnum):
4039
"""Enumerates valid label types ("raster" or "vector")."""
4140

4241
VECTOR = "vector"
@@ -46,7 +45,7 @@ class LabelType(str, Enum):
4645
"""Convenience attribute for checking if values are valid label types"""
4746

4847

49-
class LabelTask(str, Enum):
48+
class LabelTask(StringEnum):
5049
"""Enumerates recommended values for "label:tasks" field."""
5150

5251
REGRESSION = "regression"
@@ -55,7 +54,7 @@ class LabelTask(str, Enum):
5554
SEGMENTATION = "segmentation"
5655

5756

58-
class LabelMethod(str, Enum):
57+
class LabelMethod(StringEnum):
5958
"""Enumerates recommended values for "label:methods" field."""
6059

6160
AUTOMATED = "automated"

pystac/extensions/pointcloud.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
https://github.com/stac-extensions/pointcloud
44
"""
5-
from enum import Enum
65
from typing import Any, Dict, Generic, List, Optional, TypeVar, cast, Union
76

87
import pystac
@@ -13,7 +12,7 @@
1312
)
1413
from pystac.extensions.hooks import ExtensionHooks
1514
from pystac.summaries import RangeSummary
16-
from pystac.utils import map_opt, get_required
15+
from pystac.utils import StringEnum, map_opt, get_required
1716

1817
T = TypeVar("T", pystac.Item, pystac.Asset)
1918

@@ -28,7 +27,7 @@
2827
STATISTICS_PROP = PREFIX + "statistics"
2928

3029

31-
class PhenomenologyType(str, Enum):
30+
class PhenomenologyType(StringEnum):
3231
"""Valid values for the ``pc:type`` field in the :stac-ext:`Pointcloud Item
3332
Properties <pointcloud#item-properties>`."""
3433

@@ -39,7 +38,7 @@ class PhenomenologyType(str, Enum):
3938
OTHER = "other"
4039

4140

42-
class SchemaType(str, Enum):
41+
class SchemaType(StringEnum):
4342
"""Valid values for the ``type`` field in a :stac-ext:`Schema Object
4443
<pointcloud#schema-object>`."""
4544

pystac/extensions/raster.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
https://github.com/stac-extensions/raster
44
"""
55

6-
import enum
76
from typing import Any, Dict, Iterable, List, Optional, Union
87

98
import pystac
@@ -12,19 +11,19 @@
1211
PropertiesExtension,
1312
SummariesExtension,
1413
)
15-
from pystac.utils import get_opt, get_required, map_opt
14+
from pystac.utils import StringEnum, get_opt, get_required, map_opt
1615

1716
SCHEMA_URI = "https://stac-extensions.github.io/raster/v1.0.0/schema.json"
1817

1918
BANDS_PROP = "raster:bands"
2019

2120

22-
class Sampling(str, enum.Enum):
21+
class Sampling(StringEnum):
2322
AREA = "area"
2423
POINT = "point"
2524

2625

27-
class DataType(str, enum.Enum):
26+
class DataType(StringEnum):
2827
INT8 = "int8"
2928
INT16 = "int16"
3029
INT32 = "int32"

pystac/extensions/sar.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
https://github.com/stac-extensions/sar
44
"""
55

6-
import enum
76
from typing import Any, Dict, Generic, Iterable, List, Optional, TypeVar, cast, Union
87

98
import pystac
@@ -15,7 +14,7 @@
1514
SummariesExtension,
1615
)
1716
from pystac.extensions.hooks import ExtensionHooks
18-
from pystac.utils import get_required, map_opt
17+
from pystac.utils import StringEnum, get_required, map_opt
1918

2019
T = TypeVar("T", pystac.Item, pystac.Asset)
2120

@@ -40,7 +39,7 @@
4039
OBSERVATION_DIRECTION_PROP: str = PREFIX + "observation_direction"
4140

4241

43-
class FrequencyBand(str, enum.Enum):
42+
class FrequencyBand(StringEnum):
4443
P = "P"
4544
L = "L"
4645
S = "S"
@@ -51,14 +50,14 @@ class FrequencyBand(str, enum.Enum):
5150
KA = "Ka"
5251

5352

54-
class Polarization(str, enum.Enum):
53+
class Polarization(StringEnum):
5554
HH = "HH"
5655
VV = "VV"
5756
HV = "HV"
5857
VH = "VH"
5958

6059

61-
class ObservationDirection(str, enum.Enum):
60+
class ObservationDirection(StringEnum):
6261
LEFT = "left"
6362
RIGHT = "right"
6463

pystac/extensions/sat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
https://github.com/stac-extensions/sat
44
"""
55

6-
import enum
76
from datetime import datetime as Datetime
87
from pystac.summaries import RangeSummary
98
from typing import Dict, Any, List, Iterable, Generic, Optional, TypeVar, Union, cast
@@ -15,7 +14,7 @@
1514
SummariesExtension,
1615
)
1716
from pystac.extensions.hooks import ExtensionHooks
18-
from pystac.utils import str_to_datetime, datetime_to_str, map_opt
17+
from pystac.utils import StringEnum, str_to_datetime, datetime_to_str, map_opt
1918

2019
T = TypeVar("T", pystac.Item, pystac.Asset)
2120

@@ -31,7 +30,7 @@
3130
ANX_DATETIME_PROP: str = PREFIX + "anx_datetime"
3231

3332

34-
class OrbitState(str, enum.Enum):
33+
class OrbitState(StringEnum):
3534
ASCENDING = "ascending"
3635
DESCENDING = "descending"
3736
GEOSTATIONARY = "geostationary"

pystac/extensions/scientific.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"""
99

1010
import copy
11-
from enum import Enum
1211
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union, cast
1312
from urllib import parse
1413

@@ -19,7 +18,7 @@
1918
SummariesExtension,
2019
)
2120
from pystac.extensions.hooks import ExtensionHooks
22-
from pystac.utils import map_opt
21+
from pystac.utils import StringEnum, map_opt
2322

2423
T = TypeVar("T", pystac.Collection, pystac.Item)
2524

@@ -35,7 +34,7 @@
3534

3635

3736
# Link rel type.
38-
class ScientificRelType(str, Enum):
37+
class ScientificRelType(StringEnum):
3938
"""A list of rel types defined in the Scientific Citation Extension.
4039
4140
See the :stac-ext:`Scientific Citation Extension Relation types

pystac/extensions/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
https://github.com/stac-extensions/version
44
"""
5-
from enum import Enum
65
from pystac.utils import get_required, map_opt
76
from typing import Generic, List, Optional, TypeVar, Union, cast
87

98
import pystac
9+
from pystac.utils import StringEnum
1010
from pystac.extensions.base import (
1111
ExtensionManagementMixin,
1212
PropertiesExtension,
@@ -23,7 +23,7 @@
2323
DEPRECATED: str = "deprecated"
2424

2525

26-
class VersionRelType(str, Enum):
26+
class VersionRelType(StringEnum):
2727
"""A list of rel types defined in the Version Extension.
2828
2929
See the `Version Extension Relation types

pystac/media_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from enum import Enum
1+
from pystac.utils import StringEnum
22

33

4-
class MediaType(str, Enum):
4+
class MediaType(StringEnum):
55
"""A list of common media types that can be used in STAC Asset and Link metadata."""
66

77
COG = "image/tiff; application=geotiff; profile=cloud-optimized"

pystac/provider.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from enum import Enum
21
from typing import Any, Dict, List, Optional
32

3+
from pystac.utils import StringEnum
44

5-
class ProviderRole(str, Enum):
5+
6+
class ProviderRole(StringEnum):
67
"""Enumerates the allows values of the Provider "role" field."""
78

89
LICENSOR = "licensor"

pystac/rel_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from enum import Enum
1+
from pystac.utils import StringEnum
22

33

4-
class RelType(str, Enum):
4+
class RelType(StringEnum):
55
"""A list of common rel types that can be used in STAC Link metadata.
66
See :stac-spec:`"Using Relation Types <best-practices.md#using-relation-types>`
77
in the STAC Best Practices for guidelines on using relation types. You may also want

0 commit comments

Comments
 (0)