Introduce @preprocess decorator?
- Ngôn ngữ chính
- Python
- Star
- 653
- Fork
- 217
- Merge trung bình
- 2 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 2
Mô tả
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.
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.