Skip to content

Commit

Permalink
Fix bug in handling of Union default value (#1534)
Browse files Browse the repository at this point in the history
* Fix bug in handling of Union default value

* Fix style errors

* Fix spacing bug
  • Loading branch information
mdickinson authored Sep 20, 2021
1 parent 9f93433 commit df89def
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions traits/tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import unittest

from traits.api import (
Float, Instance, Int, List, Str, TraitError, TraitType, HasTraits, Union,
Type)
Bytes, DefaultValue, Float, HasTraits, Instance, Int, List, Str,
TraitError, TraitType, Type, Union)


class CustomClass(HasTraits):
Expand Down Expand Up @@ -195,10 +195,21 @@ def test_constant_default(self):
class HasUnionWithList(HasTraits):
foo = Union(Int(23), Float)

nested = Union(Union(Str(), Bytes()), Union(Int(), Float(), None))

has_union = HasUnionWithList()
value = has_union.foo
self.assertEqual(value, 23)

self.assertEqual(
has_union.trait("foo").default_value(),
(DefaultValue.constant, 23),
)
self.assertEqual(
has_union.trait("nested").default_value(),
(DefaultValue.constant, ""),
)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion traits/trait_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4028,7 +4028,7 @@ def __init__(self, *traits, **metadata):
if 'default_value' in metadata:
default_value = metadata.pop("default_value")
else:
first_default_value, first_default_value_type = (
first_default_value_type, first_default_value = (
self.list_ctrait_instances[0].default_value())

if first_default_value_type == DefaultValue.constant:
Expand Down

0 comments on commit df89def

Please sign in to comment.