本文整理汇总了Python中astroid.Attribute方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Attribute方法的具体用法?Python astroid.Attribute怎么用?Python astroid.Attribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Attribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_setters_property_name
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def get_setters_property_name(node):
"""Get the name of the property that the given node is a setter for.
:param node: The node to get the property name for.
:type node: str
:rtype: str or None
:returns: The name of the property that the node is a setter for,
or None if one could not be found.
"""
decorators = node.decorators.nodes if node.decorators else []
for decorator in decorators:
if (isinstance(decorator, astroid.Attribute) and
decorator.attrname == "setter" and
isinstance(decorator.expr, astroid.Name)):
return decorator.expr.name
return None
示例2: _check_using_constant_test
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _check_using_constant_test(self, node, test):
const_nodes = (
astroid.Module,
astroid.scoped_nodes.GeneratorExp,
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
# Attribute, 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 = utils.safe_infer(test)
if emit or isinstance(inferred, const_nodes):
self.add_message('using-constant-test', node=node)
示例3: test_classdef_method_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def test_classdef_method_call():
"""Test whether type of the method call are properly being set"""
program = f'class Network:\n' \
f' def __init__(self, name):\n' \
f' self.name = name\n' \
f' def get_name(self):\n' \
f' return self.name\n ' \
f'\n' \
f'rogers = Network("Rogers")\n' \
f'rogers.get_name()' \
f'\n'
module, inferer = cs._parse_text(program, True)
attribute_node = list(module.nodes_of_class(astroid.Attribute))[1]
expected_rtype = attribute_node.parent.inf_type.getValue()
actual_rtype = inferer.type_constraints.resolve(attribute_node.inf_type.getValue().__args__[-1]).getValue()
assert actual_rtype.__name__ == expected_rtype.__name__
示例4: get_setters_property_name
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def get_setters_property_name(node):
"""Get the name of the property that the given node is a setter for.
:param node: The node to get the property name for.
:type node: str
:rtype: str or None
:returns: The name of the property that the node is a setter for,
or None if one could not be found.
"""
decorators = node.decorators.nodes if node.decorators else []
for decorator in decorators:
if (
isinstance(decorator, astroid.Attribute)
and decorator.attrname == "setter"
and isinstance(decorator.expr, astroid.Name)
):
return decorator.expr.name
return None
示例5: _is_dataclass
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _is_dataclass(node: astroid.ClassDef) -> bool:
"""Check if a class definition defines a Python 3.7+ dataclass
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents a dataclass class. False otherwise.
:rtype: bool
"""
if not node.decorators:
return False
root_locals = node.root().locals
for decorator in node.decorators.nodes:
if isinstance(decorator, astroid.Call):
decorator = decorator.func
if not isinstance(decorator, (astroid.Name, astroid.Attribute)):
continue
if isinstance(decorator, astroid.Name):
name = decorator.name
else:
name = decorator.attrname
if name == DATACLASS_DECORATOR and DATACLASS_DECORATOR in root_locals:
return True
return False
示例6: redefined_by_decorator
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def redefined_by_decorator(node):
"""return True if the object is a method redefined via decorator.
For example:
@property
def x(self): return self._x
@x.setter
def x(self, value): self._x = value
"""
if node.decorators:
for decorator in node.decorators.nodes:
if (
isinstance(decorator, astroid.Attribute)
and getattr(decorator.expr, "name", None) == node.name
):
return True
return False
示例7: _check_misplaced_format_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _check_misplaced_format_function(self, call_node):
if not isinstance(call_node.func, astroid.Attribute):
return
if call_node.func.attrname != "format":
return
expr = utils.safe_infer(call_node.func.expr)
if expr is astroid.Uninferable:
return
if not expr:
# we are doubtful on inferred type of node, so here just check if format
# was called on print()
call_expr = call_node.func.expr
if not isinstance(call_expr, astroid.Call):
return
if (
isinstance(call_expr.func, astroid.Name)
and call_expr.func.name == "print"
):
self.add_message("misplaced-format-function", node=call_node)
示例8: _check_cmp_argument
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _check_cmp_argument(self, node):
# Check that the `cmp` argument is used
kwargs = []
if isinstance(node.func, astroid.Attribute) and node.func.attrname == "sort":
inferred = utils.safe_infer(node.func.expr)
if not inferred:
return
builtins_list = "{}.list".format(bases.BUILTINS)
if isinstance(inferred, astroid.List) or inferred.qname() == builtins_list:
kwargs = node.keywords
elif isinstance(node.func, astroid.Name) and node.func.name == "sorted":
inferred = utils.safe_infer(node.func)
if not inferred:
return
builtins_sorted = "{}.sorted".format(bases.BUILTINS)
if inferred.qname() == builtins_sorted:
kwargs = node.keywords
for kwarg in kwargs or []:
if kwarg.arg == "cmp":
self.add_message("using-cmp-argument", node=node)
return
示例9: _is_pathlib_write
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _is_pathlib_write(expr) -> bool:
if not isinstance(expr, astroid.Call):
return False
if not isinstance(expr.func, astroid.Attribute):
return False
if expr.func.attrname not in ('write_text', 'write_bytes', 'open'):
return False
# if it's open, check that mode is "w"
if expr.func.attrname == 'open':
if not _is_open_to_write(expr):
return False
for value in infer(expr.func.expr):
if isinstance(value, astroid.Instance):
if value.pytype().startswith('pathlib.'):
return True
return False
示例10: get_name
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def get_name(expr) -> Optional[str]:
if isinstance(expr, ast.Name):
return expr.id
if isinstance(expr, astroid.Name):
return expr.name
if isinstance(expr, astroid.Attribute):
left = get_name(expr.expr)
if left is None:
return None
return left + '.' + expr.attrname
if isinstance(expr, ast.Attribute):
left = get_name(expr.value)
if left is None:
return None
return left + '.' + expr.attr
return None
示例11: visit_attribute
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def visit_attribute(self, node):
"""Visit a getattr node."""
# At the end of a config.val.foo.bar chain
if not isinstance(node.parent, astroid.Attribute):
# FIXME:conf do some proper check for this...
node_str = node.as_string()
prefix = 'config.val.'
if node_str.startswith(prefix):
self._check_config(node, node_str[len(prefix):])
示例12: _looks_like_random_sample
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
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
示例13: is_registered_in_singledispatch_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def is_registered_in_singledispatch_function(node):
"""Check if the given function node is a singledispatch function."""
singledispatch_qnames = (
'functools.singledispatch',
'singledispatch.singledispatch'
)
if not isinstance(node, astroid.FunctionDef):
return False
decorators = node.decorators.nodes if node.decorators else []
for decorator in decorators:
# func.register are function calls
if not isinstance(decorator, astroid.Call):
continue
func = decorator.func
if not isinstance(func, astroid.Attribute) or func.attrname != 'register':
continue
try:
func_def = next(func.expr.infer())
except astroid.InferenceError:
continue
if isinstance(func_def, astroid.FunctionDef):
return decorated_with(func_def, singledispatch_qnames)
return False
示例14: _check_classdef_metaclasses
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _check_classdef_metaclasses(self, klass, parent_node):
if not klass._metaclass:
# Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors
return []
consumed = [] # [(scope_locals, consumed_key)]
metaclass = klass.metaclass()
name = None
if isinstance(klass._metaclass, astroid.Name):
name = klass._metaclass.name
elif metaclass:
name = metaclass.root().name
found = None
if name:
# check enclosing scopes starting from most local
for scope_locals, _, _ in self._to_consume[::-1]:
found = scope_locals.get(name)
if found:
consumed.append((scope_locals, name))
break
if found is None and not metaclass:
name = None
if isinstance(klass._metaclass, astroid.Name):
name = klass._metaclass.name
elif isinstance(klass._metaclass, astroid.Attribute):
name = klass._metaclass.as_string()
if name is not None:
if not (name in astroid.Module.scope_attrs or
utils.is_builtin(name) or
name in self.config.additional_builtins or
name in parent_node.locals):
self.add_message('undefined-variable',
node=klass,
args=(name,))
return consumed
示例15: _determine_function_name_type
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Attribute [as 别名]
def _determine_function_name_type(node, config=None):
"""Determine the name type whose regex the a function's name should match.
:param node: A function node.
:type node: astroid.node_classes.NodeNG
:param config: Configuration from which to pull additional property classes.
:type config: :class:`optparse.Values`
:returns: One of ('function', 'method', 'attr')
:rtype: str
"""
property_classes, property_names = _get_properties(config)
if not node.is_method():
return 'function'
if node.decorators:
decorators = node.decorators.nodes
else:
decorators = []
for decorator in decorators:
# If the function is a property (decorated with @property
# or @abc.abstractproperty), the name type is 'attr'.
if (isinstance(decorator, astroid.Name) or
(isinstance(decorator, astroid.Attribute) and
decorator.attrname in property_names)):
infered = utils.safe_infer(decorator)
if infered and infered.qname() in property_classes:
return 'attr'
# If the function is decorated using the prop_method.{setter,getter}
# form, treat it like an attribute as well.
elif (isinstance(decorator, astroid.Attribute) and
decorator.attrname in ('setter', 'deleter')):
return 'attr'
return 'method'