本文整理汇总了Python中astroid.Set方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Set方法的具体用法?Python astroid.Set怎么用?Python astroid.Set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def lookup(node, name):
"""Lookup the given special method name in the given *node*
If the special method was found, then a list of attributes
will be returned. Otherwise, `astroid.AttributeInferenceError`
is going to be raised.
"""
if isinstance(node, (astroid.List,
astroid.Tuple,
astroid.Const,
astroid.Dict,
astroid.Set)):
return _builtin_lookup(node, name)
elif isinstance(node, astroid.Instance):
return _lookup_in_mro(node, name)
elif isinstance(node, astroid.ClassDef):
return _class_lookup(node, name)
raise exceptions.AttributeInferenceError(
attribute=name,
target=node
)
示例2: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def visit_starred(self, node):
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, astroid.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if PY35 and isinstance(node.parent,
(astroid.List, astroid.Tuple,
astroid.Set, astroid.Dict)):
# PEP 448 unpacking.
return
stmt = node.statement()
if not isinstance(stmt, astroid.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message('star-needs-assignment-target', node=node)
示例3: _check_using_constant_test
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [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)
示例4: _check_literal_comparison
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def _check_literal_comparison(self, literal, node):
"""Check if we compare to a literal, which is usually what we do not want to do."""
nodes = (astroid.List,
astroid.Tuple,
astroid.Dict,
astroid.Set)
is_other_literal = isinstance(literal, nodes)
is_const = False
if isinstance(literal, astroid.Const):
if literal.value in (True, False, None):
# Not interested in this values.
return
is_const = isinstance(literal.value, (bytes, str, int, float))
if is_const or is_other_literal:
self.add_message('literal-comparison', node=node)
示例5: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def lookup(node, name):
"""Lookup the given special method name in the given *node*
If the special method was found, then a list of attributes
will be returned. Otherwise, `astroid.AttributeInferenceError`
is going to be raised.
"""
if isinstance(
node, (astroid.List, astroid.Tuple, astroid.Const, astroid.Dict, astroid.Set)
):
return _builtin_lookup(node, name)
if isinstance(node, astroid.Instance):
return _lookup_in_mro(node, name)
if isinstance(node, astroid.ClassDef):
return _class_lookup(node, name)
raise exceptions.AttributeInferenceError(attribute=name, target=node)
示例6: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def visit_starred(self, node):
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, astroid.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if PY35 and isinstance(
node.parent, (astroid.List, astroid.Tuple, astroid.Set, astroid.Dict)
):
# PEP 448 unpacking.
return
stmt = node.statement()
if not isinstance(stmt, astroid.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message("star-needs-assignment-target", node=node)
示例7: _check_dangerous_default
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def _check_dangerous_default(self, node):
# check for dangerous default values as arguments
is_iterable = lambda n: isinstance(n, (astroid.List,
astroid.Set,
astroid.Dict))
for default in node.args.defaults:
try:
value = next(default.infer())
except astroid.InferenceError:
continue
if (isinstance(value, astroid.Instance) and
value.qname() in DEFAULT_ARGUMENT_SYMBOLS):
if value is default:
msg = DEFAULT_ARGUMENT_SYMBOLS[value.qname()]
elif isinstance(value, astroid.Instance) or is_iterable(value):
# We are here in the following situation(s):
# * a dict/set/list/tuple call which wasn't inferred
# to a syntax node ({}, () etc.). This can happen
# when the arguments are invalid or unknown to
# the inference.
# * a variable from somewhere else, which turns out to be a list
# or a dict.
if is_iterable(default):
msg = value.pytype()
elif isinstance(default, astroid.Call):
msg = '%s() (%s)' % (value.name, value.qname())
else:
msg = '%s (%s)' % (default.as_string(), value.qname())
else:
# this argument is a name
msg = '%s (%s)' % (default.as_string(),
DEFAULT_ARGUMENT_SYMBOLS[value.qname()])
self.add_message('dangerous-default-value',
node=node,
args=(msg, ))
示例8: set_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def set_node(draw, elt=const_node(), **kwargs):
"""Return a Set node with elements drawn from elt.
"""
node = astroid.Set()
node.postinit(draw(hs.sets(elt, **kwargs)))
return node
示例9: _parse_text
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def _parse_text(source: Union[str, NodeNG], reset: bool = False) -> Tuple[astroid.Module, TypeInferer]:
"""Parse source code text and output an AST with type inference performed."""
# TODO: apparently no literal syntax for empty set in Python3, also cannot do set()
# TODO: Deal with special case later.
# if isinstance(source, astroid.Set) and len(list(source.elts)) == 0:
# source = f'{set({})}'
if not isinstance(source, str): # It's an astroid node
source = source.as_string()
module = astroid.parse(source)
type_inferer = TypeInferer()
if reset:
type_inferer.reset()
type_inferer.environment_transformer().visit(module)
type_inferer.type_inference_transformer().visit(module)
return module, type_inferer
示例10: test_homogeneous_set
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def test_homogeneous_set(node):
"""Test Set nodes representing a set of homogeneous values."""
module, _ = cs._parse_text(node)
set_node = list(module.nodes_of_class(astroid.Set))[0]
if len(set_node.elts) == 0:
assert set_node.inf_type.getValue() == Set[Any]
else:
try:
cs._verify_type_setting(module, astroid.Set, Set[type(set_node.elts[0].value)])
except AttributeError:
cs._verify_type_setting(module, astroid.Set, Set[type(set_node.elts[0].operand.value)])
示例11: test_random_set
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def test_random_set(node):
"""Test Set nodes representing a set of heterogeneous values."""
assume(not isinstance(list(node.elts)[0].value, type(list(node.elts)[1].value)))
assume(not isinstance(list(node.elts)[1].value, type(list(node.elts)[0].value)))
val_types = [type(val.value) for val in node.elts]
if int in val_types:
assume(bool not in val_types)
if bool in val_types:
assume(int not in val_types)
module, _ = cs._parse_text(node)
set_node = list(module.nodes_of_class(astroid.Set))[0]
cs._verify_type_setting(module, astroid.Set, Set[Any])
示例12: _set_start_from_first_child
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def _set_start_from_first_child(node):
"""Set the start attributes of this node from its first child."""
try:
first_child = next(node.get_children())
except StopIteration:
pass
else:
node.fromlineno = first_child.fromlineno
node.col_offset = first_child.col_offset
return node
示例13: visit_set
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def visit_set(self, node: astroid.Set) -> None:
if not node.elts:
node.inf_type = TypeInfo(Set[self.type_constraints.fresh_tvar(node)])
else:
elt_inf_type = self._unify_elements(node.elts, node)
node.inf_type = wrap_container(Set, elt_inf_type)
示例14: visit_setcomp
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def visit_setcomp(self, node: astroid.SetComp) -> None:
elt_inf_type = self.type_constraints.resolve(node.elt.inf_type)
node.inf_type = wrap_container(Set, elt_inf_type)
示例15: _check_collection
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Set [as 别名]
def _check_collection(self, node):
"""
Precondition: node is a condition in an if statement
Returns true if all the node is a structure/collection of values
Returns false otherwise
"""
if isinstance(node, astroid.List) or isinstance(node, astroid.Tuple) or isinstance(node, astroid.Dict) or \
isinstance(node, astroid.Set):
return True