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)])
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)])
def test_const(node): """Test Const nodes representing int, bool, float, and None literal values. NOTE: string literals aren't checked here because it seems that astroid doesn't parse modules that contain only a single string literal. """ assume(not isinstance(node.value, str)) module, _ = cs._parse_text(node) cs._verify_type_setting(module, astroid.Const, type(node.value))
def test_homogeneous_dict(dictionary): """Test Dictionary nodes representing a dictionary with all key:value pairs of same types.""" module, _ = cs._parse_text(dictionary) dict_node = list(module.nodes_of_class(astroid.Dict))[0] if len(dict_node.items) == 0: assert dict_node.inf_type.getValue() == Dict[Any, Any] else: first_key, first_value = next(((k, v) for k, v in dictionary.items)) cs._verify_type_setting(module, astroid.Dict, Dict[type(first_key.value), type(first_value.value)])
def test_homogeneous_dict(dictionary): """Test Dictionary nodes representing a dictionary with all key:value pairs of same types.""" module, _ = cs._parse_text(dictionary) dict_node = list(module.nodes_of_class(astroid.Dict))[0] if len(dict_node.items) == 0: assert dict_node.type_constraints.type == Dict[Any, Any] else: first_key, first_value = next(((k, v) for k, v in dictionary.items)) cs._verify_type_setting(module, astroid.Dict, Dict[type(first_key.value), type(first_value.value)])
def test_const(node): """Test Const nodes representing int, bool, float, and None literal values. NOTE: string literals aren't checked here because it seems that astroid doesn't parse modules that contain only a single string literal. """ assume(not isinstance(node.value, str)) module, _ = cs._parse_text(node) cs._verify_type_setting(module, astroid.Const, type(node.value))
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) module, _ = cs._parse_text(lst) cs._verify_type_setting(module, astroid.List, List[Any])
def test_homogeneous_set(node): """Test Set nodes representing a set of homogeneous values.""" module, _ = cs._parse_text(node) set_node = list(module.nodes_of_class(astroid.Set))[0] if len(set_node.elts) == 0: assert set_node.inf_type.getValue() == Set[Any] else: try: cs._verify_type_setting(module, astroid.Set, Set[type(set_node.elts[0].value)]) except AttributeError: cs._verify_type_setting(module, astroid.Set, Set[type(set_node.elts[0].operand.value)])
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) module, _ = cs._parse_text(lst) cs._verify_type_setting(module, astroid.List, List[Any])
def test_random_set(node): """Test Set nodes representing a set of heterogeneous values.""" assume(not isinstance(list(node.elts)[0].value, type(list(node.elts)[1].value))) assume(not isinstance(list(node.elts)[1].value, type(list(node.elts)[0].value))) val_types = [type(val.value) for val in node.elts] if int in val_types: assume(bool not in val_types) if bool in val_types: assume(int not in val_types) module, _ = cs._parse_text(node) set_node = list(module.nodes_of_class(astroid.Set))[0] cs._verify_type_setting(module, astroid.Set, Set[Any])
def test_heterogeneous_dict(node): """Test Dictionary nodes representing a dictionary with some key:value pairs of different types.""" keys = [item.value for item, _ in node.items] values = [item.value for _, item in node.items] assume(not isinstance(keys[0], type(keys[1]))) assume(not isinstance(values[0], type(values[1]))) assume(not isinstance(keys[1], type(keys[0]))) assume(not isinstance(values[1], type(values[0]))) key_types = [type(key.value) for key, _ in node.items] val_types = [type(val.value) for _, val in node.items] if int in key_types: assume(bool not in val_types) if bool in key_types: assume(int not in val_types) module, _ = cs._parse_text(node) cs._verify_type_setting(module, astroid.Dict, Dict[Any, Any])
def test_heterogeneous_dict(node): """Test Dictionary nodes representing a dictionary with some key:value pairs of different types.""" keys = [item.value for item, _ in node.items] values = [item.value for _, item in node.items] assume(not isinstance(keys[0], type(keys[1]))) assume(not isinstance(values[0], type(values[1]))) assume(not isinstance(keys[1], type(keys[0]))) assume(not isinstance(values[1], type(values[0]))) key_types = [type(key.value) for key, _ in node.items] val_types = [type(val.value) for _, val in node.items] if int in key_types: assume(bool not in val_types) if bool in key_types: assume(int not in val_types) module, _ = cs._parse_text(node) cs._verify_type_setting(module, astroid.Dict, Dict[Any, Any])
def test_homogeneous_lists(lst): """Test List nodes representing a list of values of the same primitive type.""" module = cs._parse_text(str(lst)) cs._verify_type_setting(module, astroid.List, List[type(lst[0])])
def test_simple_literal(node): """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))
def test_dict_index(dict_input, index): """Test index visitor representing a subscript a dictionary""" input_index = cs._index_input_formatter(dict_input, index) module = cs._parse_text(input_index) cs._verify_type_setting(module, astroid.Index, type(index))
def test_simple_literal(const): """Test Const nodes representing int, bool, float, and None literal values.""" assume(not isinstance(const, str)) module = cs._parse_text(str(const)) cs._verify_type_setting(module, astroid.Const, type(const))
def test_tuple_index(tuple_input, index): """Test index visitor representing a subscript for a tuple""" input_index = cs._index_input_formatter(tuple_input, index) module = cs._parse_text(input_index) cs._verify_type_setting(module, astroid.Index, type(index))
def test_heterogeneous_dict(dictionary): """Test Dictionary nodes representing a dictionary with some key:value pairs of different types.""" assume(not isinstance(list(dictionary.keys())[0], type(list(dictionary.keys())[1]))) module = cs._parse_text(str(dictionary)) cs._verify_type_setting(module, astroid.Dict, Dict[Any, Any])
def test_homogeneous_dict(dictionary): """Test Dictionary nodes representing a dictionary with all key:value pairs of same types.""" module = cs._parse_text(str(dictionary)) cs._verify_type_setting(module, astroid.Dict, Dict[type(list(dictionary.keys())[0]), type(list(dictionary.values())[0])])
def test_random_lists(lst): """Test List nodes representing a list of values of different primitive types.""" assume(not isinstance(lst[0], type(lst[1]))) module = cs._parse_text(str(lst)) cs._verify_type_setting(module, astroid.List, List[Any])
def test_tuple(t_tuple): """ Test Tuple nodes representing a tuple of various types.""" module = cs._parse_text(str(t_tuple)) cs._verify_type_setting(module, astroid.Tuple, Tuple[tuple(type(x) for x in t_tuple)])