當前位置: 首頁>>代碼示例>>Python>>正文


Python astroid.Assign方法代碼示例

本文整理匯總了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] 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:26,代碼來源:brain_attrs.py

示例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) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:22,代碼來源:utils.py

示例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) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:20,代碼來源:base.py

示例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] 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:25,代碼來源:test_annassign.py

示例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 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:21,代碼來源:test_assign.py

示例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 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:23,代碼來源:test_imports.py

示例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] 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:21,代碼來源:test_subscript.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:20,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:25,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:24,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:19,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:21,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:19,代碼來源:test_redundant_assignment_checker.py

示例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) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:19,代碼來源:test_redundant_assignment_checker.py

示例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] 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:26,代碼來源:brain_attrs.py


注:本文中的astroid.Assign方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。