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

Extendable TraitType Tags and Validators #336

Closed
wants to merge 2 commits into from

Conversation

rmorshea
Copy link
Contributor

Depends on #335

Extend chained validators:

def less_than(n):
    def validator(trait, value):
        if value < n:
            return value
        else:
            raise TraitError("%s is not less than %s" % (value, n))
    return validator

def greater_than(n):
    def validator(trait, value):
        if value > n:
            return value
        else:
            raise TraitError("%s is not greater than %s" % (value, n))
    return validator

class A(HasTraits):
    i = Int(5).allows(less_than(10))

class B(A):
    i = Int(5, extends='allows').allows(greater_than(0))

a = A()
a.i = -5 # pass
a.i = 15 # raise

b = B()
b.i = -5 # raise
b.i = 15 # raise

Extend trait metadata:

class A(HasTraits):
    i = Int().tag(x=1)
class B(A):
    i = Int(extends='tags').tag(y=2)

B.i.metadata # {'x':1, 'y':2}

@rmorshea
Copy link
Contributor Author

superceded by #429

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.

1 participant