Пример #1
0
    def visit_raise(self, node):
        func_node = node.frame()
        if not isinstance(func_node, astroid.FunctionDef):
            return

        expected_excs = docstrings_checker.possible_exc_types(node)
        if not expected_excs:
            return

        if not func_node.doc:
            # If this is a property setter,
            # the property should have the docstring instead.
            property_ = docstrings_checker.get_setters_property(func_node)
            if property_:
                func_node = property_

        doc = docstrings_checker.docstringify(func_node.doc)
        if not doc.is_valid():
            if doc.doc:
                self._handle_no_raise_doc(expected_excs, func_node)
            return

        found_excs = doc.exceptions()
        missing_excs = expected_excs - found_excs
        self._add_raise_message(missing_excs, func_node)
Пример #2
0
    def visit_raise(self, node):
        """Visits a function node that raises an exception and verifies that all
        exceptions raised in the function definition are documented.

        Args:
            node: astroid.scoped_nodes.Function. Node for a function or
                method definition in the AST.
        """
        func_node = node.frame()
        if not isinstance(func_node, astroid.FunctionDef):
            return

        expected_excs = docstrings_checker.possible_exc_types(node)
        if not expected_excs:
            return

        if not func_node.doc:
            # If this is a property setter,
            # the property should have the docstring instead.
            property_ = docstrings_checker.get_setters_property(func_node)
            if property_:
                func_node = property_

        doc = docstrings_checker.docstringify(func_node.doc)
        if not doc.is_valid():
            if doc.doc:
                self._handle_no_raise_doc(expected_excs, func_node)
            return

        found_excs = doc.exceptions()
        missing_excs = expected_excs - found_excs
        self._add_raise_message(missing_excs, func_node)
Пример #3
0
    def test_possible_exc_types_with_inference_error(self):

        @contextlib.contextmanager
        def swap(obj, attr, newvalue):
            """Swap an object's attribute value within the context of a
            'with' statement. The object can be anything that supports
            getattr and setattr, such as class instances, modules, etc.
            """
            original = getattr(obj, attr)
            setattr(obj, attr, newvalue)
            try:
                yield
            finally:
                setattr(obj, attr, original)

        raise_node = astroid.extract_node("""
        def func():
            raise Exception('An exception.') #@
        """)
        node_ignores_exception_swap = swap(
            utils, 'node_ignores_exception',
            lambda _, __: (_ for _ in ()).throw(astroid.InferenceError()))

        with node_ignores_exception_swap:
            exceptions = docstrings_checker.possible_exc_types(raise_node)
        self.assertEqual(exceptions, set([]))
Пример #4
0
    def test_possible_exc_types_with_no_exception(self):
        raise_node = astroid.extract_node("""
        def func():
            \"\"\"Function to test raising exceptions.\"\"\"
            raise #@
        """)

        exceptions = docstrings_checker.possible_exc_types(raise_node)
        self.assertEqual(exceptions, set([]))
Пример #5
0
    def test_possible_exc_types_with_exception_inside_function(self):
        raise_node = astroid.extract_node("""
        def func():
            try:
                raise Exception('An exception.')
            except Exception:
                raise #@
        """)

        exceptions = docstrings_checker.possible_exc_types(raise_node)
        self.assertEqual(exceptions, set(['Exception']))