예제 #1
0
    def _add_dependency(self, klass: Type, recursion_level: int,
                        add_to_test: bool):
        """Add constructor/methods/attributes of the given type to the test cluster.

        Args:
            klass: The type of the dependency
            recursion_level: the current recursion level of the search
            add_to_test: whether the accessible objects are also added to objects
                under test.
        """
        assert inspect.isclass(klass), "Can only add dependencies for classes."
        if klass in self._analyzed_classes:
            self._logger.debug("Class %s already analyzed", klass)
            return
        self._analyzed_classes.add(klass)
        self._logger.debug("Analyzing class %s", klass)
        generic_constructor = GenericConstructor(
            klass,
            self._inference.infer_type_info(klass.__init__)[0])
        if self._discard_accessible_with_missing_type_hints(
                generic_constructor):
            return

        self._test_cluster.add_generator(generic_constructor)
        if add_to_test:
            self._test_cluster.add_accessible_object_under_test(
                generic_constructor)
        self._add_callable_dependencies(generic_constructor, recursion_level)

        for method_name, method in inspect.getmembers(klass,
                                                      inspect.isfunction):
            # TODO(fk) why does inspect.ismethod not work here?!
            self._logger.debug("Analyzing method %s", method_name)

            generic_method = GenericMethod(
                klass, method,
                self._inference.infer_type_info(method)[0], method_name)

            if (self._is_constructor(method_name)
                    or not self._is_method_defined_in_class(klass, method)
                    or self._is_protected(method_name)
                    or self._discard_accessible_with_missing_type_hints(
                        generic_method)):
                # Skip methods that should not be added to the cluster here.
                # Constructors are handled elsewhere; inherited methods should not be
                # part of the cluster, only overridden methods; private methods should
                # neither be part of the cluster.
                continue

            self._test_cluster.add_generator(generic_method)
            self._test_cluster.add_modifier(klass, generic_method)
            if add_to_test:
                self._test_cluster.add_accessible_object_under_test(
                    generic_method)
            self._add_callable_dependencies(generic_method, recursion_level)
예제 #2
0
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},
        ),
    )
예제 #3
0
def test_generic_method_eq_modified(method_mock):
    second = GenericMethod(MagicMock, int, MagicMock(InferredSignature))
    assert method_mock != second