本文整理汇总了Python中astroid.Assign方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Assign方法的具体用法?Python astroid.Assign怎么用?Python astroid.Assign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Assign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: attr_attributes_transform
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def attr_attributes_transform(node):
"""Given that the ClassNode has an attr decorator,
rewrite class attributes as instance attributes
"""
# Astroid can't infer this attribute properly
# Prevents https://github.com/PyCQA/pylint/issues/1884
node.locals["__attrs_attrs__"] = [astroid.Unknown(parent=node.body)]
for cdefbodynode in node.body:
if not isinstance(cdefbodynode, astroid.Assign):
continue
if isinstance(cdefbodynode.value, astroid.Call):
if cdefbodynode.value.func.as_string() != ATTR_IB:
continue
else:
continue
for target in cdefbodynode.targets:
rhs_node = astroid.Unknown(
lineno=cdefbodynode.lineno,
col_offset=cdefbodynode.col_offset,
parent=cdefbodynode
)
node.locals[target.name] = [rhs_node]
示例2: clobber_in_except
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def clobber_in_except(node):
"""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(),)))
elif isinstance(node, astroid.AssignName):
name = node.name
if is_builtin(name):
return (True, (name, 'builtins'))
else:
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)
示例3: visit_starred
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [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: test_annassign_subscript_multi_list
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_annassign_subscript_multi_list():
program = """
l1: List
l2: List
l1 = [1, 2, 3]
l2 = ['Hello', 'Goodbye']
"""
module, inferer = cs._parse_text(program)
for ann_node in module.nodes_of_class(astroid.AnnAssign):
variable_type = lookup_type(inferer, ann_node, ann_node.target.name)
assert variable_type == List[Any]
assign_nodes = list(module.nodes_of_class(astroid.Assign))
assign_node_1 = assign_nodes[0]
assign_type_1 = lookup_type(inferer, assign_node_1, assign_node_1.targets[0].name)
assert assign_type_1 == List[Any]
assign_node_2 = assign_nodes[1]
assign_type_2 = lookup_type(inferer, assign_node_2, assign_node_2.targets[0].name)
assert assign_type_2 == List[Any]
示例5: test_assign_complex
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_assign_complex(variables, values):
"""Test whether visitors properly set the type constraint of the a Assign node representing a multi-target-assign
with a homogeneous list as the value.
"""
assume(type(values[0]) != type(values[1]) and len(variables) == len(values))
val_types = [type(val) for val in values]
if int in val_types:
assume(bool not in val_types)
if bool in val_types:
assume(int not in val_types)
program = ("x = ["
+ ", ".join([repr(val) for val in values])
+ "]\n"
+ ", ".join(variables)
+ " = x")
module, TypeInferrer = cs._parse_text(program)
for variable_name in variables:
variable_type_var = module.type_environment.lookup_in_env(variable_name)
assert TypeInferrer.type_constraints.resolve(variable_type_var).getValue() == Any
示例6: test_from_import_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_from_import_function():
src = """
from mod import func
x = func(0, 1)
y = func(0, 1) + 1
undefined_func(0, 1)
"""
ast_mod, ti = cs._parse_text(src, reset=True)
for call_node in ast_mod.nodes_of_class(astroid.Call):
if call_node.func.name == 'func1':
assert call_node.inf_type.getValue() == Any
if call_node.func.name == 'func2':
assert isinstance(call_node.inf_type, TypeFail)
for assgn_node in ast_mod.nodes_of_class(astroid.Assign):
if assgn_node.targets[0].name == 'x':
x = ti.lookup_typevar(assgn_node, 'x')
assert ti.type_constraints.resolve(x).getValue() == Any
if assgn_node.targets[0].name == 'y':
y = ti.lookup_typevar(assgn_node, 'y')
assert ti.type_constraints.resolve(y).getValue() == int
示例7: test_subscript_slice
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_subscript_slice():
program = '''
x = List[:]
'''
module, _ = cs._parse_text(program)
assign_node = next(module.nodes_of_class(astroid.Assign))
assert isinstance(assign_node.inf_type, TypeFail)
# TODO: this test needs to be converted, but will also fail
# @given(cs.random_list(min_size=2), cs.random_slice_indices())
# def test_subscript_heterogeneous_list_slice(input_list, slice):
# """Test visitor of Subscript node representing slicing of heterogeneous list."""
# assume(not isinstance(input_list[0], type(input_list[1])))
# input_slice = ':'.join([str(index) if index else '' for index in slice])
# program = f'{input_list}[{input_slice}]'
# module, _ = cs._parse_text(program)
# subscript_node = list(module.nodes_of_class(astroid.Subscript))[0]
# assert subscript_node.inf_type.getValue() == List[Any]
示例8: test_message_simple
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_message_simple(self):
src = """
x = 10
x = 230
print(x)
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_1, _ = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertAddsMessages(
pylint.testutils.Message(
msg_id='redundant-assignment',
node=assign_1,
),
):
self.checker.visit_assign(assign_1)
示例9: test_message_if_stmt
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_message_if_stmt(self):
src = """
x = 10
y = 5
if y > 5:
x = 20
else:
x = 15
print(x)
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_x, *_ = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertAddsMessages(
pylint.testutils.Message(
msg_id='redundant-assignment',
node=assign_x,
),
):
self.checker.visit_assign(assign_x)
示例10: test_message_scope
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_message_scope(self):
src = """
x = 25
def func():
def func2():
print(x - 1)
func2()
x = 10
func()
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_x, *_ = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertAddsMessages(
pylint.testutils.Message(
msg_id='redundant-assignment',
node=assign_x,
)
):
self.checker.visit_assign(assign_x)
示例11: test_no_message_loop_complex
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_no_message_loop_complex(self):
src = """
x = 10
for y in range(1, 10):
x = func(y)
print(x)
x = x - 1
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_x1, assign_x2, assign_x3 = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertNoMessages():
self.checker.visit_assign(assign_x1)
self.checker.visit_assign(assign_x2)
self.checker.visit_assign(assign_x3)
示例12: test_no_message_if_complex
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_no_message_if_complex(self):
src = """
x = 10
y = 5
if y > 5:
x = 20
elif y > 50:
x = 15
else:
pass
print(x)
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_x, *_ = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertNoMessages():
self.checker.visit_assign(assign_x)
示例13: test_no_message_function_def
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_no_message_function_def(self):
src = """
x = 10
if False:
x = 20
else:
def func():
x = 1
print(x)
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
assign_x, *_ = mod.nodes_of_class(astroid.Assign)
self.checker.visit_module(mod)
with self.assertNoMessages():
self.checker.visit_assign(assign_x)
示例14: test_augassign_multiple_no_message
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def test_augassign_multiple_no_message(self):
src = """
y_pos = 5
y_pos += 10
y_pos += 10
y_pos += 10
y_pos += 10
"""
mod = astroid.parse(src)
mod.accept(CFGVisitor())
self.checker.visit_module(mod)
with self.assertNoMessages():
for node in mod.nodes_of_class(astroid.Assign):
self.checker.visit_assign(node)
for node in mod.nodes_of_class(astroid.AugAssign):
self.checker.visit_augassign(node)
示例15: attr_attributes_transform
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Assign [as 别名]
def attr_attributes_transform(node):
"""Given that the ClassNode has an attr decorator,
rewrite class attributes as instance attributes
"""
# Astroid can't infer this attribute properly
# Prevents https://github.com/PyCQA/pylint/issues/1884
node.locals["__attrs_attrs__"] = [astroid.Unknown(parent=node)]
for cdefbodynode in node.body:
if not isinstance(cdefbodynode, astroid.Assign):
continue
if isinstance(cdefbodynode.value, astroid.Call):
if cdefbodynode.value.func.as_string() not in ATTRIB_NAMES:
continue
else:
continue
for target in cdefbodynode.targets:
rhs_node = astroid.Unknown(
lineno=cdefbodynode.lineno,
col_offset=cdefbodynode.col_offset,
parent=cdefbodynode,
)
node.locals[target.name] = [rhs_node]