def _update_parameter_types(self, call_trace: CallTrace, signature: InferredSignature) -> None: arg_types = call_trace.arg_types for name, type_ in signature.parameters.items(): if name not in arg_types: continue current_type = self._rewrite_type(type_) inferred_type = self._rewrite_type(arg_types[name]) if self._check_for_none(inferred_type): continue if self._check_for_none(current_type): new_type = inferred_type else: new_type = self._rewrite_type(Union[current_type, inferred_type]) if str(new_type) != str(current_type): self._logger.debug( "Update type information for %s: parameter %s, old type %s, " "new type %s", call_trace.funcname, name, str(type_), str(new_type), ) signature.update_parameter_type(name, new_type) self._parameter_updates.append( (call_trace.funcname, name, type_, new_type))
def _update_return_types(self, call_trace: CallTrace, signature: InferredSignature) -> None: current_return_type = self._rewrite_type(signature.return_type) inferred_return_type = self._rewrite_type(call_trace.return_type) if self._check_for_none(inferred_return_type): return return_type_name = (inferred_return_type.__name__ if inferred_return_type is not None and hasattr(inferred_return_type, "__name__") else str(inferred_return_type)) if mtt.DUMMY_TYPED_DICT_NAME in return_type_name: new_return_type = current_return_type elif self._check_for_none(current_return_type): new_return_type = inferred_return_type else: new_return_type = self._rewrite_type(Union[current_return_type, inferred_return_type]) if isinstance(new_return_type, type(None)): new_return_type = None if str(new_return_type) != str(current_return_type): self._logger.debug( "Update type information for %s: return type, old type " "%s, new type %s", call_trace.funcname, str(current_return_type), str(new_return_type), ) signature.update_return_type(new_return_type) self._return_type_updates.append( (call_trace.funcname, current_return_type, new_return_type))
def test_add_function(provide_callables_from_fixtures_modules): config.INSTANCE.object_reuse_probability = 0.0 test_case = dtc.DefaultTestCase() generic_function = gao.GenericFunction( function=provide_callables_from_fixtures_modules["triangle"], inferred_signature=InferredSignature( signature=Signature(parameters=[ Parameter(name="x", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), Parameter(name="y", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), Parameter(name="z", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int), ]), return_type=None, parameters={ "x": int, "y": int, "z": int }, ), ) cluster = MagicMock(TestCluster) cluster.select_concrete_type.side_effect = lambda x: x factory = tf.TestFactory(cluster) result = factory.add_function(test_case, generic_function, position=0) assert isinstance(result.variable_type, type(None)) assert test_case.size() <= 4
def test_add_method(provide_callables_from_fixtures_modules, test_cluster_mock): test_case = dtc.DefaultTestCase() object_ = Monkey("foo") methods = inspect.getmembers(object_, inspect.ismethod) generic_method = gao.GenericMethod( owner=provide_callables_from_fixtures_modules["Monkey"], method=methods[3][1], inferred_signature=InferredSignature( signature=Signature(parameters=[ Parameter( name="sentence", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=str, ), ]), return_type=provide_callables_from_fixtures_modules["Monkey"], parameters={"sentence": str}, ), ) test_cluster_mock.select_concrete_type.side_effect = lambda x: x factory = tf.TestFactory(test_cluster_mock) config.INSTANCE.none_probability = 1.0 result = factory.add_method(test_case, generic_method, position=0) assert result.variable_type == provide_callables_from_fixtures_modules[ "Monkey"] assert test_case.size() == 3
def inferred_signature(signature): return InferredSignature( signature=signature, parameters={ "x": int, "y": int }, return_type=int, )
def _get_test_for_no_branches_fixture(module) -> tcc.TestCaseChromosome: test_case = dtc.DefaultTestCase() int_stmt = prim_stmt.IntPrimitiveStatement(test_case, 5) function_call = param_stmt.FunctionStatement( test_case, gao.GenericFunction( module.identity, InferredSignature( signature=inspect.signature(module.identity), parameters={"a": int}, return_type=int, ), ), {"a": int_stmt.ret_val}, ) constructor_call = param_stmt.ConstructorStatement( test_case, gao.GenericConstructor( module.DummyClass, InferredSignature( signature=inspect.signature(module.DummyClass.__init__), parameters={"x": int}, return_type=module.DummyClass, ), ), {"x": function_call.ret_val}, ) method_call = param_stmt.MethodStatement( test_case, gao.GenericMethod( module.DummyClass, module.DummyClass.get_x, InferredSignature(signature=MagicMock(), parameters={}, return_type=int), ), constructor_call.ret_val, ) test_case.add_statement(int_stmt) test_case.add_statement(function_call) test_case.add_statement(constructor_call) test_case.add_statement(method_call) return tcc.TestCaseChromosome(test_case=test_case)
def infer_type_info(self, method: Callable) -> InferredSignature: signature = inspect.signature(method) parameters: Dict[str, Optional[type]] = {} for param_name in signature.parameters: if param_name == "self": continue parameters[param_name] = None return_type: Optional[type] = None return InferredSignature(signature=signature, parameters=parameters, return_type=return_type)
def _infer_type_info_for_callable(method: Callable) -> InferredSignature: signature = inspect.signature(method) parameters: Dict[str, Optional[type]] = {} hints = typing.get_type_hints(method) for param_name in signature.parameters: if param_name == "self": continue parameters[param_name] = hints.get(param_name, None) return_type: Optional[type] = hints.get("return", None) return InferredSignature(signature=signature, parameters=parameters, return_type=return_type)
def function_mock() -> GenericFunction: return GenericFunction( function=simple_function, inferred_signature=InferredSignature( signature=inspect.Signature(parameters=[ inspect.Parameter( name="z", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=float, ), ]), return_type=float, parameters={"z": float}, ), )
def constructor_mock() -> GenericConstructor: return GenericConstructor( owner=SomeType, inferred_signature=InferredSignature( signature=inspect.Signature(parameters=[ inspect.Parameter( name="y", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=float, ), ]), return_type=type(None), parameters={"y": float}, ), )
def infer_type_info(self, method: Callable) -> InferredSignature: assert self._pyi_dir module = sys.modules[method.__module__] name = module.__name__.replace(".", "/") pyi_src = name + ".pyi" pyi_ast = self._read_stub(pyi_src) parameter_types, return_type = self._get_parameter_annotations(method, pyi_ast) if parameter_types: return InferredSignature( signature=inspect.signature(method), parameters=parameter_types if parameter_types else {}, return_type=return_type if return_type else None, ) return TypeHintsInferenceStrategy().infer_type_info(method)
def method_mock() -> GenericMethod: return GenericMethod( owner=SomeType, method=SomeType.simple_method, inferred_signature=InferredSignature( signature=inspect.Signature(parameters=[ inspect.Parameter( name="x", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int, ), ]), return_type=float, parameters={"x": int}, ), )
def _get_test_for_nested_branch_fixture(module) -> tcc.TestCaseChromosome: test_case = dtc.DefaultTestCase() int_stmt = prim_stmt.IntPrimitiveStatement(test_case, -50) function_call = param_stmt.FunctionStatement( test_case, gao.GenericFunction( module.nested_branches, InferredSignature(signature=MagicMock(), parameters={}, return_type=int), ), [int_stmt.ret_val], ) test_case.add_statement(int_stmt) test_case.add_statement(function_call) return tcc.TestCaseChromosome(test_case=test_case)
def _get_test_for_single_branch_else_branch_fixture( module) -> tcc.TestCaseChromosome: test_case = dtc.DefaultTestCase() int_stmt = prim_stmt.IntPrimitiveStatement(test_case, -5) function_call = param_stmt.FunctionStatement( test_case, gao.GenericFunction( module.first, InferredSignature( signature=inspect.signature(module.first), parameters={"a": int}, return_type=int, ), ), {"a": int_stmt.ret_val}, ) test_case.add_statement(int_stmt) test_case.add_statement(function_call) return tcc.TestCaseChromosome(test_case=test_case)
def test_add_constructor(provide_callables_from_fixtures_modules): test_case = dtc.DefaultTestCase() generic_constructor = gao.GenericConstructor( owner=provide_callables_from_fixtures_modules["Basket"], inferred_signature=InferredSignature( signature=Signature( parameters=[ Parameter( name="foo", kind=Parameter.POSITIONAL_OR_KEYWORD, annotation=int ), ] ), return_type=None, parameters={"foo": int}, ), ) cluster = MagicMock(TestCluster) cluster.select_concrete_type.side_effect = lambda x: x factory = tf.TestFactory(cluster) result = factory.add_constructor(test_case, generic_constructor, position=0) assert result.variable_type == provide_callables_from_fixtures_modules["Basket"] assert test_case.size() == 2