Skip to content

Commit 2477feb

Browse files
committed
Fix union from string
1 parent 4a0a708 commit 2477feb

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

traitlets/tests/test_traitlets.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,17 @@ def test_tcp_from_string(s, expected):
31363136
_from_string_test(TCPAddress, s, expected)
31373137

31383138

3139+
@pytest.mark.parametrize(
3140+
"s, expected",
3141+
[
3142+
("[]", []),
3143+
('{}', '{}')
3144+
],
3145+
)
3146+
def test_union_of_list_and_unicode_from_string(s, expected):
3147+
_from_string_test(Union([List(), Unicode()]), s, expected)
3148+
3149+
31393150
def test_all_attribute():
31403151
"""Verify all trait types are added to `traitlets.__all__`"""
31413152
names = dir(traitlets)

traitlets/traitlets.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,16 @@ def __or__(self, other):
21792179
else:
21802180
return Union(self.trait_types + [other])
21812181

2182+
def from_string(self, s):
2183+
for trait_type in self.trait_types:
2184+
try:
2185+
v = trait_type.from_string(s)
2186+
return trait_type.validate(None, v)
2187+
except TraitError:
2188+
continue
2189+
self.error(None, s)
2190+
2191+
21822192

21832193
# -----------------------------------------------------------------------------
21842194
# Basic TraitTypes implementations/subclasses

0 commit comments

Comments
 (0)