Skip to content

Commit 367ac77

Browse files
authored
Merge pull request #773 from krassowski/union-from-string
2 parents 4a0a708 + 9493030 commit 367ac77

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

traitlets/tests/test_traitlets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,6 +3136,14 @@ 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+
def test_union_of_list_and_unicode_from_string(s, expected):
3144+
_from_string_test(Union([List(), Unicode()]), s, expected)
3145+
3146+
31393147
def test_all_attribute():
31403148
"""Verify all trait types are added to `traitlets.__all__`"""
31413149
names = dir(traitlets)

traitlets/traitlets.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,15 @@ 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+
21822191

21832192
# -----------------------------------------------------------------------------
21842193
# Basic TraitTypes implementations/subclasses

0 commit comments

Comments
 (0)