Skip to content

Commit a6e68e3

Browse files
authored
Bugfix for get_or_create_detector() unique name (#46)
* Fix get_or_create_detector() * Improve log message
1 parent 44ddb28 commit a6e68e3

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ packages = [
99
{include = "**/*.py", from = "src"},
1010
]
1111
readme = "README.md"
12-
version = "0.7.7"
12+
version = "0.7.8"
1313

1414
[tool.poetry.dependencies]
1515
certifi = "^2021.10.8"

src/groundlight/client.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from groundlight.binary_labels import convert_display_label_to_internal
1414
from groundlight.config import API_TOKEN_VARIABLE_NAME, API_TOKEN_WEB_URL
1515
from groundlight.images import parse_supported_image_types
16-
from groundlight.internalapi import GroundlightApiClient, sanitize_endpoint_url
16+
from groundlight.internalapi import GroundlightApiClient, NotFoundError, sanitize_endpoint_url
1717
from groundlight.optional_imports import Image, np
1818

1919
logger = logging.getLogger("groundlight.sdk")
@@ -109,25 +109,27 @@ def get_or_create_detector(
109109
confidence exists, return it. Otherwise, create a detector with the specified query and
110110
config.
111111
"""
112-
existing_detector = self.get_detector_by_name(name)
113-
if existing_detector:
114-
# TODO: We may soon allow users to update the retrieved detector's fields.
115-
if existing_detector.query != query:
116-
raise ValueError(
117-
f"Found existing detector with name={name} (id={existing_detector.id}) but the queries don't match."
118-
f" The existing query is '{existing_detector.query}'."
119-
)
120-
if confidence_threshold is not None and existing_detector.confidence_threshold != confidence_threshold:
121-
raise ValueError(
122-
f"Found existing detector with name={name} (id={existing_detector.id}) but the confidence"
123-
" thresholds don't match. The existing confidence threshold is"
124-
f" {existing_detector.confidence_threshold}."
125-
)
126-
return existing_detector
127-
128-
return self.create_detector(
129-
name=name, query=query, confidence_threshold=confidence_threshold, config_name=config_name
130-
)
112+
try:
113+
existing_detector = self.get_detector_by_name(name)
114+
except NotFoundError:
115+
logger.debug(f"We could not find a detector with name='{name}'. So we will create a new detector ...")
116+
return self.create_detector(
117+
name=name, query=query, confidence_threshold=confidence_threshold, config_name=config_name
118+
)
119+
120+
# TODO: We may soon allow users to update the retrieved detector's fields.
121+
if existing_detector.query != query:
122+
raise ValueError(
123+
f"Found existing detector with name={name} (id={existing_detector.id}) but the queries don't match."
124+
f" The existing query is '{existing_detector.query}'."
125+
)
126+
if confidence_threshold is not None and existing_detector.confidence_threshold != confidence_threshold:
127+
raise ValueError(
128+
f"Found existing detector with name={name} (id={existing_detector.id}) but the confidence"
129+
" thresholds don't match. The existing confidence threshold is"
130+
f" {existing_detector.confidence_threshold}."
131+
)
132+
return existing_detector
131133

132134
def get_image_query(self, id: str) -> ImageQuery: # pylint: disable=redefined-builtin
133135
obj = self.image_queries_api.get_image_query(id=id)

test/integration/test_groundlight.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ def test_list_detectors(gl: Groundlight):
100100
assert isinstance(detectors, PaginatedDetectorList)
101101

102102

103+
def test_get_or_create_detector(gl: Groundlight):
104+
# With a unique name, we should be creating a new detector.
105+
unique_name = f"Unique name {datetime.utcnow()}"
106+
query = "Test query?"
107+
detector = gl.get_or_create_detector(name=unique_name, query=query)
108+
assert str(detector)
109+
assert isinstance(detector, Detector)
110+
111+
# If we try to create a detector with the same name, we should get the same detector back.
112+
retrieved_detector = gl.get_or_create_detector(name=unique_name, query=query)
113+
assert retrieved_detector.id == detector.id
114+
115+
103116
def test_get_detector(gl: Groundlight, detector: Detector):
104117
_detector = gl.get_detector(id=detector.id)
105118
assert str(_detector)

0 commit comments

Comments
 (0)