Skip to content

Commit

Permalink
Ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
clinton-encord committed Jan 2, 2025
1 parent bc86ace commit 5c18d7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
15 changes: 10 additions & 5 deletions encord/objects/ontology_labels_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@
BitmaskCoordinates,
BoundingBoxCoordinates,
Coordinates,
HtmlCoordinates,
PointCoordinate,
PolygonCoordinates,
PolylineCoordinates,
RotatableBoundingBoxCoordinates,
SkeletonCoordinates,
TextCoordinates, HtmlCoordinates,
TextCoordinates,
)
from encord.objects.frames import Frames, Range, Ranges, frames_class_to_frames_list, frames_to_ranges
from encord.objects.html_node import HtmlRange
Expand Down Expand Up @@ -833,11 +834,15 @@ def add_object_instance(self, object_instance: ObjectInstance, force: bool = Tru
# We want to ensure that we are only adding the object_instance to a label_row
# IF AND ONLY IF the file type is text/html and the object_instance has range_html set
if self.file_type == "text/html" and object_instance.range_html is None:
raise LabelRowError("Unable to assign object instance without a html range to a html file. "
f"Please ensure the object instance exists on frame=0, and has coordinates of type {HtmlCoordinates}.")
raise LabelRowError(
"Unable to assign object instance without a html range to a html file. "
f"Please ensure the object instance exists on frame=0, and has coordinates of type {HtmlCoordinates}."
)
elif self.file_type != "text/html" and object_instance.range_html is not None:
raise LabelRowError("Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}.")
raise LabelRowError(
"Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}."
)

if object_instance.is_assigned_to_label_row():
raise LabelRowError(
Expand Down
16 changes: 9 additions & 7 deletions encord/objects/ontology_object_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
from encord.objects.constants import DEFAULT_CONFIDENCE, DEFAULT_MANUAL_ANNOTATION
from encord.objects.coordinates import (
ACCEPTABLE_COORDINATES_FOR_ONTOLOGY_ITEMS,
NON_GEOMETRIC_COORDINATES,
AudioCoordinates,
Coordinates,
TextCoordinates, HtmlCoordinates, NON_GEOMETRIC_COORDINATES,
HtmlCoordinates,
TextCoordinates,
)
from encord.objects.frames import (
Frames,
Expand Down Expand Up @@ -960,7 +962,9 @@ def check_coordinate_type(coordinates: Coordinates, ontology_object: Object, par
LabelRowError: If the coordinate type does not match the expected type.
"""
expected_coordinate_types = ACCEPTABLE_COORDINATES_FOR_ONTOLOGY_ITEMS[ontology_object.shape]
if all(not isinstance(coordinates, expected_coordinate_type) for expected_coordinate_type in expected_coordinate_types):
if all(
not isinstance(coordinates, expected_coordinate_type) for expected_coordinate_type in expected_coordinate_types
):
raise LabelRowError(
f"Expected coordinates of one of the following types: `{expected_coordinate_types}`, but got type `{type(coordinates)}`."
)
Expand All @@ -970,16 +974,14 @@ def check_coordinate_type(coordinates: Coordinates, ontology_object: Object, par
# HTML files, and `TextCoordinates` are only used for plain text files.
if isinstance(coordinates, TextCoordinates):
if parent is not None and parent == "text/html":
raise LabelRowError(
f"Expected coordinates of type {HtmlCoordinates}`, but got type `{type(coordinates)}`."
)
raise LabelRowError(f"Expected coordinates of type {HtmlCoordinates}`, but got type `{type(coordinates)}`.")
elif isinstance(coordinates, HtmlCoordinates):
if parent is not None and parent.file_type != "text/html":
raise LabelRowError(
"For non-html labels, ensure the `range` property "
"is set when instantiating the TextCoordinates."
"For non-html labels, ensure the `range` property " "is set when instantiating the TextCoordinates."
)


class DynamicAnswerManager:
"""
Manages dynamic answers for different frames of an ObjectInstance.
Expand Down
30 changes: 19 additions & 11 deletions tests/objects/test_label_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
from encord.objects.coordinates import (
AudioCoordinates,
BoundingBoxCoordinates,
HtmlCoordinates,
PointCoordinate,
PolygonCoordinates,
TextCoordinates, HtmlCoordinates,
TextCoordinates,
)
from encord.objects.frames import Range
from encord.objects.html_node import HtmlNode, HtmlRange
Expand Down Expand Up @@ -1381,14 +1382,19 @@ def test_html_text_object_cannot_be_added_to_non_html_label_row(
with pytest.raises(LabelRowError) as e:
empty_audio_label_row.add_object_instance(obj_instance)

assert str(e.value.message) == ("Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}.")
assert str(e.value.message) == (
"Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}."
)

with pytest.raises(LabelRowError) as e:
empty_plain_text_label_row.add_object_instance(obj_instance)

assert str(e.value.message) == ("Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}.")
assert str(e.value.message) == (
"Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}."
)


def test_set_for_frames_with_range_html_throws_error_if_used_incorrectly(
ontology, empty_html_text_label_row: LabelRowV2, empty_plain_text_label_row: LabelRowV2
Expand All @@ -1406,7 +1412,8 @@ def test_set_for_frames_with_range_html_throws_error_if_used_incorrectly(
audio_obj_instance.set_for_frames(coordinates=HtmlCoordinates(range=range_html))

assert (
str(e.value.message) == f"Expected coordinates of one of the following types: `[{AudioCoordinates}]`, but got type `{HtmlCoordinates}`."
str(e.value.message)
== f"Expected coordinates of one of the following types: `[{AudioCoordinates}]`, but got type `{HtmlCoordinates}`."
)

# Adding HtmlCoordinates to an object instance which is attached to a label row where the
Expand All @@ -1418,9 +1425,8 @@ def test_set_for_frames_with_range_html_throws_error_if_used_incorrectly(
empty_plain_text_label_row.add_object_instance(html_text_obj_instance)

assert (
str(e.value.message)
== "Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}."
str(e.value.message) == "Unable to assign object instance with a html range to a non-html file. "
f"Please ensure the object instance does not have coordinates of type {HtmlCoordinates}."
)


Expand Down Expand Up @@ -1506,5 +1512,7 @@ def test_plain_text_object_cannot_be_added_to_html_label_row(ontology, empty_htm
with pytest.raises(LabelRowError) as e:
label_row.add_object_instance(obj_instance)

assert str(e.value.message) == ("Unable to assign object instance without a html range to a html file. "
f"Please ensure the object instance exists on frame=0, and has coordinates of type {HtmlCoordinates}.")
assert str(e.value.message) == (
"Unable to assign object instance without a html range to a html file. "
f"Please ensure the object instance exists on frame=0, and has coordinates of type {HtmlCoordinates}."
)

0 comments on commit 5c18d7a

Please sign in to comment.