本文整理汇总了Python中astroid.Tuple方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Tuple方法的具体用法?Python astroid.Tuple怎么用?Python astroid.Tuple使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Tuple方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [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: _annotated_unpack_infer
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def _annotated_unpack_infer(stmt, context=None):
"""
Recursively generate nodes inferred by the given statement.
If the inferred value is a list or a tuple, recurse on the elements.
Returns an iterator which yields tuples in the format
('original node', 'infered node').
"""
if isinstance(stmt, (astroid.List, astroid.Tuple)):
for elt in stmt.elts:
inferred = utils.safe_infer(elt)
if inferred and inferred is not astroid.YES:
yield elt, inferred
return
for infered in stmt.infer(context):
if infered is astroid.YES:
continue
yield stmt, infered
示例3: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [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)
示例4: _check_literal_comparison
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [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: _get_tuple_targets
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def _get_tuple_targets(self, t: astroid.Tuple) -> List[type]:
target_tvars = []
for subtarget in t.elts:
if isinstance(subtarget, astroid.AssignAttr):
target_tvars.append(self._lookup_attribute_type(subtarget, subtarget.expr.inf_type, subtarget.attrname))
elif isinstance(subtarget, astroid.Starred):
if isinstance(subtarget.value, astroid.AssignAttr):
target_tvars.append(self.lookup_typevar(subtarget.value, subtarget.value.attrname))
else:
target_tvars.append(self.lookup_typevar(subtarget.value, subtarget.value.name))
elif isinstance(subtarget, astroid.Subscript):
target_tvars.append(self._handle_call(subtarget, '__getitem__', subtarget.value.inf_type,
subtarget.slice.inf_type))
else:
target_tvars.append(self.lookup_typevar(subtarget, subtarget.name))
return target_tvars
示例6: visit_subscript
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def visit_subscript(self, node: astroid.Subscript) -> None:
if isinstance(node.slice.inf_type, TypeFail):
node.inf_type = node.slice.inf_type
elif node.ctx == astroid.Load:
try:
val_inf_type = self.type_constraints.resolve(node.value.inf_type)
value_gorg = val_inf_type >> _gorg
except AttributeError:
value_gorg = None
if value_gorg is type and isinstance(node.slice, astroid.Index):
if isinstance(node.slice.value, astroid.Tuple):
node.inf_type = wrap_container(_node_to_type(node.value), *_node_to_type(node.slice.value))
else:
node.inf_type = wrap_container(_node_to_type(node.value), _node_to_type(node.slice.value))
else:
node.inf_type = self._handle_call(node, '__getitem__', node.value.inf_type, node.slice.inf_type)
elif node.ctx == astroid.Store:
node.inf_type = NoType()
elif node.ctx == astroid.Del:
node.inf_type = self._handle_call(node, '__delitem__', node.value.inf_type, node.slice.inf_type)
##############################################################################
# Loops
##############################################################################
示例7: _generic_to_annotation
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def _generic_to_annotation(ann_node_type: type, node: NodeNG) -> TypeResult:
if (isinstance(ann_node_type, _GenericAlias) and
ann_node_type is getattr(typing, getattr(ann_node_type, '_name', '') or '', None)):
if ann_node_type == Dict:
ann_type = wrap_container(ann_node_type, Any, Any)
elif ann_node_type == Tuple:
# TODO: Add proper support for multi-parameter Tuples
ann_type = wrap_container(ann_node_type, Any)
else:
ann_type = wrap_container(ann_node_type, Any)
elif isinstance(ann_node_type, _GenericAlias):
parsed_args = []
for arg in ann_node_type.__args__:
_generic_to_annotation(arg, node) >> parsed_args.append
ann_type = wrap_container(ann_node_type, *parsed_args)
else:
try:
_type_check(ann_node_type, '')
except TypeError:
return TypeFailAnnotationInvalid(node)
else:
ann_type = TypeInfo(ann_node_type)
return ann_type
示例8: numpy_function_infer_call_result
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def numpy_function_infer_call_result(node):
"""
A wrapper around infer_call_result method bounded to the node.
:param node: the node which infer_call_result should be filtered
:type node: FunctionDef
:return: a function that filter the results of the call to node.infer_call_result
:rtype: function
"""
# Put the origin infer_call_result method into the closure
origin_infer_call_result = node.infer_call_result
def infer_call_result_wrapper(caller=None, context=None):
"""
Call the origin infer_call_result method bounded to the node instance and
filter the results to remove List and Tuple instances
"""
unfiltered_infer_call_result = origin_infer_call_result(caller, context)
return (
x
for x in unfiltered_infer_call_result
if not isinstance(x, (astroid.List, astroid.Tuple))
)
return infer_call_result_wrapper
示例9: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [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)
示例10: _annotated_unpack_infer
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def _annotated_unpack_infer(stmt, context=None):
"""
Recursively generate nodes inferred by the given statement.
If the inferred value is a list or a tuple, recurse on the elements.
Returns an iterator which yields tuples in the format
('original node', 'infered node').
"""
if isinstance(stmt, (astroid.List, astroid.Tuple)):
for elt in stmt.elts:
inferred = utils.safe_infer(elt)
if inferred and inferred is not astroid.Uninferable:
yield elt, inferred
return
for infered in stmt.infer(context):
if infered is astroid.Uninferable:
continue
yield stmt, infered
示例11: clobber_in_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def clobber_in_except(
node: astroid.node_classes.NodeNG
) -> Tuple[bool, Tuple[str, str]]:
"""Checks if an assignment node in an except handler clobbers an existing
variable.
Returns (True, args for W0623) if assignment clobbers an existing variable,
(False, None) otherwise.
"""
if isinstance(node, astroid.AssignAttr):
return True, (node.attrname, "object %r" % (node.expr.as_string(),))
if isinstance(node, astroid.AssignName):
name = node.name
if is_builtin(name):
return (True, (name, "builtins"))
stmts = node.lookup(name)[1]
if stmts and not isinstance(
stmts[0].assign_type(),
(astroid.Assign, astroid.AugAssign, astroid.ExceptHandler),
):
return True, (name, "outer scope (line %s)" % stmts[0].fromlineno)
return False, None
示例12: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [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)
示例13: visit_assign
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def visit_assign(self, node):
"""Check unbalanced tuple unpacking for assignments
and unpacking non-sequences as well as in case self/cls
get assigned.
"""
self._check_self_cls_assign(node)
if not isinstance(node.targets[0], (astroid.Tuple, astroid.List)):
return
targets = node.targets[0].itered()
try:
infered = utils.safe_infer(node.value)
if infered is not None:
self._check_unpacking(infered, node, targets)
except astroid.InferenceError:
return
示例14: visit_assign
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
def visit_assign(self, node):
# we don't handle multiple assignment nor slice assignment
target = node.targets[0]
if isinstance(target, (astroid.Tuple, astroid.Subscript)):
return
# ignore NoneType
if is_none(node):
return
_type = node_type(node.value)
if _type:
self._assigns[-1].setdefault(target.as_string(), []).append(
(node, _type.pytype()))
示例15: _check_catching_non_exception
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Tuple [as 别名]
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.YES 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 infered 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
# emit a warning.
self.add_message('catching-non-exception',
node=handler.type,
args=(part.as_string(), ))
else:
self.add_message('catching-non-exception',
node=handler.type,
args=(part.as_string(), ))
return
if (not utils.inherit_from_std_ex(exc) and
exc.name not in self._builtin_exceptions):
if utils.has_known_bases(exc):
self.add_message('catching-non-exception',
node=handler.type,
args=(exc.name, ))