test_jstraitlets.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:schemapi 作者: altair-viz 项目源码 文件源码
def test_AnyOfObject():

    class Foo(jst.JSONHasTraits):
        intval = T.Integer()
        flag = T.Bool()

    class Bar(jst.JSONHasTraits):
        strval = T.Unicode()
        flag = T.Bool()

    class FooBar(jst.AnyOfObject):
        _classes = [Foo, Bar]

    FooBar(strval='hello', flag=True)
    FooBar(intval=5, flag=True)

    with pytest.raises(T.TraitError):
        FooBar(strval=666, flag=False)
    with pytest.raises(T.TraitError):
        FooBar(strval='hello', flag='bad arg')
    with pytest.raises(T.TraitError):
        FooBar(intval='bad arg', flag=False)
    with pytest.raises(T.TraitError):
        FooBar(intval=42, flag='bad arg')

    # Test from_dict
    FooBar.from_dict({'strval': 'hello', 'flag': True})
    FooBar.from_dict({'intval': 42, 'flag': False})
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号