Introduce @preprocess decorator?
- 主要言語
- Python
- スター
- 653
- フォーク
- 217
- 平均マージ
- 2日 21時間
- マージ済み PR(30日)
- 2
説明
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.
コントリビューションガイド
評価
この issue はまだ評価されていません。