Skip to content

Commit ac1eae3

Browse files
authored
[SYNPY-1709] Correct boolean type conversion (#1284)
* fix: improve boolean string handling in annotation type conversion
1 parent a557de9 commit ac1eae3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

synapseclient/annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def raise_anno_type_error(anno_type: str):
101101
raise_anno_type_error,
102102
{
103103
"STRING": _identity,
104-
"BOOLEAN": lambda bool_str: bool_str == "true",
104+
"BOOLEAN": lambda bool_str: bool_str and bool_str.lower() == "true",
105105
"LONG": int,
106106
"DOUBLE": float,
107107
"TIMESTAMP_MS": lambda time_str: from_unix_epoch_time(int(time_str)),

tests/unit/synapseclient/unit_test_annotations.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,47 @@ def test_check_annotations_changed():
485485
}
486486
mock_new_annotations = {"boo": 456, "far": 789}
487487
assert check_annotations_changed(mock_bundle_annotations, mock_new_annotations)
488+
489+
490+
def test_from_synapse_annotations__boolean_mixed_case():
491+
"""Test that boolean values with random mixed cases are correctly parsed"""
492+
# Test various mixed case combinations of 'true' and 'false'
493+
raw_annotations = {
494+
"id": "syn123",
495+
"etag": "7bdb83e9-a50a-46e4-987a-4962559f090f",
496+
"annotations": {
497+
"bool1": {"type": "BOOLEAN", "value": ["True"]},
498+
"bool2": {"type": "BOOLEAN", "value": ["TRUE"]},
499+
"bool3": {"type": "BOOLEAN", "value": ["true"]},
500+
"bool4": {"type": "BOOLEAN", "value": ["TrUe"]},
501+
"bool5": {"type": "BOOLEAN", "value": ["False"]},
502+
"bool6": {"type": "BOOLEAN", "value": ["FALSE"]},
503+
"bool7": {"type": "BOOLEAN", "value": ["false"]},
504+
"bool8": {"type": "BOOLEAN", "value": ["FaLsE"]},
505+
"bool9": {
506+
"type": "BOOLEAN",
507+
"value": ["true", "FALSE", "TrUe", "false"],
508+
},
509+
},
510+
}
511+
512+
result = from_synapse_annotations(raw_annotations)
513+
514+
# All variations of 'true' should be parsed as True
515+
assert result["bool1"] == [True]
516+
assert result["bool2"] == [True]
517+
assert result["bool3"] == [True]
518+
assert result["bool4"] == [True]
519+
520+
# All variations of 'false' should be parsed as False
521+
assert result["bool5"] == [False]
522+
assert result["bool6"] == [False]
523+
assert result["bool7"] == [False]
524+
assert result["bool8"] == [False]
525+
526+
# Mixed list should be parsed correctly
527+
assert result["bool9"] == [True, False, True, False]
528+
529+
# Verify id and etag are preserved
530+
assert result.id == "syn123"
531+
assert result.etag == "7bdb83e9-a50a-46e4-987a-4962559f090f"

0 commit comments

Comments
 (0)