当前位置: 首页>>代码示例>>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;未经允许,请勿转载。