def test_error_of_type(): nodes = astroid.extract_node(""" try: pass except AttributeError: #@ pass try: pass except Exception: #@ pass except: #@ pass """) assert utils.error_of_type(nodes[0], AttributeError) assert utils.error_of_type(nodes[0], (AttributeError, )) assert not utils.error_of_type(nodes[0], Exception) assert utils.error_of_type(nodes[1], Exception)
def test_error_of_type(self): nodes = test_utils.extract_node(""" try: pass except AttributeError: #@ pass try: pass except Exception: #@ pass except: #@ pass """) self.assertTrue(utils.error_of_type(nodes[0], AttributeError)) self.assertTrue(utils.error_of_type(nodes[0], (AttributeError, ))) self.assertFalse(utils.error_of_type(nodes[0], Exception)) self.assertTrue(utils.error_of_type(nodes[1], Exception)) self.assertFalse(utils.error_of_type(nodes[2], ImportError))
def test_error_of_type(): nodes = astroid.extract_node(""" try: pass except AttributeError: #@ pass try: pass except Exception: #@ pass except: #@ pass """) assert utils.error_of_type(nodes[0], AttributeError) assert utils.error_of_type(nodes[0], (AttributeError, )) assert not utils.error_of_type(nodes[0], Exception) assert utils.error_of_type(nodes[1], Exception) assert not utils.error_of_type(nodes[2], ImportError)
def test_error_of_type(self): nodes = test_utils.extract_node(""" try: pass except AttributeError: #@ pass try: pass except Exception: #@ pass except: #@ pass """) self.assertTrue(utils.error_of_type(nodes[0], AttributeError)) self.assertTrue(utils.error_of_type(nodes[0], (AttributeError, ))) self.assertFalse(utils.error_of_type(nodes[0], Exception)) self.assertTrue(utils.error_of_type(nodes[1], Exception)) self.assertFalse(utils.error_of_type(nodes[2], ImportError))
def _redefines_import(node): """Detect that the given node (AssignName) is inside an exception handler and redefines an import from the tryexcept body. Returns True if the node redefines an import, False otherwise. """ current = node while current and not isinstance(current.parent, nodes.ExceptHandler): current = current.parent if not current or not utils.error_of_type(current.parent, ImportError): return False try_block = current.parent.parent for import_node in try_block.nodes_of_class( (nodes.ImportFrom, nodes.Import)): for name, alias in import_node.names: if alias: if alias == node.name: return True elif name == node.name: return True return False