Exemple #1
0
def test_abstract_clone():
    s1 = S(t=ty.Int[32])
    s2 = S(t=ty.Int[64])
    assert upcast(s1, 64) is s2

    a1 = T([s1, L(s1)])
    assert upcast(a1, 64) is T([s2, L(s2)])
Exemple #2
0
def test_amerge():
    a = T([S(1), S(t=ty.Int[64])])
    b = T([S(1), S(t=ty.Int[64])])
    c = T([S(t=ty.Int[64]), S(t=ty.Int[64])])

    assert amerge(a, b, loop=None, forced=False) is a
    assert amerge(a, c, loop=None, forced=False) == c
    assert amerge(c, a, loop=None, forced=False) is c

    with pytest.raises(MyiaTypeError):
        amerge(a, c, loop=None, forced=True)

    assert amerge(1, 2, loop=None, forced=False) is ANYTHING
    with pytest.raises(MyiaTypeError):
        assert amerge(1, 2, loop=None, forced=True)

    with pytest.raises(MyiaTypeError):
        assert amerge("hello", "world", loop=None, forced=False)

    assert amerge(ty.Int, ty.Int[64], loop=None, forced=False) is ty.Int
    assert amerge(ty.Int[64], ty.Int, loop=None, forced=False) is ty.Int
    with pytest.raises(MyiaTypeError):
        amerge(ty.Float, ty.Int, loop=None, forced=False)
    with pytest.raises(MyiaTypeError):
        amerge(ty.Int[64], ty.Int, loop=None, forced=True)

    loop = asyncio.new_event_loop()
    p = PendingFromList([ty.Int[64], ty.Float[64]], None, None, loop=loop)
    assert amerge(ty.Number, p, loop=None, forced=False, bind_pending=False) \
        is ty.Number
    assert amerge(p, ty.Number, loop=None, forced=False, bind_pending=False) \
        is ty.Number
    with pytest.raises(MyiaTypeError):
        print(amerge(p, ty.Number, loop=None, forced=True, bind_pending=False))
Exemple #3
0
def test_abstract_clone():
    s1 = S(t=ty.Int[32])
    s2 = S(t=ty.Int[64])
    assert upcast(s1, 64) is s2

    a1 = T([s1, AbstractClass(object, {'field': s1}, {})])
    a2 = T([s2, AbstractClass(object, {'field': s2}, {})])
    assert upcast(a1, 64) is a2
Exemple #4
0
def test_abstract_clone():
    s1 = S(t=ty.Int[32])
    s2 = S(t=ty.Int[64])
    assert upcast(s1, 64) is s2

    a1 = T([s1, AbstractClass(object, {"field": s1})])
    a2 = T([s2, AbstractClass(object, {"field": s2})])
    assert upcast(a1, 64) is a2

    jt = TransformedFunction(AbstractFunctionUnique((s1, ), s1), P.J)
    assert upcast(jt, 64).fn.args == [s2]
    assert upcast(jt, 64).fn.output is s2
Exemple #5
0
    async def coro():
        s1 = S(t=ty.Int[32])
        s2 = S(t=ty.Int[64])
        assert (await upcast_async(s1)) is s2

        a1 = T([s1, L(s1)])
        assert (await upcast_async(a1)) is T([s2, L(s2)])

        f1 = AbstractFunction(P.scalar_add, P.scalar_mul)
        assert (await upcast_async(f1)) is f1

        u1 = AbstractUnion([s1])
        assert (await upcast_async(u1)) is AbstractUnion([s2])
Exemple #6
0
def test_amerge():
    a = T([S(1), S(t=ty.Int[64])])
    b = T([S(1), S(t=ty.Int[64])])
    c = T([S(t=ty.Int[64]), S(t=ty.Int[64])])

    assert amerge(a, b, forced=False) is a
    assert amerge(a, c, forced=False) == c
    assert amerge(c, a, forced=False) is c

    with pytest.raises(MyiaTypeError):
        amerge(a, c, forced=True)

    assert amerge(1, 2, forced=False) is ANYTHING
    with pytest.raises(MyiaTypeError):
        assert amerge(1, 2, forced=True)

    with pytest.raises(MyiaTypeError):
        assert amerge("hello", "world", forced=False)

    assert amerge(ty.Int, ty.Int[64], forced=False) is ty.Int
    assert amerge(ty.Int[64], ty.Int, forced=False) is ty.Int
    with pytest.raises(MyiaTypeError):
        amerge(ty.Float, ty.Int, forced=False)
    with pytest.raises(MyiaTypeError):
        amerge(ty.Int[64], ty.Int, forced=True)

    loop = asyncio.new_event_loop()
    p = PendingFromList([ty.Int[64], ty.Float[64]], None, None, loop=loop)
    assert amerge(ty.Number, p, forced=False, bind_pending=False) \
        is ty.Number
    assert amerge(p, ty.Number, forced=False, bind_pending=False) \
        is ty.Number
    with pytest.raises(MyiaTypeError):
        print(amerge(p, ty.Number, forced=True, bind_pending=False))

    assert amerge(AbstractError(DEAD),
                  AbstractError(ANYTHING),
                  forced=False) is AbstractError(ANYTHING)

    d1 = {'x': 1}
    d2 = {'y': 2}
    with pytest.raises(MyiaTypeError):
        print(amerge(d1, d2))

    td1 = TrackDict({ALIASID: 1})
    td2 = TrackDict({})
    with pytest.raises(MyiaTypeError):
        print(amerge(td1, td2, forced=True))
