ipython / ipython/traitlets

Custom validator registration

Abierto
#298 14 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Python
Estrellas
653
Forks
217
Merge medio
2 d 21 h
PR fusionados (30 d)
2

Descripción

I think that it would be interesting to push upstream the abiility that was added in jupyter-incubator's traittypes: registration of custom validators (non-cross) with a chaining `valid` method.

``` python
from traitlets import HasTraits, TraitError
from traittypes import Array

def shape(*dimensions):
def validator(trait, value):
if value.shape != dimensions:
raise TraitError('Expected an of shape %s and got and array with shape %s' % (dimensions, value.shape))
else:
return value
return validator

class Foo(HasTraits):
bar = Array(np.identity(2)).valid(shape(2, 2))
foo = Foo()

foo.bar = [1, 2] # Should raise a TraitError
```

In the case of the numpy array trait type, there are many possibilities that would be arguably natural but would be very likely bloating the TraitType specialization if implemented in the class.

Examples:
- only accepting attributes with a certain shape
- only accepting values that have less than a certain number of elements
- bounds on the dimensionality of the array
- requirements on dtypes (be a subdtype of a certain type, or exactly a certain dtype)
- squeezing dimensions en length 1, as we do in bqplot

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.