Skip to content

Commit

Permalink
lint: enable ruff FA100 rule
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Oct 29, 2024
1 parent 02e8698 commit 5e6daeb
Show file tree
Hide file tree
Showing 28 changed files with 173 additions and 141 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ ignore = [
"ISC003",
"N806",
"TID252",
"FA100",
]

[tool.mypy]
Expand Down
5 changes: 3 additions & 2 deletions raillabel/format/_attribute_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from enum import Enum


Expand All @@ -14,7 +15,7 @@ class AttributeType(Enum):
VEC = "vec"

@classmethod
def from_value(cls, attribute_value_class: t.Type) -> "AttributeType":
def from_value(cls, attribute_value_class: type) -> AttributeType:
"""Return AttributeType based on class of attribute value.
Parameters
Expand Down
25 changes: 13 additions & 12 deletions raillabel/format/_object_annotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from abc import ABC, abstractmethod, abstractproperty
from dataclasses import dataclass
from importlib import import_module
Expand All @@ -19,43 +20,43 @@ class _ObjectAnnotation(ABC):
uid: str
object: Object
sensor: Sensor
attributes: t.Dict[str, t.Union[int, float, bool, str, list]]
attributes: dict[str, int | float | bool | str | list]

@property
def name(self) -> str:
return f"{self.sensor.uid}__{self.OPENLABEL_ID}__{self.object.type}"

@property
@abstractproperty
def OPENLABEL_ID(self) -> t.List[str]:
def OPENLABEL_ID(self) -> list[str]:
raise NotImplementedError

# === Public Methods =====================================================

@abstractmethod
def asdict(self) -> t.Dict:
def asdict(self) -> dict:
raise NotImplementedError

@classmethod
@abstractmethod
def fromdict(
cls,
data_dict: t.Dict,
sensors: t.Dict,
data_dict: dict,
sensors: dict,
object: Object,
) -> t.Type["_ObjectAnnotation"]:
) -> type[_ObjectAnnotation]:
raise NotImplementedError

# === Private Methods ====================================================

def _annotation_required_fields_asdict(self) -> t.Dict:
def _annotation_required_fields_asdict(self) -> dict:
"""Return the required fields from the parent class to dict."""
return {
"uid": str(self.uid),
"name": str(self.name),
}

def _annotation_optional_fields_asdict(self) -> t.Dict:
def _annotation_optional_fields_asdict(self) -> dict:
"""Return the optional fields from the parent class to dict."""
dict_repr = {}

Expand All @@ -81,7 +82,7 @@ def _attributes_asdict(self, attributes: dict) -> dict:
return attributes_dict

@classmethod
def _coordinate_system_fromdict(cls, data_dict: dict, sensors: dict) -> t.Optional[Sensor]:
def _coordinate_system_fromdict(cls, data_dict: dict, sensors: dict) -> Sensor | None:
is_coordinate_system_in_data = (
"coordinate_system" in data_dict and data_dict["coordinate_system"] != ""
)
Expand All @@ -95,14 +96,14 @@ def _coordinate_system_fromdict(cls, data_dict: dict, sensors: dict) -> t.Option
def _attributes_fromdict(
cls,
data_dict: dict,
) -> t.Dict[str, t.Union[int, float, bool, str, list]]:
) -> dict[str, int | float | bool | str | list]:
if "attributes" not in data_dict:
return {}

return {a["name"]: a["val"] for type_ in data_dict["attributes"].values() for a in type_}


def annotation_classes() -> t.Dict[str, t.Type[_ObjectAnnotation]]:
def annotation_classes() -> dict[str, type[_ObjectAnnotation]]:
"""Return dictionary with _Annotation child classes."""
out = {}

Expand Down
4 changes: 3 additions & 1 deletion raillabel/format/bbox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from dataclasses import dataclass

from ._object_annotation import _ObjectAnnotation
Expand Down Expand Up @@ -42,7 +44,7 @@ class Bbox(_ObjectAnnotation):
OPENLABEL_ID = "bbox"

@classmethod
def fromdict(cls, data_dict: dict, sensors: dict, object: Object) -> "Bbox":
def fromdict(cls, data_dict: dict, sensors: dict, object: Object) -> Bbox:
"""Generate a Bbox object from a dict.
Parameters
Expand Down
4 changes: 3 additions & 1 deletion raillabel/format/cuboid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from dataclasses import dataclass

