示例#1
0
 def test_error_pos_is_correctly_passed_to_warning(
         self, src_pos: 'Optional[Pos]') -> None:
     for op in ['truediv', 'floordiv', 'mod']:
         TypeCheckLogger.clean_sing()
         getattr(pv.int(1), op)(pv.int(0), src_pos)
         assert len(TypeCheckLogger().warnings) == 1
         assert TypeCheckLogger().warnings[-1][2] == src_pos
示例#2
0
    def test_shifts_with_some_values(self, i: PythonValue,
                                     j: Optional[int]) -> None:
        TypeCheckLogger.clean_sing()

        new_val1 = i.lshift(pv.int(j))
        new_val2 = i.rshift(pv.int(j))

        assert not i.is_top()
        assert isinstance(i.val, AbstractValue)
        assert isinstance(new_val1.val, AbstractValue)
        assert isinstance(new_val2.val, AbstractValue)

        if i.val.is_top() or j is None:
            assert new_val1.val.is_top()
            assert new_val2.val.is_top()
            assert len(TypeCheckLogger().warnings) == 0
            return

        if new_val1.val.is_top():
            with raises(ValueError):
                ops.lshift(i.val.val, j)  # type: ignore
            assert len(TypeCheckLogger().warnings) > 0
            assert valueError.match(TypeCheckLogger().warnings[0][1])
        else:
            assert ops.lshift(i.val.val, j) == new_val1.val.val  # type: ignore

        if new_val2.val.is_top():
            with raises(ValueError):
                ops.rshift(i.val.val, j)  # type: ignore
            assert len(TypeCheckLogger().warnings) > 0
            assert valueError.match(TypeCheckLogger().warnings[-1][1])
        else:
            assert ops.rshift(i.val.val, j) == new_val2.val.val  # type: ignore
示例#3
0
 def test_none_affects_everything(self, i: Optional[int],
                                  j: Optional[float]) -> None:
     for op in ['add', 'sub', 'mul']:
         res = getattr(pv.int(i), op)(pv.float(j)).val.is_top()
         res2 = getattr(pv.float(j), op)(pv.int(i)).val.is_top()
         is_i_or_j_none = (i is None) or (j is None)
         assert (is_i_or_j_none == res == res2)
示例#4
0
    def test_float_and_ints_comform_to_baseline_python_divs(
            self, i: int, j: float) -> None:
        for op in ['truediv', 'floordiv', 'mod']:
            TypeCheckLogger.clean_sing()

            if j == 0.0:
                with raises(ZeroDivisionError):
                    getattr(ops, op)(i, j)
                newval = getattr(pv.int(i), op)(pv.float(j))
                assert isinstance(newval.val, (Int, Float))
                assert newval.val.is_top()

                assert len(TypeCheckLogger().warnings) == 1
                assert zeroDivisionError.match(
                    TypeCheckLogger().warnings[0][1])
            else:
                assert check_float_equality(
                    getattr(ops, op)(i, j),
                    getattr(pv.int(i), op)(pv.float(j)).val.val)
                assert len(TypeCheckLogger().warnings) == 0

            if i == 0:
                with raises(ZeroDivisionError):
                    getattr(ops, op)(j, i)
                newval = getattr(pv.float(j), op)(pv.int(i))
                assert isinstance(newval.val, (Int, Float))
                assert newval.val.is_top()

                assert len(TypeCheckLogger().warnings) > 0
                assert zeroDivisionError.match(
                    TypeCheckLogger().warnings[-1][1])
            else:
                assert check_float_equality(
                    getattr(ops, op)(j, i),
                    getattr(pv.float(j), op)(pv.int(i)).val.val)
示例#5
0
 def test_float_and_ints_comform_to_baseline_python(self, i: int,
                                                    j: float) -> None:
     for op in ['add', 'sub', 'mul']:
         op_fun = getattr(ops, op)
         assert check_float_equality(
             op_fun(i, j),
             getattr(pv.int(i), op)(pv.float(j)).val.val)
         assert check_float_equality(
             op_fun(j, i),
             getattr(pv.float(j), op)(pv.int(i)).val.val)
示例#6
0
 def test_op_int(self, i: int, j: int) -> None:
     """
     This test basically checks that doing something like this:
     add(Int(3), Int(5)) == Int(8)
     for operations which image is on the same initial value
     """
     for op in ['add', 'sub', 'mul']:
         pt_val = getattr(pv.int(i), op)(pv.int(j))
         py_val = getattr(ops, op)(i, j)
         assert py_val == pt_val.val.val
         assert type(py_val) is type(pt_val.val.val)  # noqa: E721
