本文整理汇总了Python中astroid.Raise方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Raise方法的具体用法?Python astroid.Raise怎么用?Python astroid.Raise使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Raise方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_exception_handlers
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def get_exception_handlers(node, exception):
"""Return the collections of handlers handling the exception in arguments.
Args:
node (astroid.Raise): the node raising the exception.
exception (builtin.Exception or str): exception or name of the exception.
Returns:
generator: the collection of handlers that are handling the exception or None.
"""
context = _import_node_context(node)
if isinstance(context, astroid.TryExcept):
return (_handler for _handler in context.handlers
if error_of_type(_handler, exception))
return None
示例2: with_raises
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def with_raises(self, node, doc):
"""with_raises checks if one line doc end-with '.' .
Args:
node (astroid.node): the node is visiting.
doc (Docstring): Docstring object.
Returns:
True if successful otherwise False.
"""
find = False
for t in node.body:
if not isinstance(t, astroid.Raise):
continue
find = True
break
if not find:
return True
if len(doc.get_raises()) == 0:
self.add_message('W9008', node=node, line=node.fromlineno)
return False
return True
示例3: is_error
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def is_error(node):
"""return true if the function does nothing but raising an exception"""
for child_node in node.get_children():
if isinstance(child_node, astroid.Raise):
return True
return False
示例4: is_raising
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def is_raising(body):
"""return true if the given statement node raise an exception"""
for node in body:
if isinstance(node, astroid.Raise):
return True
return False
示例5: is_node_inside_try_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def is_node_inside_try_except(node):
"""Check if the node is directly under a Try/Except statement.
(but not under an ExceptHandler!)
Args:
node (astroid.Raise): the node raising the exception.
Returns:
bool: True if the node is inside a try/except statement, False otherwise.
"""
context = _import_node_context(node)
return isinstance(context, astroid.TryExcept)
示例6: _is_raising
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def _is_raising(body: typing.List) -> bool:
"""Return true if the given statement node raise an exception"""
for node in body:
if isinstance(node, astroid.Raise):
return True
return False
示例7: is_error
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def is_error(node: astroid.node_classes.NodeNG) -> bool:
"""return true if the function does nothing but raising an exception"""
for child_node in node.get_children():
if isinstance(child_node, astroid.Raise):
return True
return False
示例8: is_node_inside_try_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def is_node_inside_try_except(node: astroid.Raise) -> bool:
"""Check if the node is directly under a Try/Except statement.
(but not under an ExceptHandler!)
Args:
node (astroid.Raise): the node raising the exception.
Returns:
bool: True if the node is inside a try/except statement, False otherwise.
"""
context = find_try_except_wrapper_node(node)
return isinstance(context, astroid.TryExcept)
示例9: _check_superfluous_else_raise
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def _check_superfluous_else_raise(self, node):
return self._check_superfluous_else(
node, msg_id="no-else-raise", returning_node_class=astroid.Raise
)
示例10: _is_node_return_ended
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Raise [as 别名]
def _is_node_return_ended(self, node):
"""Check if the node ends with an explicit return statement.
Args:
node (astroid.NodeNG): node to be checked.
Returns:
bool: True if the node ends with an explicit statement, False otherwise.
"""
# Recursion base case
if isinstance(node, astroid.Return):
return True
if isinstance(node, astroid.Call):
try:
funcdef_node = node.func.infered()[0]
if self._is_function_def_never_returning(funcdef_node):
return True
except astroid.InferenceError:
pass
# Avoid the check inside while loop as we don't know
# if they will be completed
if isinstance(node, astroid.While):
return True
if isinstance(node, astroid.Raise):
# a Raise statement doesn't need to end with a return statement
# but if the exception raised is handled, then the handler has to
# ends with a return statement
if not node.exc:
# Ignore bare raises
return True
if not utils.is_node_inside_try_except(node):
# If the raise statement is not inside a try/except statement
# then the exception is raised and cannot be caught. No need
# to infer it.
return True
exc = utils.safe_infer(node.exc)
if exc is None or exc is astroid.Uninferable:
return False
exc_name = exc.pytype().split('.')[-1]
handlers = utils.get_exception_handlers(node, exc_name)
handlers = list(handlers) if handlers is not None else []
if handlers:
# among all the handlers handling the exception at least one
# must end with a return statement
return any(self._is_node_return_ended(_handler) for _handler in handlers)
# if no handlers handle the exception then it's ok
return True
if isinstance(node, astroid.If):
# if statement is returning if there are exactly two return statements in its
# children : one for the body part, the other for the orelse part
# Do not check if inner function definition are return ended.
return_stmts = [self._is_node_return_ended(_child) for _child in node.get_children()
if not isinstance(_child, astroid.FunctionDef)]
return sum(return_stmts) == 2
# recurses on the children of the node except for those which are except handler
# because one cannot be sure that the handler will really be used
return any(self._is_node_return_ended(_child) for _child in node.get_children()
if not isinstance(_child, astroid.ExceptHandler))