From 6c98303522e2d9e6e83a6f76dc04b19baf6dcdea Mon Sep 17 00:00:00 2001 From: agentops Date: Sat, 17 Oct 2026 23:26:09 +0800 Subject: [PATCH] fix: LabelMeReader raises on unreadable input instead of returning None Closes #150 LabelMeReader.__init__ used to wrap file/JSON/URL loading in a broad try/except that swallowed OSError, json.JSONDecodeError, TypeError and ValueError and returned None, leaving a half-initialised reader whose callers only failed later with a confusing error. The original exception now propagates with its message and traceback. Scoped to this single quick-win item from #150; the other items in that umbrella issue are left for separate PRs. Signed-off-by: agentops --- src/machinevisiontoolbox/Sources.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/machinevisiontoolbox/Sources.py b/src/machinevisiontoolbox/Sources.py index efe1366a..7b3cb74a 100644 --- a/src/machinevisiontoolbox/Sources.py +++ b/src/machinevisiontoolbox/Sources.py @@ -4137,17 +4137,14 @@ class reads the JSON file and extracts the image, polygonal shapes, and associat def __init__(self, filename: str) -> None: self.filename = filename - try: - if str(filename).startswith(("http://", "https://")): - import urllib.request + if str(filename).startswith(("http://", "https://")): + import urllib.request - with urllib.request.urlopen(filename) as response: # noqa: S310 - self.data = json.loads(response.read().decode("utf-8")) - else: - with open(self.filename, "r", encoding="utf-8") as f: - self.data = json.load(f) - except (OSError, json.JSONDecodeError, TypeError, ValueError): - return None + with urllib.request.urlopen(filename) as response: # noqa: S310 + self.data = json.loads(response.read().decode("utf-8")) + else: + with open(self.filename, "r", encoding="utf-8") as f: + self.data = json.load(f) self._nshapes = len(self.data.get("shapes", [])) self._version = self.data.get("version")