示例#7
0
    def test_div_int(self, i: int, j: int) -> None:
        """
        Checks correctness of division between integers.
        int(3).truediv(int(5)) == int(3/5)
        for division operations
        """
        for op in ['truediv', 'floordiv', 'mod']:
            TypeCheckLogger.clean_sing()

            pt_val = getattr(pv.int(i), op)(pv.int(j))
            if pt_val.is_top():
                with raises(ZeroDivisionError):
                    getattr(ops, op)(i, j)
                assert len(TypeCheckLogger().warnings) == 1
                assert zeroDivisionError.match(
                    TypeCheckLogger().warnings[0][1])
            else:
                assert isinstance(pt_val.val, (Float, Int))
                py_val = getattr(ops, op)(i, j)
                assert py_val == pt_val.val.val
                assert type(py_val) is type(pt_val.val.val)  # noqa: E721
                assert len(TypeCheckLogger().warnings) == 0
示例#8
0
    def test_running_simple_loop(self, num: int) -> None:
        """
        This test simulates the transformation of the following piece of code:

        > i = 0
        > b = 0
        > while i > num:
        >    b += i
        >    i += 1
        """

        st = pt.Store()

        st['num'] = pv.int(num)
        st['i'] = pv.int(0)
        st['b'] = pv.int(0)

        def while_qst(st: Store) -> PythonValue:
            return st['i'].lt(st['num'])  # type: ignore

        def while_(st: Store) -> Store:
            st['b'] = st['b'].add(st['i'])
            st['i'] = st['i'].add(pv.int(1))
            return st

        st = pt.runWhile(st, while_qst, while_)

        assert isinstance(st['i'].val, Int)
        assert isinstance(st['b'].val, Int)

        if num < execute.MAX_LOOPS:
            assert isinstance(st['i'].val.val, int)
            assert st['i'].val.val == num
            assert isinstance(st['b'].val.val, int)
            assert st['b'].val.val == num * (num - 1) // 2
        else:
            assert st['i'].val.is_top()
            assert st['b'].val.is_top()
示例#9
0
    def _check_tuple_all_ints(self, shape: Tuple,
                              pos: 'Optional[Pos]') -> Tuple:
        shape = shape.copy_mut({shape.mut_id: PythonValue(PT.InConstruction)})
        for k in shape.children:
            if isinstance(k, tuple) and k[0] == 'index':
                value = shape.children[k]

                if value.is_top():
                    shape.children[k] = PythonValue(Int.top())
                elif not isinstance(value.val, Int):
                    assert isinstance(value.val, AbstractValue)
                    TypeCheckLogger().new_warning(
                        "E017",
                        f"TypeError: '{value.val.type_name}' object cannot"
                        " be interpreted as an integer", pos)
                    shape.children[k] = pv.int()
        return shape
示例#10
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1

store = {
    'np': numpy_module,
    'c': pv.Top,
    '_': pv.Top,
    'a': PythonValue(NdArray(pv.tuple(pv.int(2), pv.int()))),
    'nonexistent': PythonValue(BuiltinModule()),
    'b': PythonValue(Tuple((pv.int(2), pv.int()), size=(2, 2))),
}
示例#11
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 0

store = {
    'np': numpy_module,
    'arr': PythonValue(NdArray(pv.tuple(pv.int(2), pv.int(4)))),
}
示例#12
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values import PythonValue as PV

exitcode = 0

store = {
    'either': pv.Top,
    'a': pv.list([]),
    'b': pv.int(),
    'c': pv.list([pv.int(2), pv.int(3)]),
}
示例#13
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1

store = {
    'np': numpy_module,
    'a': PythonValue(NdArray(pv.tuple(pv.int(12)))),
}
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1

store = {
    'np': numpy_module,
    'nonexistent': PythonValue(BuiltinModule()),
    'a': PythonValue(NdArray(pv.tuple(pv.int(2), pv.int()))),
    'b': pv.Top,
    'c': pv.Top,
    'd': PythonValue(NdArray(pv.tuple())),
    'e': pv.Top,
    'f': PythonValue(NdArray(pv.tuple(pv.int(2), pv.int()))),
}
示例#15
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1


def ndarray_(*lst):
    return PythonValue(NdArray(pv.tuple(*(pv.int(i) for i in lst))))


def list_int(*lst):
    return pv.list([pv.int(i) for i in lst])


store = {
    'np': numpy_module,
    'lst': pv.list([list_int(1, 3, 4), list_int(2, 8, 9)]),
    'a': pv.int(6),
    'a_': pv.int(6),
    'b': pv.tuple(pv.int(2), pv.int(3)),
    'b_': pv.tuple(pv.int(2), pv.int(3)),
    'c': pv.int(2),
    'c_': pv.int(2),
    'd': pv.Top,
    'd_': pv.Top,
}
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 0


