Skip to content

Commit

Permalink
Fix mypy in a more neat way
Browse files Browse the repository at this point in the history
Signed-off-by: Kim, Vinnam <[email protected]>
  • Loading branch information
vinnamkim committed Apr 7, 2023
1 parent 774bf99 commit c5d476b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
11 changes: 7 additions & 4 deletions otx/api/entities/dataset_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import itertools
import logging
from threading import Lock
from typing import List, Optional, Sequence, Set, Tuple, Union
from typing import List, Optional, Sequence, Set, Tuple, TypeVar, Union
from bson import ObjectId
import numpy as np

Expand All @@ -28,6 +28,9 @@
logger = logging.getLogger(__name__)


T = TypeVar("T", bound="DatasetItemEntity")


class DatasetItemEntity(metaclass=abc.ABCMeta):
"""DatasetItemEntity represents an item in the DatasetEntity.
Expand Down Expand Up @@ -488,9 +491,9 @@ def get_metadata_by_name_and_model(self, name: str, model: Optional[ModelEntity]
"""
return [meta for meta in self.get_metadata() if meta.data.name == name and meta.model == model]

def wrap(self, **kwargs) -> "DatasetItemEntity":
def wrap(self: T, **kwargs) -> T:
"""Creates a new DatasetItemEntity, overriding only the given arguments to the existing ones for this instance."""
return DatasetItemEntity(
return self.__class__(
media=kwargs.get("media", self.media),
annotation_scene=kwargs.get("annotation_scene", self.annotation_scene),
roi=kwargs.get("roi", self.roi),
Expand Down Expand Up @@ -538,7 +541,7 @@ def __eq__(self, other):

def wrap(self, **kwargs) -> "DatasetItemEntityWithID":
"""Creates a new DatasetItemEntityWithID, overriding only the given arguments to the existing ones for this instance."""
return DatasetItemEntityWithID(
return self.__class__(
media=kwargs.get("media", self.media),
annotation_scene=kwargs.get("annotation_scene", self.annotation_scene),
roi=kwargs.get("roi", self.roi),
Expand Down
11 changes: 6 additions & 5 deletions otx/api/entities/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __next__(self) -> DatasetItemEntity:
return item


TDatasetItemEntity = TypeVar("TDatasetItemEntity", bound=DatasetItemEntity)
TDatasetItemEntity = TypeVar("TDatasetItemEntity", bound="DatasetItemEntity")


class DatasetEntity(Generic[TDatasetItemEntity]):
Expand Down Expand Up @@ -309,9 +309,10 @@ def with_empty_annotations(
Defaults to AnnotationSceneKind.PREDICTION
Returns:
DatasetEntity: a new dataset containing the same items, with empty annotation objects.
DatasetEntity: a new dataset containing the same items, with empty annotation objects.asdf
"""
new_dataset = DatasetEntity[DatasetItemEntity](purpose=self.purpose)
new_dataset = DatasetEntity[TDatasetItemEntity](purpose=self.purpose)

for dataset_item in self:
if isinstance(dataset_item, DatasetItemEntity):
empty_annotation = AnnotationSceneEntity(annotations=[], kind=annotation_kind)
Expand Down Expand Up @@ -360,7 +361,7 @@ def remove(self, item: TDatasetItemEntity) -> None:
This function calls remove_at_indices function.
Args:
item (TDatasetItemEntity): the item to be deleted.
item (DatasetItemEntity): the item to be deleted.
Raises:
ValueError: if the input item is not in the dataset
Expand All @@ -384,7 +385,7 @@ def append(self, item: TDatasetItemEntity) -> None:
>>> dataset.append(dataset_item)
Args:
item (TDatasetItemEntity): item to append
item (DatasetItemEntity): item to append
"""

if item.media is None:
Expand Down

0 comments on commit c5d476b

Please sign in to comment.