def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.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_ = utils.get_setters_property(func_node) if property_: func_node = property_ doc = utils.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)
def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.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_ = utils.get_setters_property(func_node) if property_: func_node = property_ doc = utils.docstringify(func_node.doc, self.config.default_docstring_type) if not doc.is_valid(): if doc.doc: self._handle_no_raise_doc(expected_excs, func_node) return found_excs_full_names = doc.exceptions() # Extract just the class name, e.g. "error" from "re.error" found_excs_class_names = {exc.split(".")[-1] for exc in found_excs_full_names} missing_excs = expected_excs - found_excs_class_names self._add_raise_message(missing_excs, func_node)
def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.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_ = utils.get_setters_property(func_node) if property_: func_node = property_ doc = utils.docstringify(func_node.doc, self.config.default_docstring_type) if not doc.is_valid(): if doc.doc: self._handle_no_raise_doc(expected_excs, func_node) return found_excs_full_names = doc.exceptions() # Extract just the class name, e.g. "error" from "re.error" found_excs_class_names = { exc.split(".")[-1] for exc in found_excs_full_names } missing_excs = expected_excs - found_excs_class_names self._add_raise_message(missing_excs, func_node)
def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.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_ = utils.get_setters_property(func_node) if property_: func_node = property_ doc = utils.docstringify( func_node.doc, self.config.default_docstring_type, ) 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)
def test_exception_instance(self): raise_node = astroid.extract_node(''' def my_func(): raise NotImplementedError("Not implemented!") #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NotImplementedError"]) self.assertEqual(found, expected)
def test_exception_instance(self): raise_node = test_utils.extract_node(''' def my_func(): raise NotImplementedError("Not implemented!") #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NotImplementedError"]) self.assertEqual(found, expected)
def test_exception_class(self): raise_node = astroid.extract_node(''' def my_func(): raise NotImplementedError #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NotImplementedError"]) self.assertEqual(found, expected)
def test_exception_class(self): raise_node = test_utils.extract_node(''' def my_func(): raise NotImplementedError #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NotImplementedError"]) self.assertEqual(found, expected)
def test_possible_exc_types_raising_potential_none() -> None: raise_node = astroid.extract_node( """ def a(): return raise a() #@ """ ) assert utils.possible_exc_types(raise_node) == set()
def test_multiple_rethrow_types(self): raise_node = test_utils.extract_node(''' def my_func(): try: fake_func() except (RuntimeError, ValueError): raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["RuntimeError", "ValueError"]) self.assertEqual(found, expected)
def test_rethrow(self): raise_node = test_utils.extract_node(''' def my_func(): try: fake_func() except RuntimeError: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["RuntimeError"]) self.assertEqual(found, expected)
def test_multiple_rethrow_types(self): raise_node = astroid.extract_node(''' def my_func(): try: fake_func() except (RuntimeError, ValueError): raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["RuntimeError", "ValueError"]) self.assertEqual(found, expected)
def test_no_rethrow_types(self): raise_node = astroid.extract_node(''' def my_func(): try: fake_func() except: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set() self.assertEqual(found, expected)
def test_rethrow(self): raise_node = astroid.extract_node(''' def my_func(): try: fake_func() except RuntimeError: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["RuntimeError"]) self.assertEqual(found, expected)
def test_no_rethrow_types(self): raise_node = test_utils.extract_node(''' def my_func(): try: fake_func() except: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set() self.assertEqual(found, expected)
def test_ignores_uninferable_type(self): raise_node = astroid.extract_node(''' import not_a_module def my_func(): try: fake_func() except not_a_module.Error: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set() self.assertEqual(found, expected)
def test_ignores_uninferable_type(self): raise_node = test_utils.extract_node(''' import not_a_module def my_func(): try: fake_func() except not_a_module.Error: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set() self.assertEqual(found, expected)
def test_nested_in_try_except(self): raise_node = test_utils.extract_node(''' def my_func(): try: fake_func() except RuntimeError: try: another_func() except NameError: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NameError"]) self.assertEqual(found, expected)
def test_nested_in_try_except(self): raise_node = test_utils.extract_node(''' def my_func(): try: fake_func() except RuntimeError: try: another_func() except NameError: raise #@ ''') found = utils.possible_exc_types(raise_node) expected = set(["NameError"]) self.assertEqual(found, expected)
def visit_raise(self, node: nodes.Raise) -> None: func_node = node.frame(future=True) if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.possible_exc_types(node) if not expected_excs: return if not func_node.doc_node: # If this is a property setter, # the property should have the docstring instead. property_ = utils.get_setters_property(func_node) if property_: func_node = property_ doc = utils.docstringify(func_node.doc_node, self.linter.namespace.default_docstring_type) if not doc.matching_sections(): if doc.doc: missing = {exc.name for exc in expected_excs} self._handle_no_raise_doc(missing, func_node) return found_excs_full_names = doc.exceptions() # Extract just the class name, e.g. "error" from "re.error" found_excs_class_names = { exc.split(".")[-1] for exc in found_excs_full_names } missing_excs = set() for expected in expected_excs: for found_exc in found_excs_class_names: if found_exc == expected.name: break if any(found_exc == ancestor.name for ancestor in expected.ancestors()): break else: missing_excs.add(expected.name) self._add_raise_message(missing_excs, func_node)
def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.possible_exc_types(node) if not expected_excs: return doc = utils.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)
def visit_raise(self, node): func_node = node.frame() if not isinstance(func_node, astroid.FunctionDef): return expected_excs = utils.possible_exc_types(node) if not expected_excs: return doc = utils.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)
def test_exception(raise_node, expected): found_nodes = utils.possible_exc_types(raise_node) for node in found_nodes: assert isinstance(node, astroid.nodes.ClassDef) assert {node.name for node in found_nodes} == expected
def test_exception(raise_node, expected): found = utils.possible_exc_types(raise_node) assert found == expected
def test_exception(raise_node, expected): found = utils.possible_exc_types(raise_node) assert found == expected