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


Python astroid.AssignName方法代碼示例

本文整理匯總了Python中astroid.AssignName方法的典型用法代碼示例。如果您正苦於以下問題:Python astroid.AssignName方法的具體用法?Python astroid.AssignName怎麽用?Python astroid.AssignName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在astroid的用法示例。


在下文中一共展示了astroid.AssignName方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: clobber_in_except

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [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

示例2: visit_for

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def visit_for(self, node):
        assigned_to = [var.name for var in node.target.nodes_of_class(astroid.AssignName)]

        # Only check variables that are used
        dummy_rgx = self.config.dummy_variables_rgx
        assigned_to = [var for var in assigned_to if not dummy_rgx.match(var)]

        for variable in assigned_to:
            for outer_for, outer_variables in self._loop_variables:
                if (variable in outer_variables
                        and not in_for_else_branch(outer_for, node)):
                    self.add_message(
                        'redefined-outer-name',
                        args=(variable, outer_for.fromlineno),
                        node=node
                    )
                    break

        self._loop_variables.append((node, assigned_to)) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:21,代碼來源:variables.py

示例3: _check_redefined_argument_from_local

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def _check_redefined_argument_from_local(self, name_node):
        if self._dummy_rgx and self._dummy_rgx.match(name_node.name):
            return
        if not name_node.lineno:
            # Unknown position, maybe it is a manually built AST?
            return

        scope = name_node.scope()
        if not isinstance(scope, astroid.FunctionDef):
            return

        for defined_argument in scope.args.nodes_of_class(astroid.AssignName):
            if defined_argument.name == name_node.name:
                self.add_message('redefined-argument-from-local',
                                 node=name_node,
                                 args=(name_node.name, )) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:18,代碼來源:refactoring.py

示例4: _redefines_import

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def _redefines_import(node):
    """ Detect that the given node (AssignName) is inside an
    exception handler and redefines an import from the tryexcept body.
    Returns True if the node redefines an import, False otherwise.
    """
    current = node
    while current and not isinstance(current.parent, astroid.ExceptHandler):
        current = current.parent
    if not current or not utils.error_of_type(current.parent, ImportError):
        return False
    try_block = current.parent.parent
    for import_node in try_block.nodes_of_class((astroid.ImportFrom, astroid.Import)):
        for name, alias in import_node.names:
            if alias:
                if alias == node.name:
                    return True
            elif name == node.name:
                return True
    return False 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:21,代碼來源:base.py

示例5: visit_with

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def visit_with(self, node):
        if not PY3K:
            # in Python 2 a "with" statement with multiple managers coresponds
            # to multiple nested AST "With" nodes
            pairs = []
            parent_node = node.parent
            if isinstance(parent_node, astroid.With):
                # we only care about the direct parent, since this method
                # gets called for each with node anyway
                pairs.extend(parent_node.items)
            pairs.extend(node.items)
        else:
            # in PY3K a "with" statement with multiple managers coresponds
            # to one AST "With" node with multiple items
            pairs = node.items
        if pairs:
            for prev_pair, pair in zip(pairs, pairs[1:]):
                if (isinstance(prev_pair[1], astroid.AssignName) and
                        (pair[1] is None and not isinstance(pair[0], astroid.Call))):
                    # don't emit a message if the second is a function call
                    # there's no way that can be mistaken for a name assignment
                    if PY3K or node.lineno == node.parent.lineno:
                        # if the line number doesn't match
                        # we assume it's a nested "with"
                        self.add_message('confusing-with-statement', node=node) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:27,代碼來源:base.py

示例6: test_userdefn_mro_simple

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_userdefn_mro_simple(draw=False):
    src = """
    class A:
        def foo(self):
            return 0
            
    class B(A):
        pass
        
    b = B()
    x = b.foo()
    """
    ast_mod, ti = cs._parse_text(src, reset=True)
    _, b, x = [ti.lookup_typevar(node, node.name) for node
               in ast_mod.nodes_of_class(astroid.AssignName)]
    assert ti.type_constraints.resolve(x).getValue() == int
    if draw:
        gen_graph_from_nodes(ti.type_constraints._nodes) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:20,代碼來源:test_tnode_structure.py

示例7: test_userdefn_mro_multilevel

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_userdefn_mro_multilevel(draw=False):
    src = """
    class A:
        def foo(self):
            return 0
            
    class B:
        pass
        
    class C(A):
        pass
        
    class D(B, C):
        pass
        
    d = D()
    x = d.foo()
    """
    ast_mod, ti = cs._parse_text(src, reset=True)
    _, d, x = [ti.lookup_typevar(node, node.name) for node
               in ast_mod.nodes_of_class(astroid.AssignName)]
    assert ti.type_constraints.resolve(x).getValue() == int
    if draw:
        gen_graph_from_nodes(ti.type_constraints._nodes) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:26,代碼來源:test_tnode_structure.py

示例8: test_userdefn_mro_diamond

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_userdefn_mro_diamond(draw=False):
    src = """
    class A:
        pass
            
    class B(A):
        def foo(self):
            return 'a'
        
    class C(A):
        def foo(self):
            return 0
        
    class D(B,C):
        pass
        
    d = D()
    x = d.foo() # this is a call to B.foo()
    """
    ast_mod, ti = cs._parse_text(src, reset=True)
    _, _, d, x = [ti.lookup_typevar(node, node.name) for node
                  in ast_mod.nodes_of_class(astroid.AssignName)]
    assert ti.type_constraints.resolve(x).getValue() == str
    if draw:
        gen_graph_from_nodes(ti.type_constraints._nodes) 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:27,代碼來源:test_tnode_structure.py

示例9: test_binop_userdefn

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_binop_userdefn():
    src = """
    class A:
        def __add__(self, i):
            return 1

        def __radd__(self, i):
            return 1.0

    class B:
        def __add__(self, i):
            return 'abc'

        def __radd__(self, i):
            return True

    a = A()
    b = B()
    x = a + b
    """
    ast_mod, ti = cs._parse_text(src, reset=True)
    a, b, x = [ti.lookup_typevar(node, node.name) for node
               in ast_mod.nodes_of_class(astroid.AssignName)][8:]
    assert ti.type_constraints.resolve(x).getValue() == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:26,代碼來源:test_binops.py

示例10: test_binop_reverse_right_subclasses_left

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_binop_reverse_right_subclasses_left():
    src = """
    class A:
        def __add__(self, i):
            return 1
            
        def __radd__(self, i):
            return 1.0
            
    class B(A):
        def __add__(self, i):
            return 'abc'
    
        def __radd__(self, i):
            return [1]
            
    a = A()
    b = B()
    x = a + b
    """
    ast_mod, ti = cs._parse_text(src, reset=True)
    a, b, x = [ti.lookup_typevar(node, node.name) for node
               in ast_mod.nodes_of_class(astroid.AssignName)][8:]
    assert ti.type_constraints.resolve(x).getValue() == List[int] 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:26,代碼來源:test_binops.py

示例11: test_augassign_userdefn

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_augassign_userdefn():
    skip('Support for attribute-based inference required for this test to pass')
    program = """
    class A:
        def __init__(self, val):
            self.val = val
    
        def __iadd__(self, other):
            return A(int(self.val + other.val))
            
        def __add__(self, other):
            return A(str(self.val + other.val))
            
    a = A(0)
    b = A(1)
    a += b
    x = a.val
    """
    module, inferer = cs._parse_text(program, reset=True)
    a, b, _, x = [inferer.lookup_typevar(node, node.name) for node
               in module.nodes_of_class(astroid.AssignName)][6:]
    assert inferer.type_constraints.resolve(x).getValue() == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:24,代碼來源:test_assign.py

示例12: test_augassign_userdefn_fallback

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_augassign_userdefn_fallback():
    skip('Support for attribute-based inference required for this test to pass')
    program = """
    class A:
        def __init__(self, val):
            self.val = val

        def __add__(self, other):
            return A(int(self.val + other.val))

    a = A(0)
    b = A(1)
    a += b
    x = a.val
    """
    module, inferer = cs._parse_text(program, reset=True)
    a, b, _, x = [inferer.lookup_typevar(node, node.name) for node
                  in module.nodes_of_class(astroid.AssignName)][4:]
    assert inferer.type_constraints.resolve(x).getValue() == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:21,代碼來源:test_assign.py

示例13: test_subscript_attribute

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_subscript_attribute():
    program = \
        '''
        class A:
            def __init__(self):
                self.lst = []
                self.lst[0] = 1
                
        a = A()
        x = a.lst[0]
        '''
    module, ti = cs._parse_text(program, reset=True)
    for assgn_node in module.nodes_of_class(astroid.AssignName):
        if assgn_node.name == 'x':
            x = ti.lookup_typevar(assgn_node, assgn_node.name)
            assert ti.type_constraints.resolve(x).getValue() == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:18,代碼來源:test_attribute.py

示例14: test_unknown_class_attribute2

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_unknown_class_attribute2():
    program = \
        '''
        def foo(x):
            return x.name + 2
            
        def bar(y):
            z = foo(y)
        '''
    module, ti = cs._parse_text(program, reset=True)
    for binop_node in module.nodes_of_class(astroid.BinOp):
        assert binop_node.inf_type.getValue() == int
    for assgn_node in module.nodes_of_class(astroid.AssignName):
        if assgn_node.name == 'z':
            z = ti.lookup_typevar(assgn_node, assgn_node.name)
            assert ti.type_constraints.resolve(z).getValue() == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:18,代碼來源:test_attribute.py

示例15: test_for_dict

# 需要導入模塊: import astroid [as 別名]
# 或者: from astroid import AssignName [as 別名]
def test_for_dict():
    program = """
        some_dict = {'A': 1, 'B': 2}

        for a, b in some_dict.items():
            x = a
            y = b
        """
    skip(f'Return type of some_dict.items() is inferred as ItemsView[str, int],'
                   f'which does not unify with List[Tuple[str, int]]')
    module, ti = cs._parse_text(program)
    for assign_node in module.nodes_of_class(astroid.AssignName):
        if assign_node.name == 'x' or assign_node.name == 'a':
            assert lookup_type(ti, assign_node, assign_node.name) == str
        elif assign_node.name == 'y' or assign_node.name == 'b':
            assert lookup_type(ti, assign_node, assign_node.name) == int 
開發者ID:pyta-uoft,項目名稱:pyta,代碼行數:18,代碼來源:test_for.py


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