from ._object_annotation import _ObjectAnnotation
Expand Down Expand Up @@ -47,7 +49,7 @@ class Cuboid(_ObjectAnnotation):
OPENLABEL_ID = "cuboid"

@classmethod
def fromdict(cls, data_dict: dict, sensors: dict, object: Object) -> "Cuboid":
def fromdict(cls, data_dict: dict, sensors: dict, object: Object) -> Cuboid:
"""Generate a Cuboid object from a dict.
Parameters
Expand Down
11 changes: 6 additions & 5 deletions raillabel/format/element_data_pointer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from dataclasses import dataclass

from ._attribute_type import AttributeType
Expand Down Expand Up @@ -31,8 +32,8 @@ class ElementDataPointer:
"""

uid: str
frame_intervals: t.List[FrameInterval]
attribute_pointers: t.Dict[str, AttributeType]
frame_intervals: list[FrameInterval]
attribute_pointers: dict[str, AttributeType]

@property
def annotation_type(self) -> str:
Expand All @@ -59,10 +60,10 @@ def asdict(self) -> dict:
"attribute_pointers": self._attribute_pointers_asdict(),
}

def _frame_intervals_asdict(self) -> t.List[t.Dict[str, int]]:
def _frame_intervals_asdict(self) -> list[dict[str, int]]:
return [fi.asdict() for fi in self.frame_intervals]

def _attribute_pointers_asdict(self) -> t.Dict[str, str]:
def _attribute_pointers_asdict(self) -> dict[str, str]:
return {
attr_name: attr_type.value for attr_name, attr_type in self.attribute_pointers.items()
}
40 changes: 21 additions & 19 deletions raillabel/format/frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import decimal
import typing as t
import uuid
Expand Down Expand Up @@ -42,13 +44,13 @@ class Frame:
"""

uid: int
timestamp: t.Optional[decimal.Decimal] = None
sensors: t.Dict[str, SensorReference] = field(default_factory=dict)
frame_data: t.Dict[str, Num] = field(default_factory=dict)
annotations: t.Dict[str, t.Type[_ObjectAnnotation]] = field(default_factory=dict)
timestamp: decimal.Decimal | None = None
sensors: dict[str, SensorReference] = field(default_factory=dict)
frame_data: dict[str, Num] = field(default_factory=dict)
annotations: dict[str, type[_ObjectAnnotation]] = field(default_factory=dict)

@property
def object_data(self) -> t.Dict[str, t.Dict[str, t.Type[_ObjectAnnotation]]]:
def object_data(self) -> dict[str, dict[str, type[_ObjectAnnotation]]]:
"""Return annotations categorized by Object-Id.
Returns
Expand All @@ -72,9 +74,9 @@ def fromdict(
cls,
uid: str,
data_dict: dict,
objects: t.Dict[str, Object],
sensors: t.Dict[str, Sensor],
) -> "Frame":
objects: dict[str, Object],
sensors: dict[str, Sensor],
) -> Frame:
"""Generate a Frame object from a dict.
Parameters
Expand Down Expand Up @@ -140,16 +142,16 @@ def asdict(self) -> dict:
return dict_repr

@classmethod
def _timestamp_fromdict(cls, data_dict: dict) -> t.Optional[decimal.Decimal]:
def _timestamp_fromdict(cls, data_dict: dict) -> decimal.Decimal | None:
if "frame_properties" not in data_dict or "timestamp" not in data_dict["frame_properties"]:
return None

return decimal.Decimal(data_dict["frame_properties"]["timestamp"])

@classmethod
def _sensors_fromdict(
cls, data_dict: dict, scene_sensors: t.Dict[str, Sensor]
) -> t.Dict[str, SensorReference]:
cls, data_dict: dict, scene_sensors: dict[str, Sensor]
) -> dict[str, SensorReference]:
if "frame_properties" not in data_dict or "streams" not in data_dict["frame_properties"]:
return {}

Expand All @@ -163,7 +165,7 @@ def _sensors_fromdict(
return sensors

@classmethod
def _frame_data_fromdict(cls, data_dict: dict, sensors: t.Dict[str, Sensor]) -> t.Dict[str, Num]:
def _frame_data_fromdict(cls, data_dict: dict, sensors: dict[str, Sensor]) -> dict[str, Num]:
if "frame_properties" not in data_dict or "frame_data" not in data_dict["frame_properties"]:
return {}

Expand All @@ -178,9 +180,9 @@ def _frame_data_fromdict(cls, data_dict: dict, sensors: t.Dict[str, Sensor]) ->
def _objects_fromdict(
cls,
data_dict: dict,
objects: t.Dict[str, Object],
sensors: t.Dict[str, Sensor],
) -> t.Dict[uuid.UUID, t.Type[_ObjectAnnotation]]:
objects: dict[str, Object],
sensors: dict[str, Sensor],
) -> dict[uuid.UUID, type[_ObjectAnnotation]]:
if "objects" not in data_dict:
return {}

Expand All @@ -203,18 +205,18 @@ def _object_annotations_fromdict(
cls,
data_dict: dict,
object: Object,
sensors: t.Dict[str, Sensor],
) -> t.Iterator[t.Type[_ObjectAnnotation]]:
sensors: dict[str, Sensor],
) -> t.Iterator[type[_ObjectAnnotation]]:
for ann_type, annotations_raw in data_dict.items():
for ann_raw in annotations_raw:
yield annotation_classes()[ann_type].fromdict(ann_raw, sensors, object)

def _annotations_asdict(self) -> dict:
annotations_dict = {}
for object_id, annotations in self.object_data.items():
for object_id, annotations_ in self.object_data.items():
annotations_dict[object_id] = {"object_data": {}}

for annotation in annotations.values():
for annotation in annotations_.values():
if annotation.OPENLABEL_ID not in annotations_dict[object_id]["object_data"]:
annotations_dict[object_id]["object_data"][annotation.OPENLABEL_ID] = []

Expand Down
9 changes: 5 additions & 4 deletions raillabel/format/frame_interval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from dataclasses import dataclass


Expand All @@ -22,7 +23,7 @@ class FrameInterval:
frame_end: int

@classmethod
def fromdict(cls, data_dict: dict) -> "FrameInterval":
def fromdict(cls, data_dict: dict) -> FrameInterval:
"""Generate a FrameInterval object from a dict.
Parameters
Expand All @@ -37,7 +38,7 @@ def fromdict(cls, data_dict: dict) -> "FrameInterval":
)

