Skip to content

Default values are not cross-validated with @validation #287

@ankostis

Description

@ankostis

While the defaults values are validated against the type of their trait, they are not cross-validated against any @validate rules.

This may lead to conflicting behavior in HasTraits subclasses when they contain invalid defaults, as exemplified in #263, and copied here for clarity:

class A1(HasTraits):
   virtue = Unicode('BAD')

   @validate('virtue')
   def _no_evil(self, proposal):
       if proposal['value'] == 'BAD':
           raise TraitError()
       return proposal['value']

class A2(HasTraits):
   virtue = Unicode('GOOD')

   @validate('virtue')
   def _no_evil(self, proposal):
       if proposal['value'] == 'BAD':
           raise TraitError()

>>> A1('virtue='BAD')  ## Pass
>>> A2('virtue='BAD')  ## Screams!
traitlets.traitlets.TraitError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions