Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
astroid.Lambda, astroid.FunctionDef, astroid.ClassDef,
astroid.bases.Generator, astroid.UnboundMethod,
astroid.BoundMethod, astroid.Module)
structs = (astroid.Dict, astroid.Tuple, astroid.Set)
# These nodes are excepted, since they are not constant
# values, requiring a computation to happen. The only type
# of node in this list which doesn't have this property is
# Getattr, which is excepted because the conditional statement
# can be used to verify that the attribute was set inside a class,
# which is definitely a valid use case.
except_nodes = (astroid.Attribute, astroid.Call,
astroid.BinOp, astroid.BoolOp, astroid.UnaryOp,
astroid.Subscript)
inferred = None
emit = isinstance(test, (astroid.Const, ) + structs + const_nodes)
if not isinstance(test, except_nodes):
inferred = helpers.safe_infer(test)
if emit or isinstance(inferred, const_nodes):
self.add_message('using-constant-test', node=node)
def is_none(node):
return (node is None or
(isinstance(node, astroid.Const) and node.value is None) or
(isinstance(node, astroid.Name) and node.name == 'None')
)
and previous.has_dynamic_getattr()
):
# Don't warn if the object has a custom __getattr__
break
path = get_access_path(key, parsed)
self.add_message(
"missing-format-attribute",
args=(specifier, path),
node=node,
)
break
else:
warn_error = False
if hasattr(previous, "getitem"):
try:
previous = previous.getitem(astroid.Const(specifier))
except (
astroid.AstroidIndexError,
astroid.AstroidTypeError,
astroid.AttributeInferenceError,
):
warn_error = True
except astroid.InferenceError:
break
if previous is astroid.Uninferable:
break
else:
try:
# Lookup __getitem__ in the current node,
# but skip further checks, because we can't
# retrieve the looked object
previous.getattr("__getitem__")
def _is_meta_with_abstract(node):
if isinstance(node, ClassDef) and node.name == 'Meta':
for meta_child in node.get_children():
if not isinstance(meta_child, Assign):
continue
if not meta_child.targets[0].name == 'abstract':
continue
if not isinstance(meta_child.value, Const):
continue
# TODO: handle tuple assignment?
# eg:
# abstract, something_else = True, 1
if meta_child.value.value:
# this class is abstract
return True
return False
return
first = node.args[0]
if not isinstance(first, astroid.Const):
# Computed first argument, can't tell what it is.
return
if not isinstance(first.value, int):
# First argument is not an int, that's fine.
return
# If there are three args and the third is 1, that's bad.
three1 = False
if len(node.args) == 3:
third = node.args[2]
if isinstance(third, astroid.Const):
if isinstance(third.value, int) and third.value == 1:
three1 = True
if first.value == 0:
# The first argument is 0, suspicious.
if len(node.args) == 2:
# range(0, n): bad.
self.add_message(self.MESSAGE_ID, args=(node.func.name, "single"), node=node)
elif three1:
# range(0, n, 1): bad.
self.add_message(self.MESSAGE_ID, args=(node.func.name, "single"), node=node)
elif three1:
# range(n, m, 1): bad.
self.add_message(self.MESSAGE_ID, args=(node.func.name, "two"), node=node)
def handle_bin_op(expr, **kwargs) -> Optional[Token]:
token_info = dict(line=expr.lineno, col=expr.col_offset)
if isinstance(expr.op, ast.Div) or expr.op == '/':
if isinstance(expr.right, astroid.node_classes.NodeNG):
guesses = infer(expr=expr.right)
token_info['col'] = expr.right.col_offset
for guess in guesses:
if type(guess) is not astroid.Const:
continue
return Token(value=ZeroDivisionError, **token_info)
if isinstance(expr.right, ast.Num) and expr.right.n == 0:
token_info['col'] = expr.right.col_offset
return Token(value=ZeroDivisionError, **token_info)
return None
yield Token(value=expr.value.value, **token_info)
continue
# positive number
if isinstance(expr.value, ast.Num):
yield Token(value=expr.value.n, **token_info)
continue
# negative number
if isinstance(expr.value, TOKENS.UNARY_OP):
is_minus = isinstance(expr.value.op, ast.USub) or expr.value.op == '-'
if is_minus:
if isinstance(expr.value.operand, ast.Num):
yield Token(value=-expr.value.operand.n, **token_info)
continue
if isinstance(expr.value.operand, astroid.Const):
yield Token(value=-expr.value.operand.value, **token_info)
continue
# astroid inference
if hasattr(expr.value, 'infer'):
try:
guesses = tuple(expr.value.infer())
except astroid.exceptions.NameInferenceError:
continue
for value in guesses:
if isinstance(value, astroid.Const):
yield Token(value=value.value, **token_info)
def _is_constant_zero(node):
return isinstance(node, astroid.Const) and node.value == 0
return
function_node = safe_infer(node.value.func)
# skip class, generator and incomplete function definition
if not (isinstance(function_node, astroid.Function) and
function_node.root().fully_defined()):
return
if function_node.is_generator() \
or function_node.is_abstract(pass_is_abstract=False):
return
returns = list(function_node.nodes_of_class(astroid.Return,
skip_klass=astroid.Function))
if len(returns) == 0:
self.add_message('assignment-from-no-return', node=node)
else:
for rnode in returns:
if not (isinstance(rnode.value, astroid.Const)
and rnode.value.value is None
or rnode.value is None):
break
else:
self.add_message('assignment-from-none', node=node)