Skip to content

Commit

Permalink
Fix typing errors reported by mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
hackermd committed Dec 1, 2021
1 parent 3b92850 commit 18dd62d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/highdicom/ann/content.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Content that is specific to Annotation IODs."""
from copy import deepcopy
from typing import List, Optional, Sequence, Tuple, Union
from typing import cast, List, Optional, Sequence, Tuple, Union

import numpy as np
from pydicom.dataset import Dataset
Expand All @@ -24,7 +24,7 @@ class Measurements(Dataset):
def __init__(
self,
name: Union[Code, CodedConcept],
values: np.array,
values: np.ndarray,
unit: Union[Code, CodedConcept]
) -> None:
"""
Expand Down Expand Up @@ -152,7 +152,7 @@ def from_dataset(cls, dataset: Dataset) -> 'Measurements':
)
]

return measurements
return cast(Measurements, measurements)


class AnnotationGroup(Dataset):
Expand Down Expand Up @@ -588,9 +588,9 @@ def get_measurements(
if name is None or item.name == name
]
if len(values) > 0:
values = np.vstack(values).T
value_array = np.vstack(values).T
else:
values = np.empty((number_of_annotations, 0), np.float32)
value_array = np.empty((number_of_annotations, 0), np.float32)
names = [
item.name for item in self.MeasurementsSequence
if name is None or item.name == name
Expand All @@ -600,10 +600,10 @@ def get_measurements(
if name is None or item.name == name
]
else:
values = np.empty((number_of_annotations, 0), np.float32)
value_array = np.empty((number_of_annotations, 0), np.float32)
names = []
units = []
return (names, values, units)
return (names, value_array, units)

def _get_coordinate_index(
self,
Expand Down Expand Up @@ -726,4 +726,4 @@ def from_dataset(cls, dataset: Dataset) -> 'AnnotationGroup':
for ds in group.PrimaryAnatomicStructureSequence
]

return group
return cast(AnnotationGroup, group)
4 changes: 2 additions & 2 deletions src/highdicom/ann/sop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import defaultdict
from copy import deepcopy
from operator import eq
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
from typing import Any, cast, Dict, List, Optional, Sequence, Tuple, Union

import numpy as np
from pydicom.dataset import Dataset
Expand Down Expand Up @@ -417,4 +417,4 @@ def from_dataset(
for item in ann.AnnotationGroupSequence
]

return ann
return cast(MicroscopyBulkSimpleAnnotations, ann)
1 change: 1 addition & 0 deletions src/highdicom/ko/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
meaning='Source',
)
for ds in referenced_objects:
reference_item: Union[ImageContentItem, CompositeContentItem]
if 'Rows' in ds and 'Columns' in ds:
reference_item = ImageContentItem(
name=name,
Expand Down
3 changes: 1 addition & 2 deletions src/highdicom/pm/sop.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def __init__(
raise ValueError('Pixel array must be a 2D, 3D, or 4D array.')

# Acquisition Context
self.AcquisitionContextSequence = []
self.AcquisitionContextSequence: List[Dataset] = []

# Image Pixel
self.Rows = pixel_array.shape[1]
Expand Down Expand Up @@ -784,7 +784,6 @@ def _encode_frame(self, pixel_array: np.ndarray) -> bytes:
'in case of encapsulated format encoding.'
)
if self.file_meta.TransferSyntaxUID.is_encapsulated:
# Check that only a single plane was passed
return encode_frame(
pixel_array,
transfer_syntax_uid=self.file_meta.TransferSyntaxUID,
Expand Down
2 changes: 2 additions & 0 deletions src/highdicom/sr/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4023,6 +4023,7 @@ def get_planar_roi_measurement_groups(
matches.append(found_ref_type == reference_type)

if graphic_type is not None:
found_gt: Union[GraphicTypeValues, GraphicTypeValues3D]
if isinstance(graphic_type, GraphicTypeValues):
if ref_value_type == ValueTypeValues.SCOORD:
found_gt = GraphicTypeValues(ref_item.GraphicType)
Expand Down Expand Up @@ -4278,6 +4279,7 @@ def get_volumetric_roi_measurement_groups(
matches.append(found_ref_type == reference_type)

if graphic_type is not None:
found_gt: Union[GraphicTypeValues, GraphicTypeValues3D]
if isinstance(graphic_type, GraphicTypeValues):
if ref_value_type == ValueTypeValues.SCOORD:
found_gt = GraphicTypeValues(
Expand Down
2 changes: 1 addition & 1 deletion src/highdicom/sr/value_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _from_dataset_base(cls, dataset: Dataset) -> 'ContentItem':
item.ConceptNameCodeSequence = [
CodedConcept.from_dataset(item.ConceptNameCodeSequence[0])
]
return item
return cast(ContentItem, item)

@property
def name(self) -> CodedConcept:
Expand Down

0 comments on commit 18dd62d

Please sign in to comment.