Skip to content

Commit 964ba4e

Browse files
authored
Fix TypeError in by_name() when name is not found (#83)
* Fix TypeError in by_name() when name is not found When using `match="equals"` (the default), `by_name()` would return `None` from `dict.get(name, None)` if the name wasn't found in the lookup cache. This caused `len(matches)` on line 51 to raise: TypeError: object of type 'NoneType' has no len() This fix changes the default value from `None` to `[]` (empty list), making it consistent with the `match="contains"` branch which already initializes `matches = []`. * fix: move empty result check up before checking if user asked for all * add(test): tests for consistent None return for by_name method
1 parent c13bab0 commit 964ba4e

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

pipeline/src/additional_methods/by_name.py.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@
3838
else:
3939
cls._instance_lookup[key] = [instance]
4040
if match == "equals":
41-
matches = cls._instance_lookup.get(name, None)
41+
matches = cls._instance_lookup.get(name, [])
4242
elif match == "contains":
4343
matches = []
4444
for key, instances in cls._instance_lookup.items():
4545
if name in key:
4646
matches.extend(instances)
4747
else:
4848
raise ValueError("'match' must be either 'equals' or 'contains'")
49-
if all:
49+
if not matches:
50+
return None
51+
elif all:
5052
return matches
51-
elif len(matches) > 0:
52-
return matches[0]
5353
else:
54-
return None
54+
return matches[0]

pipeline/tests/test_regressions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,17 @@ def test_issue0069(om):
331331
results = om.core.License.by_name("Creative Commons", all=True, match="contains")
332332
assert len(results) == 7
333333
assert all("CC" in r.short_name for r in results)
334+
335+
@pytest.mark.parametrize("om", [openminds.latest])
336+
def test_pr0083(om):
337+
# https://github.com/openMetadataInitiative/openMINDS_Python/pull/83
338+
# by_name() should return None consistently
339+
# when no matches are found, regardless of the 'all' parameter
340+
341+
# all=False (default) should return None when no match is found
342+
result = om.controlled_terms.BiologicalOrder.by_name("nonexistent_order_xyz")
343+
assert result is None
344+
345+
# all=True should also return None when no match is found
346+
results = om.controlled_terms.BiologicalOrder.by_name("nonexistent_order_xyz", all=True)
347+
assert results is None

0 commit comments

Comments
 (0)