本文整理汇总了Python中pylint.checkers.utils.is_builtin_object函数的典型用法代码示例。如果您正苦于以下问题:Python is_builtin_object函数的具体用法?Python is_builtin_object怎么用?Python is_builtin_object使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_builtin_object函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _check_reversed
def _check_reversed(self, node):
""" check that the argument to `reversed` is a sequence """
try:
argument = safe_infer(get_argument_from_call(node, position=0))
except NoSuchArgumentError:
self.add_message('missing-reversed-argument', node=node)
else:
if argument is astroid.YES:
return
if argument is None:
# nothing was infered
# try to see if we have iter()
if isinstance(node.args[0], astroid.CallFunc):
try:
func = node.args[0].func.infer().next()
except InferenceError:
return
if (getattr(func, 'name', None) == 'iter' and
is_builtin_object(func)):
self.add_message('bad-reversed-sequence', node=node)
return
if isinstance(argument, astroid.Instance):
if (argument._proxied.name == 'dict' and
is_builtin_object(argument._proxied)):
self.add_message('bad-reversed-sequence', node=node)
return
elif any(ancestor.name == 'dict' and is_builtin_object(ancestor)
for ancestor in argument._proxied.ancestors()):
# mappings aren't accepted by reversed()
self.add_message('bad-reversed-sequence', node=node)
return
for methods in REVERSED_METHODS:
for meth in methods:
try:
argument.getattr(meth)
except astroid.NotFoundError:
break
else:
break
else:
# check if it is a .deque. It doesn't seem that
# we can retrieve special methods
# from C implemented constructs
if argument._proxied.qname().endswith(".deque"):
return
self.add_message('bad-reversed-sequence', node=node)
elif not isinstance(argument, (astroid.List, astroid.Tuple)):
# everything else is not a proper sequence for reversed()
self.add_message('bad-reversed-sequence', node=node)
示例2: visit_name
def visit_name(self, node):
"""Detect when a "bad" built-in is referenced."""
node_infer = utils.safe_infer(node)
if not utils.is_builtin_object(node_infer):
# Skip not builtin objects
return
if node_infer.name == 'eval':
self.add_message('eval-referenced', node=node)
示例3: _check_protected_attribute_access
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) and
attrname not in self.config.exclude_protected):
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('protected-access', node=node, args=attrname)
return
# If the expression begins with a call to super, that's ok.
if isinstance(node.expr, astroid.CallFunc) and \
isinstance(node.expr.func, astroid.Name) and \
node.expr.func.name == 'super':
return
# We are in a class, one remaining valid cases, Klass._attr inside
# Klass
if not (callee == klass.name or callee in klass.basenames):
# Detect property assignments in the body of the class.
# This is acceptable:
#
# class A:
# b = property(lambda: self._b)
stmt = node.parent.statement()
try:
if (isinstance(stmt, astroid.Assign) and
(stmt in klass.body or klass.parent_of(stmt)) and
isinstance(stmt.value, astroid.CallFunc) and
isinstance(stmt.value.func, astroid.Name) and
stmt.value.func.name == 'property' and
is_builtin_object(next(stmt.value.func.infer(), None))):
return
except astroid.InferenceError:
pass
self.add_message('protected-access', node=node, args=attrname)
示例4: _is_invalid_metaclass
def _is_invalid_metaclass(metaclass):
try:
mro = metaclass.mro()
except NotImplementedError:
# Cannot have a metaclass which is not a newstyle class.
return True
else:
if not any(is_builtin_object(cls) and cls.name == 'type'
for cls in mro):
return True
return False
示例5: visit_callfunc
def visit_callfunc(self, node):
"""visit a CallFunc node, check if map or filter are called with a
lambda
"""
if not node.args:
return
if not isinstance(node.args[0], astroid.Lambda):
return
infered = safe_infer(node.func)
if (is_builtin_object(infered)
and infered.name in ['map', 'filter']):
self.add_message('deprecated-lambda', node=node)
示例6: visit_call
def visit_call(self, node):
node_infer = utils.safe_infer(node.func)
if utils.is_builtin_object(node_infer) and \
self.get_func_name(node.func) == 'format':
self.add_message('prefer-other-formatting', node=node)
if 'fields' == self.get_func_lib(node.func) and \
isinstance(node.parent, astroid.Assign) and \
isinstance(node.parent.parent, astroid.ClassDef):
has_help = False
args = misc.join_node_args_kwargs(node)
for argument in args:
argument_aux = argument
if isinstance(argument, astroid.Keyword):
argument_aux = argument.value
if argument.arg in ['compute', 'search', 'inverse'] and \
isinstance(argument.value, astroid.Const) and \
argument_aux.value and \
not argument_aux.value.startswith(
'_' + argument.arg + '_'):
self.add_message('method-' + argument.arg,
node=argument_aux)
elif argument.arg == 'help':
has_help = True
elif argument.arg == 'selection_add':
# The argument "selection_add" is for overwrite field.
# Then don't need help.
has_help = None
if isinstance(argument_aux, astroid.CallFunc) and \
isinstance(argument_aux.func, astroid.Name) and \
argument_aux.func.name == '_':
self.add_message('translation-field', node=argument_aux)
if has_help is False:
self.add_message('consider-add-field-help', node=node)
# Check cr.commit()
if isinstance(node, astroid.CallFunc) and \
isinstance(node.func, astroid.Getattr) and \
node.func.attrname == 'commit' and \
self.get_cursor_name(node.func) in self.config.cursor_expr:
self.add_message('invalid-commit', node=node)
# SQL Injection
if isinstance(node, astroid.CallFunc) and node.args and \
isinstance(node.func, astroid.Getattr) and \
node.func.attrname == 'execute' and \
self.get_cursor_name(node.func) in self.config.cursor_expr:
first_arg = node.args[0]
is_bin_op = isinstance(first_arg, astroid.BinOp) and \
first_arg.op == '%'
is_format = isinstance(first_arg, astroid.CallFunc) and \
self.get_func_name(first_arg.func) == 'format'
if is_bin_op or is_format:
self.add_message('sql-injection', node=node)
示例7: _check_init
def _check_init(self, node):
"""check that the __init__ method call super or ancestors'__init__
method
"""
if (not self.linter.is_message_enabled('super-init-not-called') and
not self.linter.is_message_enabled('non-parent-init-called')):
return
klass_node = node.parent.frame()
to_call = _ancestors_to_call(klass_node)
not_called_yet = dict(to_call)
for stmt in node.nodes_of_class(astroid.Call):
expr = stmt.func
if not isinstance(expr, astroid.Attribute) \
or expr.attrname != '__init__':
continue
# skip the test if using super
if isinstance(expr.expr, astroid.Call) and \
isinstance(expr.expr.func, astroid.Name) and \
expr.expr.func.name == 'super':
return
try:
for klass in expr.expr.infer():
if klass is astroid.YES:
continue
# The infered klass can be super(), which was
# assigned to a variable and the `__init__`
# was called later.
#
# base = super()
# base.__init__(...)
if (isinstance(klass, astroid.Instance) and
isinstance(klass._proxied, astroid.ClassDef) and
is_builtin_object(klass._proxied) and
klass._proxied.name == 'super'):
return
elif 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 six.iteritems(not_called_yet):
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)
示例8: _duplicated_isinstance_types
def _duplicated_isinstance_types(node):
"""Get the duplicated types from the underlying isinstance calls.
:param astroid.BoolOp node: Node which should contain a bunch of isinstance calls.
:returns: Dictionary of the comparison objects from the isinstance calls,
to duplicate values from consecutive calls.
:rtype: dict
"""
duplicated_objects = set()
all_types = collections.defaultdict(set)
for call in node.values:
if not isinstance(call, astroid.Call) or len(call.args) != 2:
continue
inferred = utils.safe_infer(call.func)
if not inferred or not utils.is_builtin_object(inferred):
continue
if inferred.name != 'isinstance':
continue
isinstance_object = call.args[0].as_string()
isinstance_types = call.args[1]
if isinstance_object in all_types:
duplicated_objects.add(isinstance_object)
if isinstance(isinstance_types, astroid.Tuple):
elems = [class_type.as_string() for class_type in isinstance_types.itered()]
else:
elems = [isinstance_types.as_string()]
all_types[isinstance_object].update(elems)
# Remove all keys which not duplicated
return {key: value for key, value in all_types.items()
if key in duplicated_objects}
示例9: _is_invalid_base_class
def _is_invalid_base_class(cls):
return cls.name in INVALID_BASE_CLASSES and is_builtin_object(cls)
示例10: _is_builtin
def _is_builtin(node, function):
inferred = utils.safe_infer(node)
if not inferred:
return False
return utils.is_builtin_object(inferred) and inferred.name == function
示例11: visit_function
def visit_function(self, node):
"""Called for every function definition in the source code."""
# ignore actual functions
if not node.is_method():
return
method_name = node.name
if method_name not in self.METHOD_NAMES:
return
klass_node = node.parent.frame()
to_call = _ancestors_to_call(klass_node, method_name)
not_called_yet = dict(to_call)
for stmt in node.nodes_of_class(astroid.CallFunc):
expr = stmt.func
if not isinstance(expr, astroid.Getattr):
continue
if expr.attrname != method_name:
continue
# Skip the test if using super
if (isinstance(expr.expr, astroid.CallFunc) and
isinstance(expr.expr.func, astroid.Name) and
expr.expr.func.name == 'super'):
return
try:
klass = next(expr.expr.infer())
if klass is astroid.YES:
continue
# The infered klass can be super(), which was
# assigned to a variable and the `__init__` was called later.
#
# base = super()
# base.__init__(...)
# pylint: disable=protected-access
if (isinstance(klass, astroid.Instance) and
isinstance(klass._proxied, astroid.Class) and
utils.is_builtin_object(klass._proxied) and
klass._proxied.name == 'super'):
return
try:
del not_called_yet[klass]
except KeyError:
if klass not in to_call:
self.add_message(
self.NON_PARENT_MESSAGE_ID,
node=expr,
args=(method_name, klass.name),
)
except astroid.InferenceError:
continue
for klass, method in six.iteritems(not_called_yet):
if klass.name == 'object' or method.parent.name == 'object':
continue
self.add_message(
self.NOT_CALLED_MESSAGE_ID,
args=(method_name, klass.name),
node=node,
)