Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _transfer(self, block: CFGBlock, out_facts: Set[str]) -> Set[str]:
gen = out_facts.copy()
kill = set()
for statement in reversed(block.statements):
if isinstance(statement, astroid.FunctionDef):
# `nodes_of_class` below doesnt block looking for required nodes
# in function definitions, hence this case.
continue
for node in statement.nodes_of_class((astroid.AssignName, astroid.DelName, astroid.Name,
astroid.Nonlocal, astroid.Global),
astroid.FunctionDef):
if isinstance(node, astroid.AssignName):
if node.name in gen.difference(kill):
self._redundant_assignment.add(node.parent)
elif node.parent in self._redundant_assignment:
self._redundant_assignment.remove(node.parent)
# When node.parent is an AugAssign, the name counts as a use of the variable,
# and so is added to kill.
if isinstance(node.parent, astroid.AugAssign):
kill.add(node.name)
else:
kill.discard(node.name)
gen.add(node.name)
elif isinstance(node, (astroid.Nonlocal, astroid.Global)):
def _looks_like_random_sample(node):
func = node.func
if isinstance(func, astroid.Attribute):
return func.attrname == "sample"
if isinstance(func, astroid.Name):
return func.name == "sample"
return False
def _resolve_name(contract):
if not isinstance(contract, astroid.Name):
return contract
definitions = contract.lookup(contract.name)[1]
if not definitions:
return contract
definition = definitions[0]
if isinstance(definition, astroid.FunctionDef):
return definition
if isinstance(definition, astroid.AssignName):
return definition.parent.value
# resolved into something tricky, live with it
return contract # pragma: no cover
return
if not isinstance(type_annotation, astroid.Subscript):
return
if (
isinstance(type_annotation.value, astroid.Attribute)
and isinstance(type_annotation.value.expr, astroid.Name)
and type_annotation.value.expr.name == TYPING_MODULE
):
self._type_annotation_names.append(TYPING_MODULE)
return
self._type_annotation_names.extend(
annotation.name
for annotation in type_annotation.nodes_of_class(astroid.Name)
)
def looks_like_numpy_member(
member_name: str, node: astroid.node_classes.NodeNG
) -> bool:
"""
Returns True if the node is a member of numpy whose
name is member_name.
:param member_name: name of the member
:param node: node to test
:return: True if the node is a member of numpy
"""
return (
isinstance(node, astroid.Attribute)
and node.attrname == member_name
and isinstance(node.expr, astroid.Name)
and _is_a_numpy_module(node.expr)
)
def _populate_local_env_attrs(self, node: NodeNG) -> None:
"""Store in TypeStore the attributes of any unresolved class names"""
for attr_node in chain(node.nodes_of_class(astroid.Attribute), node.nodes_of_class(astroid.AssignAttr)):
if isinstance(attr_node.expr, astroid.Name) and attr_node.expr.name in node.type_environment.locals:
class_type = node.type_environment.lookup_in_env(attr_node.expr.name)
if isinstance(class_type, TypeVar):
self.type_store.classes[class_type.__name__]['__mro'] = [class_type.__name__]
if not attr_node.attrname in self.type_store.classes[class_type.__name__]:
self.type_store.classes[class_type.__name__][attr_node.attrname] = \
[(self.type_constraints.fresh_tvar(attr_node), 'attribute')]
def visit_function(self, node):
if not redefined_by_decorator(node):
self._check_redefinition(node.is_method() and 'method' or 'function', node)
# checks for max returns, branch, return in __init__
returns = node.nodes_of_class(astroid.Return,
skip_klass=(astroid.Function, astroid.Class))
if node.is_method() and node.name == '__init__':
if node.is_generator():
self.add_message('init-is-generator', node=node)
else:
values = [r.value for r in returns]
# Are we returning anything but None from constructors
if [v for v in values
if not (v is None or
(isinstance(v, astroid.Const) and v.value is None) or
(isinstance(v, astroid.Name) and v.name == 'None')
)]:
self.add_message('return-in-init', node=node)
elif node.is_generator():
# make sure we don't mix non-None returns and yields
if not PY33:
for retnode in returns:
if isinstance(retnode.value, astroid.Const) and \
retnode.value.value is not None:
self.add_message('return-arg-in-generator', node=node,
line=retnode.fromlineno)
# Check for duplicate names
args = set()
for name in node.argnames():
if name in args:
self.add_message('duplicate-argument-name', node=node, args=(name,))
else:
def _looks_like_functools_member(node, member):
"""Check if the given Call node is a functools.partial call"""
if isinstance(node.func, astroid.Name):
return node.func.name == member
elif isinstance(node.func, astroid.Attribute):
return (
node.func.attrname == member
and isinstance(node.func.expr, astroid.Name)
and node.func.expr.name == "functools"
)
def is_decorated_with_property(node):
"""Check if the function is decorated as a property.
:param node: The node to check.
:type node: astroid.nodes.FunctionDef
:returns: True if the function is a property, False otherwise.
:rtype: bool
"""
if not node.decorators:
return False
for decorator in node.decorators.nodes:
if not isinstance(decorator, astroid.Name):
continue
try:
if _is_property_decorator(decorator):
return True
except astroid.InferenceError:
pass
return False
"""
parent = node.parent
# Since a call can't be the loop variant we only need to know if the node's
# parent is a 'for' loop to know it's being used as the iterator for the
# loop.
if isinstance(parent, astroid.For):
return True
# Need to make sure the use of the node is in the iterator part of the
# comprehension.
elif isinstance(parent, astroid.Comprehension):
if parent.iter == node:
return True
# Various built-ins can take in an iterable or list and lead to the same
# value.
elif isinstance(parent, astroid.CallFunc):
if isinstance(parent.func, astroid.Name):
parent_scope = parent.func.lookup(parent.func.name)[0]
if _is_builtin(parent_scope) and parent.func.name in _accepts_iterator:
return True
elif isinstance(parent.func, astroid.Getattr):
if parent.func.attrname == 'join':
return True
# If the call is in an unpacking, there's no need to warn,
# since it can be considered iterating.
elif (isinstance(parent, astroid.Assign) and
isinstance(parent.targets[0], (astroid.List, astroid.Tuple))):
if len(parent.targets[0].elts) > 1:
return True
return False