Fix type annotation for NumPy 2.3.0

Fix the following issues in py313-test_galaxy_packages :

```
galaxy/tool_util/verify/asserts/image.py:763: error: Incompatible types in
assignment (expression has type "list[Any]", variable has type
"ndarray[tuple[Any, ...], dtype[Any]]")  [assignment]
            present_labels = [label for label in present_labels if label i...
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...
galaxy/tool_util/verify/asserts/image.py:770: error: Incompatible types in
assignment (expression has type "list[Any]", variable has type
"ndarray[tuple[Any, ...], dtype[Any]]")  [assignment]
        present_labels = [label for label in present_labels if label not i...
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...
galaxy/tool_util/verify/asserts/image.py:773: error: Incompatible return value
type (got
"tuple[ndarray[tuple[Any, ...], dtype[Any]], ndarray[tuple[Any, ...], dtype[Any]]]",
expected "tuple[ndarray[tuple[Any, ...], dtype[Any]], list[Any]]")
[return-value]
        return im_arr, present_labels
               ^~~~~~~~~~~~~~~~~~~~~~
```
This commit is contained in:
Nicola Soranzo
2025-06-08 14:04:56 +01:00
parent 162fe28318
commit 252a7fd72c
+10 -10
View File
@@ -320,19 +320,19 @@ CenterOfMassEps = Annotated[
),
]
Labels = Annotated[
Optional[Union[str, List[int]]],
Optional[Union[str, List[Union[float, int]]]],
AssertionParameter(
"List of labels, separated by a comma. Labels *not* on this list will be excluded from consideration. Cannot be used in combination with ``exclude_labels``.",
xml_type="xs:string",
json_type="typing.Optional[typing.List[int]]",
json_type=f"typing.Optional[typing.List[{JSON_STRICT_NUMBER}]]",
),
]
ExcludeLabels = Annotated[
Optional[Union[str, List[int]]],
Optional[Union[str, List[Union[float, int]]]],
AssertionParameter(
"List of labels to be excluded from consideration, separated by a comma. The primary usage of this attribute is to exclude the background of a label image. Cannot be used in combination with ``labels``.",
xml_type="xs:string",
json_type="typing.Optional[typing.List[int]]",
json_type=f"typing.Optional[typing.List[{JSON_STRICT_NUMBER}]]",
),
]
MeanObjectSize = Annotated[
@@ -734,8 +734,8 @@ def _get_image_labels(
channel: Optional[Union[int, str]] = None,
slice: Optional[Union[int, str]] = None,
frame: Optional[Union[int, str]] = None,
labels: Optional[Union[str, List[int]]] = None,
exclude_labels: Optional[Union[str, List[int]]] = None,
labels: Labels = None,
exclude_labels: ExcludeLabels = None,
) -> Tuple["numpy.typing.NDArray", List[Any]]:
"""
Determines the unique labels in the output image or a specific channel.
@@ -743,7 +743,7 @@ def _get_image_labels(
assert labels is None or exclude_labels is None
im_arr = _get_image(output_bytes, channel, slice, frame)
def cast_label(label):
def cast_label(label: str) -> Union[float, int]:
label = label.strip()
if numpy.issubdtype(im_arr.dtype, numpy.integer):
return int(label)
@@ -752,12 +752,12 @@ def _get_image_labels(
raise AssertionError(f'Unsupported image label type: "{im_arr.dtype}"')
# Determine labels present in the image.
present_labels = numpy.unique(im_arr)
present_labels: List[Any] = numpy.unique(im_arr).tolist()
# Apply filtering due to `labels` (keep only those).
if labels is None:
labels = []
if isinstance(labels, str):
elif isinstance(labels, str):
labels = [cast_label(label) for label in labels.split(",") if len(label) > 0]
if len(labels) > 0:
present_labels = [label for label in present_labels if label in labels]
@@ -765,7 +765,7 @@ def _get_image_labels(
# Apply filtering due to `exclude_labels`.
if exclude_labels is None:
exclude_labels = []
if isinstance(exclude_labels, str):
elif isinstance(exclude_labels, str):
exclude_labels = [cast_label(label) for label in exclude_labels.split(",") if len(label) > 0]
present_labels = [label for label in present_labels if label not in exclude_labels]