ipython / ipython/traitlets

Custom validator registration

オープン
#298 コメント 14 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
653
フォーク
217
平均マージ
2日 21時間
マージ済み PR(30日)
2

説明

I think that it would be interesting to push upstream the abiility that was added in jupyter-incubator's traittypes: registration of custom validators (non-cross) with a chaining `valid` method.

``` python
from traitlets import HasTraits, TraitError
from traittypes import Array

def shape(*dimensions):
def validator(trait, value):
if value.shape != dimensions:
raise TraitError('Expected an of shape %s and got and array with shape %s' % (dimensions, value.shape))
else:
return value
return validator

class Foo(HasTraits):
bar = Array(np.identity(2)).valid(shape(2, 2))
foo = Foo()

foo.bar = [1, 2] # Should raise a TraitError
```

In the case of the numpy array trait type, there are many possibilities that would be arguably natural but would be very likely bloating the TraitType specialization if implemented in the class.

Examples:
- only accepting attributes with a certain shape
- only accepting values that have less than a certain number of elements
- bounds on the dimensionality of the array
- requirements on dtypes (be a subdtype of a certain type, or exactly a certain dtype)
- squeezing dimensions en length 1, as we do in bqplot

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Review the custom-validator behavior in jupyter-incubator's traittypes and the issue's Array examples first. Done means agreeing on the registration and chaining API, defining its validation semantics, and covering the demonstrated shape failure and related array constraints with tests.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
numpy, python
領域
backend-api-design
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。