ipython / ipython/traitlets

Introduce @preprocess decorator?

Offen
#451 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
653
Forks
217
Ø Merge
2 T. 21 Std.
Gemergte PRs (30 T.)
2

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.