Skip to content

Commit ff842f5

Browse files
authored
Use Sequence for annotations for label classes (#627)
* Use Sequence for annotations for label classes Per https://mypy.readthedocs.io/en/stable/common_issues.html#variance, `List` is invariant, so the LabelClasses.classes type annotation is unncessarily strict. This commit relaxes the type annotation to use `Sequence`, including a simple unit test. * Update CHANGELOG for #627
1 parent 021bae7 commit ff842f5

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
as `root.stac_io`, if that value is not `None`
1818
([#590](https://github.com/stac-utils/pystac/pull/590))
1919
- Links will get their `title` from their target if no `title` is provided ([#607](https://github.com/stac-utils/pystac/pull/607))
20+
- Relax typing on `LabelClasses` from `List` to `Sequence` ([#627](https://github.com/stac-utils/pystac/pull/627))
2021

2122
### Fixed
2223

pystac/extensions/label.py

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

66
from enum import Enum
77
from pystac.extensions.base import ExtensionManagementMixin, SummariesExtension
8-
from typing import Any, Dict, Iterable, List, Optional, Union, cast
8+
from typing import Any, Dict, Iterable, List, Optional, Sequence, Union, cast
99

1010
import pystac
1111
from pystac.serialization.identify import STACJSONDescription, STACVersionID
@@ -73,7 +73,7 @@ def __init__(self, properties: Dict[str, Any]):
7373

7474
def apply(
7575
self,
76-
classes: List[Union[str, int, float]],
76+
classes: Sequence[Union[str, int, float]],
7777
name: Optional[str] = None,
7878
) -> None:
7979
"""Sets the properties for this instance.
@@ -90,7 +90,7 @@ def apply(
9090
@classmethod
9191
def create(
9292
cls,
93-
classes: List[Union[str, int, float]],
93+
classes: Sequence[Union[str, int, float]],
9494
name: Optional[str] = None,
9595
) -> "LabelClasses":
9696
"""Creates a new :class:`~LabelClasses` instance.
@@ -106,12 +106,12 @@ def create(
106106
return c
107107

108108
@property
109-
def classes(self) -> List[Union[str, int, float]]:
109+
def classes(self) -> Sequence[Union[str, int, float]]:
110110
"""Gets or sets the class values."""
111111
return get_required(self.properties.get("classes"), self, "classes")
112112

113113
@classes.setter
114-
def classes(self, v: List[Union[str, int, float]]) -> None:
114+
def classes(self, v: Sequence[Union[str, int, float]]) -> None:
115115
self.properties["classes"] = v
116116

117117
@property

tests/extensions/test_label.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ def test_label_classes(self) -> None:
248248

249249
label_item.validate()
250250

251+
def test_label_classes_typing(self) -> None:
252+
classes: List[str] = ["foo", "bar"]
253+
LabelClasses.create(classes=classes)
254+
251255
def test_label_tasks(self) -> None:
252256
label_item = pystac.Item.from_file(self.label_example_1_uri)
253257

0 commit comments

Comments
 (0)