Exemple #7
0
def test_type_to_abstract():
    assert type_to_abstract(int) is S(t=ty.Int[64])
    assert type_to_abstract(float) is S(t=ty.Float[64])
    assert type_to_abstract(bool) is S(t=ty.Bool)
    assert (type_to_abstract(typing.List) is U(type_to_abstract(Empty),
                                               type_to_abstract(Cons)))
    assert type_to_abstract(typing.Tuple) is T(ANYTHING)
Exemple #8
0
def test_merge():
    a = T([S(1), S(t=ty.Int[64])])
    b = T([S(1), S(t=ty.Int[64])])
    c = T([S(t=ty.Int[64]), S(t=ty.Int[64])])

    assert amerge(a, b, loop=None, forced=False) is a
    assert amerge(a, c, loop=None, forced=False) == c
    assert amerge(c, a, loop=None, forced=False) is c

    with pytest.raises(MyiaTypeError):
        amerge(a, c, loop=None, forced=True)

    assert amerge(1, 2, loop=None, forced=False) is ANYTHING
    with pytest.raises(MyiaTypeError):
        assert amerge(1, 2, loop=None, forced=True)

    with pytest.raises(MyiaTypeError):
        assert amerge("hello", "world", loop=None, forced=False)
Exemple #9
0
def test_merge_from_types():
    a = T([S(1), S(t=ty.Int[64])])

    t1 = type_to_abstract(typing.Tuple)
    t2 = type_to_abstract(typing.Tuple[ty.Int[64], ty.Int[64]])
    t3 = type_to_abstract(typing.Tuple[ty.Int[64]])
    assert amerge(t1, a, forced=True) is t1
    assert amerge(t2, a, forced=True) is t2
    with pytest.raises(MyiaTypeError):
        amerge(t3, a, forced=True)
Exemple #10
0
def test_amerge():
    a = T([S(1), S(t=ty.Int[64])])
    b = T([S(1), S(t=ty.Int[64])])
    c = T([S(t=ty.Int[64]), S(t=ty.Int[64])])

    assert amerge(a, b, forced=False) is a
    assert amerge(a, c, forced=False) == c
    assert amerge(c, a, forced=False) is c

    with pytest.raises(MyiaTypeError):
        amerge(a, c, forced=True)

    assert amerge(1, 2, forced=False) is ANYTHING
    with pytest.raises(MyiaTypeError):
        assert amerge(1, 2, forced=True)

    with pytest.raises(MyiaTypeError):
        assert amerge("hello", "world", forced=False)

    assert amerge(ty.Int, ty.Int[64], forced=False) is ty.Int
    assert amerge(ty.Int[64], ty.Int, forced=False) is ty.Int
    with pytest.raises(MyiaTypeError):
        amerge(ty.Float, ty.Int, forced=False)
    with pytest.raises(MyiaTypeError):
        amerge(ty.Int[64], ty.Int, forced=True)

    assert amerge(
        AbstractError(DEAD), AbstractError(ANYTHING), forced=False
    ) is AbstractError(ANYTHING)

    assert amerge(
        AbstractError(ANYTHING), AbstractError(DEAD), forced=True
    ) is AbstractError(ANYTHING)

    d1 = {"x": 1}
    d2 = {"y": 2}
    with pytest.raises(MyiaTypeError):
        print(amerge(d1, d2))

    td1 = TrackDict({ALIASID: 1})
    td2 = TrackDict({})
    with pytest.raises(MyiaTypeError):
        print(amerge(td1, td2, forced=True))
Exemple #11
0
def test_concretize_recursive():
    loop = asyncio.new_event_loop()
    s = S(t=ty.Int[64])
    p = Pending(None, None, loop=loop)
    t = T([s, p])
    p.set_result(t)

    sa = S(t=ty.Int[64])
    ta = T.empty()
    ta.__init__([sa, ta])
    ta = ta.intern()

    async def coro():
        assert (await concretize_abstract(t)) is ta

    asyncio.run(coro())
Exemple #12
0
def test_build_value():
    assert build_value(S(1)) == 1
    with pytest.raises(ValueError):
        build_value(S(t=ty.Int[64]))
    assert build_value(S(t=ty.Int[64]), default=ANYTHING) is ANYTHING

    assert build_value(T([S(1), S(2)])) == (1, 2)

    loop = InferenceLoop(errtype=Exception)
    p = loop.create_pending(resolve=(lambda: None), priority=(lambda: None))
    with pytest.raises(ValueError):
        assert build_value(S(p, t=ty.Int[64])) is p
    assert build_value(S(p, t=ty.Int[64]), default=ANYTHING) is ANYTHING
    p.set_result(1234)
    assert build_value(S(p, t=ty.Int[64])) == 1234

    pt = Point(1, 2)
    assert build_value(to_abstract_test(pt)) == pt
Exemple #13
0
def test_type_to_abstract():
    assert type_to_abstract(bool) is S(t=ty.Bool)
    assert type_to_abstract(typing.List) is L(ANYTHING)
    assert type_to_abstract(typing.Tuple) is T(ANYTHING)
Exemple #14
0
def test_abstract_clone_edge_cases():
    tup = T(ANYTHING)
    assert abstract_check(tup)
    tup2 = abstract_clone(tup)
    assert tup is tup2