@classmethod
def from_frame_uids(cls, frame_uids: t.List[int]) -> t.List["FrameInterval"]:
def from_frame_uids(cls, frame_uids: list[int]) -> list[FrameInterval]:
"""Convert a list of frame uids into FrameIntervals.
Parameters
Expand Down Expand Up @@ -92,7 +93,7 @@ def __len__(self) -> int:
return abs(self.frame_start - self.frame_end) + 1

@classmethod
def _slice_into_intervals(cls, sorted_frame_uids: t.List[int]) -> t.List[t.List[int]]:
def _slice_into_intervals(cls, sorted_frame_uids: list[int]) -> list[list[int]]:
if len(sorted_frame_uids) == 0:
return []

Expand Down
9 changes: 5 additions & 4 deletions raillabel/format/intrinsics_pinhole.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import typing as t
from __future__ import annotations

from dataclasses import dataclass


Expand All @@ -27,13 +28,13 @@ class IntrinsicsPinhole:
"""

camera_matrix: t.Tuple[float, ...]
distortion: t.Tuple[float, ...]
camera_matrix: tuple[float, ...]
distortion: tuple[float, ...]
width_px: int
height_px: int

@classmethod
def fromdict(cls, data_dict: dict) -> "IntrinsicsPinhole":
def fromdict(cls, data_dict: dict) -> IntrinsicsPinhole:
"""Generate a IntrinsicsPinhole object from a dict.
Parameters
Expand Down
4 changes: 3 additions & 1 deletion raillabel/format/intrinsics_radar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

from dataclasses import dataclass


Expand All @@ -25,7 +27,7 @@ class IntrinsicsRadar:
height_px: int

@classmethod
def fromdict(cls, data_dict: dict) -> "IntrinsicsRadar":
def fromdict(cls, data_dict: dict) -> IntrinsicsRadar:
"""Generate a IntrinsicsRadar object from a dict.
Parameters
Expand Down
Loading

0 comments on commit 5e6daeb

Please sign in to comment.