def ndarray_(*lst):
    return PythonValue(NdArray(pv.tuple(*(pv.int(i) for i in lst))))


store = {
    'np': numpy_module,
    'Top': pv.Top,
    'NdArray': PythonValue(NdArrayAnnotation()),
    'a': pv.int(160),
    'b': pv.int(),
    'c': ndarray_(2),
}
示例#17
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1

store = {'_': pv.Top, 'a': pv.list([pv.int(2)])}
示例#18
0
def list_int(*lst):
    return pv.list([pv.int(i) for i in lst])
def to_pv_Tuple(*ints):
    return pv.tuple(*(pv.int(i) for i in ints))
示例#20
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values import PythonValue as PV

exitcode = 0

a = pv.list([None, pv.int(2)])
a.val.children['index', 0] = a

store = {
    'either': pv.int(),
    'a': a,
}
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1

store = {
    'np':
    numpy_module,
    'm':
    pv.Top,
    'n':
    pv.Top,
    '_':
    pv.tuple(),
    '__':
    pv.tuple(pv.int(1)),
    'a':
    pv.tuple(pv.int(2)),
    'b_':
    PythonValue(NdArray(pv.tuple(pv.int(2), pv.int(3)))),
    'b':
    pv.tuple(pv.int(2), pv.int(3)),
    'c':
    pv.tuple(pv.int(2)),
    'd':
    pv.tuple(pv.int(2), pv.int(3)),
    # TODO(helq): fix np.array, 'e' should be the commented tuple below
    # 'e': PythonValue(Tuple([pv.int(2)], size=(1,3))),  # THIS IS THE RIGHT ANSWER
    'e':
    PythonValue(Tuple([pv.int(2)], size=(1, inf))),
    'f':
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT

exitcode = 1

r = List([pv.int(21)], size=(1, 1))

store = {
  '_': PythonValue(PT.Top),
  'f': r.get_attrs()['append'],
  'r': PythonValue(r),
}
示例#23
0
def ndarray_(*lst):
    return PythonValue(NdArray(pv.tuple(*(pv.int(i) for i in lst))))
示例#24
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 0

store = {
    'array': PythonValue(array),
    'zeros': PythonValue(zeros),
    'x': PythonValue(NdArray(pv.tuple(pv.int(3)))),
    'y': PythonValue(NdArray(pv.tuple(pv.int(2), pv.int(3), pv.int(4)))),
    'xn': pv.int(1),
    'yn': pv.int(3),
}
示例#25
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT

exitcode = 1

b_ = pv.list([pv.float(5.0), pv.none()])
a = pv.list([pv.int(9), b_])
b_.val.children['index', 1] = a

store = {
    'a': a,
    'b': a.val.get_attrs()['append'],
    'c': b_.val.get_attrs()['append'],
}
示例#26
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 0


def ndarray_(*lst):
    return PythonValue(NdArray(pv.tuple(*(pv.int(i) for i in lst))))


store = {
    'np': numpy_module,
    'i': pv.int(10),
    'a': ndarray_(10, 4),
    'b': ndarray_(10, None),
}
示例#27
0
 def while_(st: Store) -> Store:
     st['b'] = st['b'].add(st['i'])
     st['i'] = st['i'].add(pv.int(1))
     return st
示例#28
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT
from pytropos.libs_checking.numpy import *

exitcode = 1


def ndarray_(*lst):
    return PythonValue(NdArray(pv.tuple(*(pv.int(i) for i in lst))))


store = {
    'np': numpy_module,
    'a': ndarray_(10, 6),
    'm': pv.int(4),
    'n': pv.int(2),
    'b': ndarray_(3, 11),
    'res': PythonValue(NdArray.top()),
    'var': pv.bool(True),
}
示例#29
0
import pytropos.internals.values as pv
from pytropos.internals.values.builtin_values import *
from pytropos.internals.values.python_values.builtin_mutvalues import *
from pytropos.internals.values.python_values.wrappers import *
from pytropos.internals.values.python_values.python_values import PythonValue, PT

exitcode = 1

a = pv.list([pv.int(2), pv.int(12)])

store = {
    'a': a,
    '_': pv.Top,
    'b': a.val.get_attrs()['append'],
}
示例#30
0
import pytropos.internals.values as pv
from pytropos.internals.values.python_values import PythonValue as PV

exitcode = 1

store = {
    'a': PV.top(),
    'l': pv.list([PV.top(), pv.int(21), PV.top()]),
    'n': PV.top(),
    'm': PV.top(),
}