Skip to content

Commit 5cf34f6

Browse files
authored
Fix assigning string value to Set (#903)
1 parent 8203a01 commit 5cf34f6

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

tests/test_traitlets.py

+18
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,24 @@ def coerce(self, value):
16581658
return value
16591659

16601660

1661+
class SetTrait(HasTraits):
1662+
value = Set(Unicode())
1663+
1664+
1665+
class TestSet(TraitTestBase):
1666+
obj = SetTrait()
1667+
1668+
_default_value: t.Set[str] = set()
1669+
_good_values = [{"a", "b"}, "ab"]
1670+
_bad_values = [1]
1671+
1672+
def coerce(self, value):
1673+
if isinstance(value, str):
1674+
# compatibility handling: convert string to set containing string
1675+
value = {value}
1676+
return value
1677+
1678+
16611679
class Foo:
16621680
pass
16631681

traitlets/traitlets.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -3698,12 +3698,7 @@ def validate_elements(self, obj: t.Any, value: t.Any) -> t.Any:
36983698

36993699
def set(self, obj: t.Any, value: t.Any) -> None:
37003700
if isinstance(value, str):
3701-
return super().set(
3702-
obj,
3703-
set(
3704-
value,
3705-
),
3706-
)
3701+
return super().set(obj, {value})
37073702
else:
37083703
return super().set(obj, value)
37093704

0 commit comments

Comments
 (0)