Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix union from string #773

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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