Skip to content

casting traits inherit from mixin#299

Closed
rmorshea wants to merge 1 commit intoipython:masterfrom
rmorshea:casting_mixin
Closed

casting traits inherit from mixin#299
rmorshea wants to merge 1 commit intoipython:masterfrom
rmorshea:casting_mixin

Conversation

@rmorshea
Copy link
Copy Markdown
Contributor

Adds a generic CastingMixin class that all other casting TraitTypes inherit from (i.e. CInt, CFloat, etc.). Upon setting a value, these reconfigured traits first coerce the input value to the proper type, and then pass it to super().validate(...). This mixin also makes new casting traits like CDict and CList more sensible to implement, because the complex base class validation heuristics don't need to be hard coded in their casting versions anymore:

class A(HasTraits):
    cd = CDict()

a = A()
a.cd = [("a", 1)]
# prints: {'a': 1}
print(cd)

@rmorshea rmorshea force-pushed the casting_mixin branch 2 times, most recently from 59d1083 to 0d7a5f0 Compare August 31, 2016 23:01
@minrk minrk added this to the 5.0 milestone Sep 1, 2016
Comment thread traitlets/traitlets.py
cast_to = None

def validate(self, obj, value):
cast_types = getattr(self, '_cast_types', All)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be defined as the default on the mixin, rather than using getattr with a falllback

@rmorshea
Copy link
Copy Markdown
Contributor Author

Working on a more generic solution...

@rmorshea rmorshea closed this Sep 13, 2016
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.

2 participants