示例#1
0
 def visit_name(self, node: astroid.Name) -> None:
     try:
         node.inf_type = TypeInfo(self.lookup_type(node, node.name))
     except KeyError:
         if node.name in self.type_store.classes:
             node.inf_type = TypeInfo(Type[__builtins__[node.name]])
         elif node.name in self.type_store.functions:
             node.inf_type = TypeInfo(
                 self.type_store.functions[node.name][0])
         else:
             # This is an unbound identifier. Ignore it.
             node.inf_type = TypeInfo(Any)
示例#2
0
def is_defined_before(var_node: astroid.Name) -> bool:
    """Check if the given variable node is defined before

    Verify that the variable node is defined by a parent node
    (list, set, dict, or generator comprehension, lambda)
    or in a previous sibling node on the same line
    (statement_defining ; statement_using).
    """
    varname = var_node.name
    _node = var_node.parent
    while _node:
        if is_defined_in_scope(var_node, varname, _node):
            return True
        _node = _node.parent
    # possibly multiple statements on the same line using semi colon separator
    stmt = var_node.statement()
    _node = stmt.previous_sibling()
    lineno = stmt.fromlineno
    while _node and _node.fromlineno == lineno:
        for assign_node in _node.nodes_of_class(astroid.AssignName):
            if assign_node.name == varname:
                return True
        for imp_node in _node.nodes_of_class(
            (astroid.ImportFrom, astroid.Import)):
            if varname in [name[1] or name[0] for name in imp_node.names]:
                return True
        _node = _node.previous_sibling()
    return False
示例#3
0
def _is_property_decorator(decorator: astroid.Name) -> bool:
    for infered in decorator.infer():
        if isinstance(infered, astroid.ClassDef):
            if infered.root().name == BUILTINS_NAME and infered.name == 'property':
                return True
            for ancestor in infered.ancestors():
                if ancestor.name == 'property' and ancestor.root().name == BUILTINS_NAME:
                    return True
    return False
示例#4
0
def transform(cls):
    """Pretend that any class inheriting from 'Dict' also inherits from 'dict'.
    Works around PyLint not knowing about operations on the 'Dict' class.
    https://github.com/PyCQA/pylint/issues/3129
    """
    for index, base in enumerate(cls.bases):
        if isinstance(base, Subscript):
            if isinstance(base.value, Name):
                if base.value.name == 'Dict':
                    cls.bases.append(Name('dict', parent=cls))
示例#5
0
def _scope_lookup(node: astroid.Name) -> Optional[NodeNG]:
    """Look up the given name node's assigment node.

    This is a replacement for astroid's LocalsDictNodeNG._scope_lookup method, which doesn't
    seem to handle nested comprehensions (?).
    """
    scope = node.scope()
    while node.name not in scope and not isinstance(scope, astroid.Module):
        scope = scope.parent.scope()

    if node.name in scope:
        return scope[node.name]
    else:
        return None
示例#6
0
def _transform_assert_false_into_raise(assertion):
    if isinstance(assertion.test, Const) and assertion.test.value is False:
        out = Raise(lineno=assertion.lineno,
                    col_offset=assertion.col_offset,
                    parent=assertion.parent)
        exc = Call(parent=out)
        if assertion.fail:
            args = [assertion.fail]
            args[0].parent = exc
        else:
            args = []
        exc.postinit(Name("AssertionError", parent=exc), args)
        out.postinit(exc, None)
        return out
 def visit_name(self, node: astroid.Name) -> None:
     node.inf_type = self.lookup_inf_type(node, node.name)
示例#8
0
 def visit_name(self, node: astroid.Name) -> None:
     node.inf_type = self.lookup_inf_type(node, node.name)