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 dynamic default values for Union traits #1522

Merged
merged 1 commit into from
Sep 13, 2021
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
26 changes: 23 additions & 3 deletions traits/tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

import unittest

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


class CustomClass(HasTraits):
Expand All @@ -27,7 +28,7 @@ def validate(self, obj, name, value):
self.error(obj, name, value)


class TestCaseEnumTrait(unittest.TestCase):
class TestUnion(unittest.TestCase):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by fix of long-standing copypasta.


def test_union_incompatible_trait(self):
with self.assertRaises(ValueError) as exception_context:
Expand Down Expand Up @@ -179,6 +180,25 @@ class TestClass(HasTraits):

TestClass(s="sdf")

def test_list_inside_union_default(self):
class HasUnionWithList(HasTraits):
foo = Union(List(Int), Str)

has_union = HasUnionWithList()
value = has_union.foo
self.assertIsInstance(value, list)
with self.assertRaises(TraitError):
value.append("not an integer")

def test_constant_default(self):
# Exercise the branch where the default is constant.
class HasUnionWithList(HasTraits):
foo = Union(Int(23), Float)

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


if __name__ == '__main__':
unittest.main()
19 changes: 14 additions & 5 deletions traits/trait_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from .trait_list_object import TraitListEvent, TraitListObject
from .trait_set_object import TraitSetEvent, TraitSetObject
from .trait_type import (
_infer_default_value_type,
NoDefaultSpecified,
TraitType,
)
Expand Down Expand Up @@ -4001,15 +4000,25 @@ def __init__(self, *traits, **metadata):
"'default'."
)

default_value = None
if 'default_value' in metadata:
default_value = metadata.pop("default_value")
elif self.list_ctrait_instances:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition here is unnecessary: list_ctrait_instances is always nonempty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to be clear, this is always nonempty because we can't/don't use trait = Union(). right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union() is legal, but it's equivalent to Union(None). That doesn't feel quite right to me (I think I'd prefer an exception), but fixing it is outside the scope of this issue. Code ref:

traits/traits/trait_types.py

Lines 3982 to 3983 in 385f42b

if not traits:
traits = (_NoneTrait,)

default_value = self.list_ctrait_instances[0].default
else:
first_default_value, first_default_value_type = (
self.list_ctrait_instances[0].default_value())

if first_default_value_type == DefaultValue.constant:
self.default_value_type = DefaultValue.constant
default_value = first_default_value
else:
self.default_value_type = DefaultValue.callable
default_value = self._get_default_value

self.default_value_type = _infer_default_value_type(default_value)
super().__init__(default_value, **metadata)

def _get_default_value(self, object):
return self.list_ctrait_instances[0].default_value_for(
object, "<inner_trait>")

def validate(self, obj, name, value):
""" Return the value by the first trait in the list that can
validate the assigned value, raise an error if none of them can.
Expand Down