本文整理汇总了Python中astroid.Return方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Return方法的具体用法?Python astroid.Return怎么用?Python astroid.Return使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Return方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: returns_something
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def returns_something(return_node):
"""Check if a return node returns a value other than None.
:param return_node: The return node to check.
:type return_node: astroid.Return
:rtype: bool
:return: True if the return node returns a value other than None,
False otherwise.
"""
returns = return_node.value
if returns is None:
return False
return not (isinstance(returns, astroid.Const) and returns.value is None)
示例2: _if_statement_is_always_returning
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _if_statement_is_always_returning(if_node):
def _has_return_node(elems, scope):
for node in elems:
if isinstance(node, astroid.If):
yield _if_statement_is_always_returning(node)
elif isinstance(node, astroid.Return):
yield node.scope() is scope
scope = if_node.scope()
body_returns = _all_elements_are_true(
_has_return_node(if_node.body, scope=scope)
)
if if_node.orelse:
orelse_returns = _all_elements_are_true(
_has_return_node(if_node.orelse, scope=scope)
)
else:
orelse_returns = False
return body_returns and orelse_returns
示例3: _check_consistent_returns
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _check_consistent_returns(self, node):
"""Check that all return statements inside a function are consistent.
Return statements are consistent if:
- all returns are explicit and if there is no implicit return;
- all returns are empty and if there is, possibly, an implicit return.
Args:
node (astroid.FunctionDef): the function holding the return statements.
"""
# explicit return statements are those with a not None value
explicit_returns = [_node for _node in self._return_nodes[node.name]
if _node.value is not None]
if not explicit_returns:
return
if (len(explicit_returns) == len(self._return_nodes[node.name])
and self._is_node_return_ended(node)):
return
self.add_message('inconsistent-return-statements', node=node)
示例4: visit_return
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def visit_return(self, node: astroid.Return) -> None:
return_tvar = self.lookup_typevar(node, 'return')
# TODO: Replace with isinstance() once proper TypeFail subclass is created for unbound indentifiers
if return_tvar == TypeFail("Unbound identifier"):
return_target = TypeFailReturn(node)
else:
return_target = return_tvar
if node.value is not None and getattr(node.scope(), 'returns', None) is not None:
return_annotation = _ann_node_to_type(node.scope().returns)
return_value = self.type_constraints.unify(node.value.inf_type, return_annotation, node)
elif node.value is not None:
return_value = node.value.inf_type
else:
return_value = TypeInfo(None)
val_inf_type = self.type_constraints.unify(return_value, return_target, node)
node.inf_type = val_inf_type if isinstance(val_inf_type, TypeFail) else NoType()
示例5: accept_failable
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def accept_failable(f: Callable) -> Callable:
"""Decorator to allow function fo to optionally acceptance instances of Failable as arguments."""
def _f(*args, **kwargs):
"""Extract value from Failable arguments, pass values to function f.
Return TypeFail instead if found.
"""
new_args = []
new_kwargs = {}
for a in args:
if isinstance(a, Failable):
if isinstance(a, TypeFail):
return a
a >> new_args.append
else:
new_args.append(a)
for kw in kwargs:
if isinstance(kwargs[kw], Failable):
if isinstance(kwargs[kw], Failable):
return kwargs[kw]
new_kwargs += kwargs[kw] >> (lambda t: dict(kw=t))
else:
new_kwargs[kw] = kwargs[kw]
return f(*new_args, **new_kwargs)
return _f
示例6: resolve
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def resolve(self, t: type) -> TypeResult:
"""Return the concrete type or set representative associated with the given type.
"""
if isinstance(t, _GenericAlias):
if t.__args__ is not None:
res_args = [self.resolve(arg) for arg in t.__args__]
return _wrap_generic_meta(t, failable_collect(res_args))
else:
return TypeInfo(t)
elif isinstance(t, TypeVar):
try:
repr = self.find_repr(self.type_to_tnode[str(t)])
if repr and repr.type is not t:
if any(elt is t for elt in getattr(repr.type, '__args__', [])):
return TypeInfo(t)
else:
return self.resolve(repr.type)
except KeyError:
return TypeInfo(t)
return TypeInfo(t)
示例7: _check_consistent_returns
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _check_consistent_returns(self, node):
"""Check that all return statements inside a function are consistent.
Return statements are consistent if:
- all returns are explicit and if there is no implicit return;
- all returns are empty and if there is, possibly, an implicit return.
Args:
node (astroid.FunctionDef): the function holding the return statements.
"""
# explicit return statements are those with a not None value
explicit_returns = [
_node for _node in self._return_nodes[node.name] if _node.value is not None
]
if not explicit_returns:
return
if len(explicit_returns) == len(
self._return_nodes[node.name]
) and self._is_node_return_ended(node):
return
self.add_message("inconsistent-return-statements", node=node)
示例8: _has_homonym_in_upper_function_scope
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _has_homonym_in_upper_function_scope(self, node, index):
"""
Return True if there is a node with the same name in the to_consume dict of an upper scope
and if that scope is a function
:param node: node to check for
:type node: astroid.Node
:param index: index of the current consumer inside self._to_consume
:type index: int
:return: True if there is a node with the same name in the to_consume dict of an upper scope
and if that scope is a function
:rtype: bool
"""
for _consumer in self._to_consume[index - 1 :: -1]:
if _consumer.scope_type == "function" and node.name in _consumer.to_consume:
return True
return False
示例9: _check_return_at_the_end
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _check_return_at_the_end(self, node):
"""Check for presence of a *single* return statement at the end of a
function. "return" or "return None" are useless because None is the
default return type if they are missing.
NOTE: produces a message only if there is a single return statement
in the function body. Otherwise _check_consistent_returns() is called!
Per its implementation and PEP8 we can have a "return None" at the end
of the function body if there are other return statements before that!
"""
if len(self._return_nodes[node.name]) > 1:
return
if len(node.body) <= 1:
return
last = node.body[-1]
if isinstance(last, astroid.Return):
# e.g. "return"
if last.value is None:
self.add_message("useless-return", node=node)
# return None"
elif isinstance(last.value, astroid.Const) and (last.value.value is None):
self.add_message("useless-return", node=node)
示例10: check_functiondef_returns
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def check_functiondef_returns(self, node, node_doc):
if ((not node_doc.supports_yields and node.is_generator())
or node.is_abstract()):
return
return_nodes = node.nodes_of_class(astroid.Return)
if ((node_doc.has_returns() or node_doc.has_rtype()) and
not any(utils.returns_something(ret_node) for ret_node in return_nodes)):
self.add_message(
'redundant-returns-doc',
node=node)
示例11: _check_late_binding_closure
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _check_late_binding_closure(self, node, assignment_node):
def _is_direct_lambda_call():
return (isinstance(node_scope.parent, astroid.Call)
and node_scope.parent.func is node_scope)
node_scope = node.scope()
if not isinstance(node_scope, (astroid.Lambda, astroid.FunctionDef)):
return
if isinstance(node.parent, astroid.Arguments):
return
if isinstance(assignment_node, astroid.Comprehension):
if assignment_node.parent.parent_of(node.scope()):
self.add_message('cell-var-from-loop', node=node, args=node.name)
else:
assign_scope = assignment_node.scope()
maybe_for = assignment_node
while not isinstance(maybe_for, astroid.For):
if maybe_for is assign_scope:
break
maybe_for = maybe_for.parent
else:
if (maybe_for.parent_of(node_scope)
and not _is_direct_lambda_call()
and not isinstance(node_scope.statement(), astroid.Return)):
self.add_message('cell-var-from-loop', node=node, args=node.name)
示例12: _ignore_class_scope
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _ignore_class_scope(self, node):
"""
Return True if the node is in a local class scope, as an assignment.
:param node: Node considered
:type node: astroid.Node
:return: True if the node is in a local class scope, as an assignment. False otherwise.
:rtype: bool
"""
# Detect if we are in a local class scope, as an assignment.
# For example, the following is fair game.
#
# class A:
# b = 1
# c = lambda b=b: b * b
#
# class B:
# tp = 1
# def func(self, arg: tp):
# ...
# class C:
# tp = 2
# def func(self, arg=tp):
# ...
name = node.name
frame = node.statement().scope()
in_annotation_or_default = self._defined_in_function_definition(node, frame)
if in_annotation_or_default:
frame_locals = frame.parent.scope().locals
else:
frame_locals = frame.locals
return not ((isinstance(frame, astroid.ClassDef) or in_annotation_or_default) and
name in frame_locals)
示例13: _check_exception_inherit_from_stopiteration
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _check_exception_inherit_from_stopiteration(exc):
"""Return True if the exception node in argument inherit from StopIteration"""
stopiteration_qname = '{}.StopIteration'.format(utils.EXCEPTIONS_MODULE)
return any(_class.qname() == stopiteration_qname for _class in exc.mro())
示例14: visit_functiondef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def visit_functiondef(self, node):
self._return_nodes[node.name] = []
return_nodes = node.nodes_of_class(astroid.Return)
self._return_nodes[node.name] = [_rnode for _rnode in return_nodes
if _rnode.frame() == node.frame()]
示例15: _is_function_def_never_returning
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Return [as 别名]
def _is_function_def_never_returning(self, node):
"""Return True if the function never returns. False otherwise.
Args:
node (astroid.FunctionDef): function definition node to be analyzed.
Returns:
bool: True if the function never returns, False otherwise.
"""
try:
return node.qname() in self._never_returning_functions
except TypeError:
return False