ipython / ipython/traitlets

Introduce @preprocess decorator?

Open
#451 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
653
Forks
217
Avg merge
2d 21h
Merged PRs (30d)
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.