Skip to content

Commit

Permalink
Merge pull request #773 from krassowski/union-from-string
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Sep 9, 2022
2 parents 4a0a708 + 9493030 commit 367ac77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,14 @@ def test_tcp_from_string(s, expected):
_from_string_test(TCPAddress, s, expected)


@pytest.mark.parametrize(
"s, expected",
[("[]", []), ("{}", "{}")],
)
def test_union_of_list_and_unicode_from_string(s, expected):
_from_string_test(Union([List(), Unicode()]), s, expected)


def test_all_attribute():
"""Verify all trait types are added to `traitlets.__all__`"""
names = dir(traitlets)
Expand Down
9 changes: 9 additions & 0 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,15 @@ def __or__(self, other):
else:
return Union(self.trait_types + [other])

def from_string(self, s):
for trait_type in self.trait_types:
try:
v = trait_type.from_string(s)
return trait_type.validate(None, v)
except TraitError:
continue
self.error(None, s)


# -----------------------------------------------------------------------------
# Basic TraitTypes implementations/subclasses
Expand Down

0 comments on commit 367ac77

Please sign in to comment.