-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
) | ||||||
|
@@ -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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition here is unnecessary: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Lines 3982 to 3983 in 385f42b
|
||||||
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. | ||||||
|
There was a problem hiding this comment.
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.