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_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, astroid.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
def _check(self, node):
attrname = node.attrname
klass = node_frame_class(node)
if klass is None or (
attrname not in klass.instance_attrs and
attrname not in (m.name for m in klass.methods())):
old_check_protected_attribute_access(self, node)
def check_functiondef_params(self, node, node_doc):
node_allow_no_param = None
if node.name in self.constructor_names:
class_node = checker_utils.node_frame_class(node)
if class_node is not None:
class_doc = utils.docstringify(
class_node.doc, self.config.default_docstring_type
)
self.check_single_constructor_params(class_doc, node_doc, class_node)
# __init__ or class docstrings can have no parameters documented
# as long as the other documents them.
node_allow_no_param = (
class_doc.has_params()
or class_doc.params_documented_elsewhere()
or None
)
class_allow_no_param = (
node_doc.has_params()
or node_doc.params_documented_elsewhere()
return
if isinstance(klass, objects.Super):
return
try:
del not_called_yet[klass]
except KeyError:
if klass not in to_call:
self.add_message(
"non-parent-init-called", node=expr, args=klass.name
)
except astroid.InferenceError:
continue
for klass, method in not_called_yet.items():
if decorated_with(node, ["typing.overload"]):
continue
cls = node_frame_class(method)
if klass.name == "object" or (cls and cls.name == "object"):
continue
self.add_message("super-init-not-called", args=klass.name, node=node)
def visit_call(self, node):
""" Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
try:
infered = next(node.func.infer())
except astroid.InferenceError:
return
if not isinstance(infered, astroid.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is infered:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
metaclass = infered.metaclass()
abstract_methods = _has_abstract_methods(infered)
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in infered.ancestors():
if ancestor.qname() == 'abc.ABC' and abstract_methods:
self.add_message('abstract-class-instantiated',
property_name = "{}.property".format(BUILTINS)
for attr in attributes:
if attr is astroid.Uninferable:
continue
try:
inferred = next(attr.infer())
except astroid.InferenceError:
continue
if isinstance(inferred, astroid.FunctionDef) and decorated_with_property(
inferred
):
return True
if inferred.pytype() != property_name:
continue
cls = node_frame_class(inferred)
if cls == klass.declared_metaclass():
continue
return True
return False
def _check_protected_attribute_access(self, node):
'''Given an attribute access node (set or get), check if attribute
access is legitimate. Call _check_first_attr with node before calling
this method. Valid cases are:
* self._attr in a method or cls._attr in a classmethod. Checked by
_check_first_attr.
* Klass._attr inside "Klass" class.
* Klass2._attr inside "Klass" class when Klass2 is a base class of
Klass.
'''
attrname = node.attrname
if is_attr_protected(attrname):
klass = node_frame_class(node)
# XXX infer to be more safe and less dirty ??
# in classes, check we are not getting a parent method
# through the class object or through super
callee = node.expr.as_string()
# We are not in a class, no remaining valid case
if klass is None:
self.add_message('W0212', node=node, args=attrname)
return
# We are in a class, one remaining valid cases, Klass._attr inside
# Klass
if not (callee == klass.name or callee in klass.basenames):
self.add_message('W0212', node=node, args=attrname)