Skip to content

Commit 81daa6a

Browse files
petercorkezw1915claude
authored
fix: LabelMeReader raises on unreadable input instead of returning None (#208)
Remove the broad try/except in LabelMeReader.__init__, which swallowed OSError and JSON errors and returned a half-initialised reader. The original exception now propagates. Adds tests for a missing file and for invalid JSON. Refs #150 Co-authored-by: zw1915 <134981642+zw1915@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 3594254 commit 81daa6a

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

‎src/machinevisiontoolbox/Sources.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,6 +4444,12 @@ class LabelMeReader:
44444444
44454445
:param filename: path to LabelMe JSON file, or an ``http(s)://`` URL
44464446
:type filename: str
4447+
:raises OSError: if the file cannot be opened or the URL cannot be fetched
4448+
:raises json.JSONDecodeError: if the content is not valid JSON
4449+
4450+
.. versionchanged:: 2.5.0
4451+
An unreadable file or invalid JSON now raises, where it used to return an
4452+
incompletely initialised reader.
44474453
44484454
LabelMe is a popular annotation tool that saves annotations in a JSON format. This
44494455
class reads the JSON file and extracts the image, polygonal shapes, and associated
@@ -4492,17 +4498,14 @@ class reads the JSON file and extracts the image, polygonal shapes, and associat
44924498
def __init__(self, filename: str) -> None:
44934499
self.filename = filename
44944500

4495-
try:
4496-
if str(filename).startswith(("http://", "https://")):
4497-
import urllib.request
4501+
if str(filename).startswith(("http://", "https://")):
4502+
import urllib.request
44984503

4499-
with urllib.request.urlopen(filename) as response: # noqa: S310
4500-
self.data = json.loads(response.read().decode("utf-8"))
4501-
else:
4502-
with open(self.filename, "r", encoding="utf-8") as f:
4503-
self.data = json.load(f)
4504-
except (OSError, json.JSONDecodeError, TypeError, ValueError):
4505-
return None
4504+
with urllib.request.urlopen(filename) as response: # noqa: S310
4505+
self.data = json.loads(response.read().decode("utf-8"))
4506+
else:
4507+
with open(self.filename, "r", encoding="utf-8") as f:
4508+
self.data = json.load(f)
45064509
self._nshapes = len(self.data.get("shapes", []))
45074510
self._version = self.data.get("version")
45084511

‎tests/test_image_sources.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,27 @@ def test_labelme_read(self):
607607
self.assertEqual(shapes[1].flags, {"hard": False})
608608

609609

610+
class TestLabelMeReaderErrors(unittest.TestCase):
611+
"""A LabelMe file that cannot be read raises, it does not return a half-built reader."""
612+
613+
def test_missing_file_raises(self):
614+
from machinevisiontoolbox import LabelMeReader
615+
616+
with tempfile.TemporaryDirectory() as tmp:
617+
with self.assertRaises(FileNotFoundError):
618+
LabelMeReader(os.path.join(tmp, "missing.json"))
619+
620+
def test_invalid_json_raises(self):
621+
from machinevisiontoolbox import LabelMeReader
622+
623+
with tempfile.TemporaryDirectory() as tmp:
624+
path = os.path.join(tmp, "bad.json")
625+
with open(path, "w", encoding="utf-8") as f:
626+
f.write("{ this is not json")
627+
with self.assertRaises(json.JSONDecodeError):
628+
LabelMeReader(path)
629+
630+
610631
class TestEarthViewKey(unittest.TestCase):
611632
"""How EarthView finds its API key (no network access needed)."""
612633

0 commit comments

Comments
 (0)