Skip to content

Commit

Permalink
Fix assigning string value to Set (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
azjps authored Apr 19, 2024
1 parent 8203a01 commit 5cf34f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
18 changes: 18 additions & 0 deletions tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,24 @@ def coerce(self, value):
return value


class SetTrait(HasTraits):
value = Set(Unicode())


class TestSet(TraitTestBase):
obj = SetTrait()

_default_value: t.Set[str] = set()
_good_values = [{"a", "b"}, "ab"]
_bad_values = [1]

def coerce(self, value):
if isinstance(value, str):
# compatibility handling: convert string to set containing string
value = {value}
return value


class Foo:
pass

Expand Down
7 changes: 1 addition & 6 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3698,12 +3698,7 @@ def validate_elements(self, obj: t.Any, value: t.Any) -> t.Any:

def set(self, obj: t.Any, value: t.Any) -> None:
if isinstance(value, str):
return super().set(
obj,
set(
value,
),
)
return super().set(obj, {value})
else:
return super().set(obj, value)

Expand Down

0 comments on commit 5cf34f6

Please sign in to comment.