Skip to content

Commit

Permalink
Use Sequence for annotations for label classes (#627)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gadomski authored Sep 27, 2021
1 parent 021bae7 commit ff842f5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
as `root.stac_io`, if that value is not `None`
([#590](https://github.com/stac-utils/pystac/pull/590))
- Links will get their `title` from their target if no `title` is provided ([#607](https://github.com/stac-utils/pystac/pull/607))
- Relax typing on `LabelClasses` from `List` to `Sequence` ([#627](https://github.com/stac-utils/pystac/pull/627))

### Fixed

Expand Down
10 changes: 5 additions & 5 deletions pystac/extensions/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

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

def apply(
self,
classes: List[Union[str, int, float]],
classes: Sequence[Union[str, int, float]],
name: Optional[str] = None,
) -> None:
"""Sets the properties for this instance.
Expand All @@ -90,7 +90,7 @@ def apply(
@classmethod
def create(
cls,
classes: List[Union[str, int, float]],
classes: Sequence[Union[str, int, float]],
name: Optional[str] = None,
) -> "LabelClasses":
"""Creates a new :class:`~LabelClasses` instance.
Expand All @@ -106,12 +106,12 @@ def create(
return c

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

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

@property
Expand Down
4 changes: 4 additions & 0 deletions tests/extensions/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ def test_label_classes(self) -> None:

label_item.validate()

def test_label_classes_typing(self) -> None:
classes: List[str] = ["foo", "bar"]
LabelClasses.create(classes=classes)

def test_label_tasks(self) -> None:
label_item = pystac.Item.from_file(self.label_example_1_uri)

Expand Down

0 comments on commit ff842f5

Please sign in to comment.