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

Conversation

mdickinson
Copy link
Member

@mdickinson mdickinson commented Sep 13, 2021

This PR fixes the default value for a trait of the form Union(List(Int), Str) so that appropriate validation is performed on the list.

Fixes the Union part of #1520.

Closes #1131.

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,)

@@ -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.

@mdickinson mdickinson merged commit c791f76 into main Sep 13, 2021
@mdickinson mdickinson deleted the fix/union-dynamic-default branch September 13, 2021 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static default value in Union not transformed for container
2 participants