@given(cs.homogeneous_list(min_size=1)) def test_for_homogeneous_list(iterable): """Test whether visitors properly set the type constraint of the a For node representing for/else statement iterating over a homogeneous list. """ program = f'for elt in {iterable}:\n' \ f' x = elt\n' module, TypeInferrer = cs._parse_text(program) for_node = list(module.nodes_of_class(astroid.For))[0] local_type_var = module.type_environment.lookup_in_env('x') inferred_type = TypeInferrer.type_constraints.lookup_concrete(local_type_var) assert inferred_type == for_node.iter.type_constraints.type.__args__[0] @given(cs.random_list(min_size=2)) def test_for_heterogeneous_list(iterable): """Test whether visitors properly set the type constraint of the a For node representing for/else statement iterating over a heterogeneous list. """ assume(type(iterable[0]) != type(iterable[1])) val_types = [type(val) for val in iterable] if int in val_types: assume(bool not in val_types) if bool in val_types: assume(int not in val_types) program = f'for elt in {iterable}:\n' \ f' x = elt\n' module, TypeInferrer = cs._parse_text(program) for_node = list(module.nodes_of_class(astroid.For))[0] local_type_var = module.type_environment.lookup_in_env('x')
@given(cs.tuple_strategy(min_size=2)) 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)]) @given(cs.homogeneous_list(min_size=2)) 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])]) @given(cs.random_list(min_size=2)) 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]) @given(cs.homogeneous_dictionary(min_size=1)) 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])]) @given(cs.heterogeneous_dictionary(min_size=2))
for variable_name in variables_dict: assume(not iskeyword(variable_name)) program = ("x = [" + ", ".join([repr(value) for value in variables_dict.values()]) + "]\n" + ", ".join(variables_dict.keys()) + " = x") module, typeinferrer = cs._parse_text(program) ass_node = list(module.nodes_of_class(astroid.Assign))[0] for variable_name in variables_dict: var_tvar = module.type_environment.lookup_in_env(variable_name) assert typeinferrer.type_constraints.lookup_concrete(var_tvar) == ass_node.value.elts[0].type_constraints.type @nottest @given(hs.lists(cs.valid_identifier(), min_size=2), cs.random_list(min_size=2)) def test_assign_complex(variables, values): """Test whether visitors properly set the type constraint of the a Assign node representing a multi-target-assign with a homogeneous list as the value. """ assume(type(values[0]) != type(values[1]) and len(variables) == len(values)) val_types = [type(val) for val in values] if int in val_types: assume(bool not in val_types) if bool in val_types: assume(int not in val_types) program = ("x = [" + ", ".join([repr(val) for val in values]) + "]\n" + ", ".join(variables) + " = x")
@settings(suppress_health_check=[HealthCheck.too_slow]) def test_for_homogeneous_list(iterable): """Test whether visitors properly set the type constraint of the a For node representing for/else statement iterating over a homogeneous list. """ program = f'for elt in {iterable}:\n' \ f' x = elt\n' module, TypeInferrer = cs._parse_text(program) for_node = list(module.nodes_of_class(astroid.For))[0] local_type_var = module.type_environment.lookup_in_env('x') inferred_type = TypeInferrer.type_constraints.resolve( local_type_var).getValue() assert inferred_type == for_node.iter.inf_type.getValue().__args__[0] @given(cs.random_list(min_size=2)) @settings(suppress_health_check=[HealthCheck.too_slow]) def test_for_heterogeneous_list(iterable): """Test whether visitors properly set the type constraint of the a For node representing for/else statement iterating over a heterogeneous list. """ assume(type(iterable[0]) != type(iterable[1])) val_types = [type(val) for val in iterable] if int in val_types: assume(bool not in val_types) if bool in val_types: assume(int not in val_types) program = f'for elt in {iterable}:\n' \ f' x = elt\n' module, TypeInferrer = cs._parse_text(program) for_node = list(module.nodes_of_class(astroid.For))[0]
""" for variable_name in variables_dict: assume(not iskeyword(variable_name)) program = ("x = [" + ", ".join([repr(value) for value in variables_dict.values()]) + "]\n" + ", ".join(variables_dict.keys()) + " = x") module, typeinferrer = cs._parse_text(program) ass_node = list(module.nodes_of_class(astroid.Assign))[0] for variable_name in variables_dict: var_tvar = module.type_environment.lookup_in_env(variable_name) assert typeinferrer.type_constraints.lookup_concrete(var_tvar) == ass_node.value.elts[0].type_constraints.type @given(hs.lists(cs.valid_identifier(), min_size=2), cs.random_list(min_size=2)) def test_assign_complex(variables, values): """Test whether visitors properly set the type constraint of the a Assign node representing a multi-target-assign with a homogeneous list as the value. """ assume(type(values[0]) != type(values[1]) and len(variables) == len(values)) val_types = [type(val) for val in values] if int in val_types: assume(bool not in val_types) if bool in val_types: assume(int not in val_types) program = ("x = [" + ", ".join([repr(val) for val in values]) + "]\n" + ", ".join(variables) + " = x")