From c94ed3f8b2492f40d134232a74eaf7e8b46983d3 Mon Sep 17 00:00:00 2001 From: devteamaegis Date: Thu, 11 Jun 2026 02:33:37 -0400 Subject: [PATCH] fix(feature): avoid IndexError when parsing a dtype with a trailing dot parse_nested_brackets crashed with 'IndexError: string index out of range' on inputs like 'bionty.' (e.g. via parse_dtype('cat[bionty.]')) because it indexed parts[1][0] without checking the segment was non-empty. Guard the access so malformed dtypes raise a clear ValidationError instead. --- lamindb/models/feature.py | 2 +- tests/core/test_feature_dtype.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lamindb/models/feature.py b/lamindb/models/feature.py index 8d7134494..9ca9b5942 100644 --- a/lamindb/models/feature.py +++ b/lamindb/models/feature.py @@ -297,7 +297,7 @@ def parse_nested_brackets(dtype_str: str) -> dict[str, Any]: # No brackets - handle simple cases like "A" or "A.field" if "." in dtype_str: parts = dtype_str.split(".") - if len(parts) == 2 and parts[1][0].isupper(): + if len(parts) == 2 and parts[1] != "" and parts[1][0].isupper(): # bionty.CellType return {"registry": dtype_str, "filter_str": "", "field": ""} elif len(parts) == 3: diff --git a/tests/core/test_feature_dtype.py b/tests/core/test_feature_dtype.py index ffaafd6bb..3a9511fbe 100644 --- a/tests/core/test_feature_dtype.py +++ b/tests/core/test_feature_dtype.py @@ -9,6 +9,7 @@ from lamindb.models.feature import ( parse_dtype, parse_filter_string, + parse_nested_brackets, resolve_relation_filters, serialize_dtype, ) @@ -123,6 +124,19 @@ def test_serialize_with_field_information(): # ----------------------------------------------------------------------------- +def test_parse_nested_brackets_trailing_dot(): + # a trailing dot used to raise `IndexError: string index out of range` + # because `parts[1][0]` was accessed on an empty second part + assert parse_nested_brackets("bionty.") == { + "registry": "bionty", + "filter_str": "", + "field": "", + } + # malformed dtype must surface as a clear ValidationError, not IndexError + with pytest.raises(ValidationError): + parse_dtype("cat[bionty.]") + + def test_simple_record_with_subtype_and_field(): # Create a Record type to get its UID customer_type = ln.Record(name="Customer", is_type=True).save()