Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_binop_errors(self, node):
for error in node.type_errors():
# Let the error customize its output.
if any(
isinstance(obj, astroid.ClassDef) and not has_known_bases(obj)
for obj in (error.left_type, error.right_type)
):
continue
self.add_message("unsupported-binary-operation", args=str(error), node=node)
"""
if node_ignores_exception(node, AttributeError):
return False
if owner_name in ignored_classes:
return False
# skip None anyway
if isinstance(owner, astroid.Const) and owner.value is None:
return False
if is_super(owner) or getattr(owner, 'type', None) == 'metaclass':
return False
if ignored_mixins and owner_name[-5:].lower() == 'mixin':
return False
if isinstance(owner, astroid.Function) and owner.decorators:
return False
if isinstance(owner, Instance):
if owner.has_dynamic_getattr() or not has_known_bases(owner):
return False
if isinstance(owner, objects.Super):
# Verify if we are dealing with an invalid Super object.
# If it is invalid, then there's no point in checking that
# it has the required attribute. Also, don't fail if the
# MRO is invalid.
try:
owner.super_mro()
except (MroError, SuperError):
return False
if not all(map(has_known_bases, owner.type.mro())):
return False
# explicit skipping of module member access
if owner.root().name in ignored_modules:
return False
if isinstance(owner, astroid.Class):
if owner_name and ignored_mixins and owner_name[-5:].lower() == "mixin":
return False
if isinstance(owner, astroid.FunctionDef) and owner.decorators:
return False
if isinstance(owner, (astroid.Instance, astroid.ClassDef)):
if owner.has_dynamic_getattr():
# Issue #2565: Don't ignore enums, as they have a `__getattr__` but it's not
# invoked at this point.
try:
metaclass = owner.metaclass()
except exceptions.MroError:
return False
if metaclass:
return metaclass.qname() == "enum.EnumMeta"
return False
if not has_known_bases(owner):
return False
# Exclude typed annotations, since these might actually exist
# at some point during the runtime of the program.
attribute = owner.locals.get(node.attrname, [None])[0]
if (
attribute
and isinstance(attribute, astroid.AssignName)
and isinstance(attribute.parent, astroid.AnnAssign)
):
return False
if isinstance(owner, objects.Super):
# Verify if we are dealing with an invalid Super object.
# If it is invalid, then there's no point in checking that
# it has the required attribute. Also, don't fail if the
# MRO is invalid.
def visit_callfunc(self, node):
"""check property usage"""
parent = node.parent.frame()
if (isinstance(parent, astroid.Class) and
not parent.newstyle and
isinstance(node.func, astroid.Name)):
confidence = (INFERENCE if has_known_bases(parent)
else INFERENCE_FAILURE)
name = node.func.name
if name == 'property':
self.add_message('property-on-old-class', node=node,
confidence=confidence)
args=expr.name)
elif ((isinstance(expr, astroid.Name) and expr.name == 'NotImplemented')
or (isinstance(expr, astroid.CallFunc) and
isinstance(expr.func, astroid.Name) and
expr.func.name == 'NotImplemented')):
self.add_message('notimplemented-raised', node=node)
elif isinstance(expr, (Instance, astroid.Class)):
if isinstance(expr, Instance):
# pylint: disable=protected-access
expr = expr._proxied
if (isinstance(expr, astroid.Class) and
not inherit_from_std_ex(expr)):
if expr.newstyle:
self.add_message('raising-non-exception', node=node)
else:
if has_known_bases(expr):
confidence = INFERENCE
else:
confidence = INFERENCE_FAILURE
self.add_message(
'nonstandard-exception', node=node,
confidence=confidence)
else:
value_found = False
else:
value_found = False
return value_found
def _check_catching_non_exception(self, handler, exc, part):
if isinstance(exc, astroid.Tuple):
# Check if it is a tuple of exceptions.
inferred = [utils.safe_infer(elt) for elt in exc.elts]
if any(node is astroid.Uninferable for node in inferred):
# Don't emit if we don't know every component.
return
if all(
node
and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
for node in inferred
):
return
if not isinstance(exc, astroid.ClassDef):
# Don't emit the warning if the inferred stmt
# is None, but the exception handler is something else,
# maybe it was redefined.
if isinstance(exc, astroid.Const) and exc.value is None:
if (
isinstance(handler.type, astroid.Const)
and handler.type.value is None
) or handler.type.parent_of(exc):
# If the exception handler catches None or
# the exception component, which is None, is
# defined by the entire exception handler, then
def _check_in_slots(self, node):
""" Check that the given AssignAttr node
is defined in the class slots.
"""
inferred = safe_infer(node.expr)
if not isinstance(inferred, astroid.Instance):
return
klass = inferred._proxied
if not has_known_bases(klass):
return
if "__slots__" not in klass.locals or not klass.newstyle:
return
slots = klass.slots()
if slots is None:
return
# If any ancestor doesn't use slots, the slots
# defined for this class are superfluous.
if any(
"__slots__" not in ancestor.locals and ancestor.name != "object"
for ancestor in klass.ancestors()
):
return
if not any(slot.value == node.attrname for slot in slots):
return False
if isinstance(owner, astroid.Function) and owner.decorators:
return False
if isinstance(owner, Instance):
if owner.has_dynamic_getattr() or not has_known_bases(owner):
return False
if isinstance(owner, objects.Super):
# Verify if we are dealing with an invalid Super object.
# If it is invalid, then there's no point in checking that
# it has the required attribute. Also, don't fail if the
# MRO is invalid.
try:
owner.super_mro()
except (MroError, SuperError):
return False
if not all(map(has_known_bases, owner.type.mro())):
return False
# explicit skipping of module member access
if owner.root().name in ignored_modules:
return False
if isinstance(owner, astroid.Class):
# Look up in the metaclass only if the owner is itself
# a class.
# TODO: getattr doesn't return by default members
# from the metaclass, because handling various cases
# of methods accessible from the metaclass itself
# and/or subclasses only is too complicated for little to
# no benefit.
metaclass = owner.metaclass() or owner.implicit_metaclass()
try:
if metaclass and metaclass.getattr(attrname):
return False
def _check_unused_arguments(self, name, node, stmt, argnames):
is_method = node.is_method()
klass = node.parent.frame()
if is_method and isinstance(klass, astroid.ClassDef):
confidence = (
INFERENCE if utils.has_known_bases(klass) else INFERENCE_FAILURE
)
else:
confidence = HIGH
if is_method:
# Don't warn for the first argument of a (non static) method
if node.type != "staticmethod" and name == argnames[0]:
return
# Don't warn for argument of an overridden method
overridden = overridden_method(klass, node.name)
if overridden is not None and name in overridden.argnames():
return
if node.name in utils.PYMETHODS and node.name not in (
"__init__",
"__new__",
):