Skip to content

Commit 9d3d695

Browse files
authored
enhancement: raise notimplementederror on single index rather than random error (#450)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Explicitly raises NotImplementedError when single-indexing `TextRegions`; adds a skipped test, updates changelog, and bumps version to 1.1.1. > > - **Elements**: > - `unstructured_inference/inference/elements.py`: Single-element indexing of `TextRegions` now raises `NotImplementedError` in `slice()` (used by `__getitem__`). > - **Tests**: > - `test_unstructured_inference/test_elements.py`: Adds a skipped test covering single-element access expectations. > - **Release**: > - `__version__` bumped to `1.1.1`. > - `CHANGELOG.md`: Notes the new `NotImplementedError` behavior for single indexing. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 64b9438. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent f8bf10d commit 9d3d695

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.1
2+
3+
* Add NotImplementedError when trying to single index a TextRegions, reflecting the fact that it won't behave correctly at the moment.
4+
15
## 1.1.0
26

37
* Enhancement: Add `TextSource` to track where the text of an element came from

test_unstructured_inference/test_elements.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,31 @@ def test_textregions_from_coords_accepts_source():
593593

594594
assert region.source == Source.YOLOX
595595
assert region.is_extracted
596+
597+
598+
@pytest.mark.skip(reason="Not implemented")
599+
def test_textregions_allows_for_single_element_access_and_returns_textregion_with_correct_values():
600+
"""Test that TextRegions allows for single element access and returns a TextRegion with the
601+
correct values"""
602+
603+
regions = [
604+
TextRegion.from_coords(
605+
0, 0, 10, 10, text="first", source=Source.YOLOX, is_extracted=IsExtracted.TRUE
606+
),
607+
TextRegion.from_coords(
608+
0,
609+
0,
610+
20,
611+
20,
612+
text="second",
613+
source=Source.DETECTRON2_ONNX,
614+
is_extracted=IsExtracted.PARTIAL,
615+
),
616+
]
617+
text_regions = TextRegions.from_list(regions)
618+
for i, region in enumerate(regions):
619+
sliced = text_regions[i]
620+
assert isinstance(sliced, TextRegion)
621+
assert sliced.text == region.text
622+
assert sliced.source == region.source
623+
assert sliced.is_extracted is region.is_extracted
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.0" # pragma: no cover
1+
__version__ = "1.1.1" # pragma: no cover

unstructured_inference/inference/elements.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ def slice(self, indices) -> TextRegions:
253253
# NOTE(alan): I would expect if I try to access a single element, it should return a
254254
# TextRegion, not a TextRegions. Currently, you get an error when trying to access a single
255255
# element.
256+
if self.element_coords[indices].ndim == 1:
257+
# We've indexed a single element. For now this isn't implemented.
258+
raise NotImplementedError("Slicing a single element is not implemented")
256259
return TextRegions(
257260
element_coords=self.element_coords[indices],
258261
texts=self.texts[indices],

0 commit comments

Comments
 (0)