Esempio n. 1
0
import astroid
import nose
from hypothesis import given, settings
from typing import List
import tests.custom_hypothesis_support as cs
settings.load_profile("pyta")


@given(cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1), cs.index_node()))
def test_inference_list_subscript(node):
    """Test whether visitor properly set the type constraint of Subscript node representing list-index access."""
    module, _ = cs._parse_text(node)
    for subscript_node in module.nodes_of_class(astroid.Subscript):
        list_node = subscript_node.value
        assert subscript_node.type_constraints.type == list_node.elts[0].type_constraints.type


@given(cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1), cs.slice_node()))
def test_subscript_homogeneous_list_slice(node):
    """Test visitor of Subscript node representing slicing of homogeneous list."""
    module, _ = cs._parse_text(node)
    for subscript_node in module.nodes_of_class(astroid.Subscript):
        list_node = subscript_node.value
        assert subscript_node.type_constraints.type == List[list_node.elts[0].type_constraints.type]


# TODO: this test currently fails
# @given(cs.simple_homogeneous_dict_node(min_size=1))
# def test_inference_dict_subscript(node):
#     """Note that this test only takes in a dictionary because the subscript index
#     must be the same type as the dictionary's keys in order to type check.
Esempio n. 2
0
import astroid
import nose
from hypothesis import assume, given, settings, HealthCheck
import tests.custom_hypothesis_support as cs
from typing import Any, List
settings.load_profile("pyta")


@given(cs.simple_homogeneous_list_node(min_size=1))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_homogeneous_lists(lst):
    """Test List nodes representing a list of values of the same primitive type."""
    module, _ = cs._parse_text(lst)
    list_node = list(module.nodes_of_class(astroid.List))[0]
    if len(list_node.elts) == 0:
        assert list_node.inf_type.getValue() == List[Any]
    else:
        cs._verify_type_setting(module, astroid.List, List[type(lst.elts[0].value)])


@given(cs.list_node(min_size=2))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_random_lists(lst):
    """Test List nodes representing a list of values of different primitive types."""
    assume(not isinstance(lst.elts[0].value, type(lst.elts[1].value)))
    assume(not isinstance(lst.elts[1].value, type(lst.elts[0].value)))
    val_types = [type(val.value) for val in lst.elts]
    if int in val_types:
        assume(bool not in val_types)
    if bool in val_types:
        assume(int not in val_types)
Esempio n. 3
0
import astroid
import nose
from nose import SkipTest
from hypothesis import given, settings, HealthCheck
from typing import List
import tests.custom_hypothesis_support as cs
from python_ta.typecheck.base import TypeFail
from python_ta.transforms.type_inference_visitor import NoType
settings.load_profile("pyta")


@given(cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1), cs.index_node()))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_inference_list_subscript(node):
    """Test whether visitor properly set the type constraint of Subscript node representing list-index access."""
    module, _ = cs._parse_text(node)
    for subscript_node in module.nodes_of_class(astroid.Subscript):
        list_node = subscript_node.value
        assert subscript_node.inf_type.getValue() == list_node.elts[0].inf_type.getValue()


@given(cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1), cs.slice_node()))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_subscript_homogeneous_list_slice(node):
    """Test visitor of Subscript node representing slicing of homogeneous list."""
    module, _ = cs._parse_text(node)
    for subscript_node in module.nodes_of_class(astroid.Subscript):
        list_node = subscript_node.value
        assert subscript_node.inf_type.getValue() == List[list_node.elts[0].inf_type.getValue()]

Esempio n. 4
0
import astroid
import nose
from nose import SkipTest
from hypothesis import given, settings, HealthCheck
from typing import List
import tests.custom_hypothesis_support as cs
from python_ta.typecheck.base import TypeFail, TypeFailFunction
from python_ta.transforms.type_inference_visitor import NoType

settings.load_profile("pyta")


@given(
    cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1),
                      cs.index_node()))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_inference_list_subscript(node):
    """Test whether visitor properly set the type constraint of Subscript node representing list-index access."""
    module, _ = cs._parse_text(node)
    for subscript_node in module.nodes_of_class(astroid.Subscript):
        list_node = subscript_node.value
        assert subscript_node.inf_type.getValue(
        ) == list_node.elts[0].inf_type.getValue()


@given(
    cs.subscript_node(cs.simple_homogeneous_list_node(min_size=1),
                      cs.slice_node()))
@settings(suppress_health_check=[HealthCheck.too_slow])
def test_subscript_homogeneous_list_slice(node):
    """Test visitor of Subscript node representing slicing of homogeneous list."""
Esempio n. 5
0
    """Test Const nodes representing int, bool, float, and None literal values."""
    assume(not(isinstance(node.value, str)))
    module, _ = cs._parse_text(node)
    cs._verify_type_setting(module, astroid.Const, type(node.value))


@given(cs.tuple_node())
def test_tuple(t_tuple):
    """ Test Tuple nodes representing a tuple of various types."""
    module, _ = cs._parse_text(t_tuple)
    for t_node in module.nodes_of_class(astroid.Tuple):
        elt_types = tuple(elt.type_constraints.type for elt in t_node.elts)
        assert t_node.type_constraints.type == Tuple[elt_types]


@given(cs.simple_homogeneous_list_node(min_size=1))
def test_homogeneous_lists(lst):
    """Test List nodes representing a list of values of the same primitive type."""
    module, _ = cs._parse_text(lst)
    list_node = list(module.nodes_of_class(astroid.List))[0]
    if len(list_node.elts) == 0:
        assert list_node.type_constraints.type == List[Any]
    else:
        cs._verify_type_setting(module, astroid.List, List[type(lst.elts[0].value)])


@given(cs.list_node(min_size=2))
def test_random_lists(lst):
    """Test List nodes representing a list of values of different primitive types."""
    assume(not isinstance(lst.elts[0].value, type(lst.elts[1].value)))
    assume(not isinstance(lst.elts[1].value, type(lst.elts[0].value)))