ipython / ipython/traitlets

Introduce @preprocess decorator?

Ouverte
#451 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Python
Étoiles
653
Forks
217
Merge moyen
2 j 21 h
PR mergées (30 j)
2

Description

As part of ipython/ipywidgets#1676, @SylvainCorlay and I were discussing a sort of validator of this sort:

```python
@validate('value)
def validate_value(self, proposal):
if isinstance(proposal, six.text_types):
self.format = 'url'
proposal = proposal.encode('utf-8')
return proposal
```

The problem with this is that `value` is a `Bytes` trait, and [`validate`](https://github.com/ipython/traitlets/blob/master/traitlets/traitlets.py#L589) calls the trait validator *before* the `@validate` decorator, so the only way to implement something like this is to declare the trait `Any` (even though in the message it should *always* be `Bytes`.

This *could* be solved by moving the user-specified validation up the stack, but to me this sort of seems like a separate action from validation it seems like it should go Input->Preprocessing->Validation->Observer.

That said, on the Python side, this *could* introduce a distinction between the types that can be *assigned to a trait* vs. the types that can be *emitted as a message* - I don't see a major problem here (it seems the `Unicode` type class already does this), but it might be worth introducing a mechanism for documenting which types are allowed as input to the preprocessor.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.