How to use the astroid.Name function in astroid

To help you get started, we’ve selected a few astroid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyta-uoft / pyta / python_ta / checkers / redundant_assignment_checker.py View on Github external
def _transfer(self, block: CFGBlock, out_facts: Set[str]) -> Set[str]:
        gen = out_facts.copy()
        kill = set()
        for statement in reversed(block.statements):
            if isinstance(statement, astroid.FunctionDef):
                # `nodes_of_class` below doesnt block looking for required nodes
                # in function definitions, hence this case.
                continue
            for node in statement.nodes_of_class((astroid.AssignName, astroid.DelName, astroid.Name,
                                                  astroid.Nonlocal, astroid.Global),
                                              astroid.FunctionDef):
                if isinstance(node, astroid.AssignName):
                    if node.name in gen.difference(kill):
                        self._redundant_assignment.add(node.parent)
                    elif node.parent in self._redundant_assignment:
                        self._redundant_assignment.remove(node.parent)

                    # When node.parent is an AugAssign, the name counts as a use of the variable,
                    # and so is added to kill.
                    if isinstance(node.parent, astroid.AugAssign):
                        kill.add(node.name)
                    else:
                        kill.discard(node.name)
                    gen.add(node.name)
                elif isinstance(node, (astroid.Nonlocal, astroid.Global)):
github PyCQA / astroid / astroid / brain / brain_random.py View on Github external
def _looks_like_random_sample(node):
    func = node.func
    if isinstance(func, astroid.Attribute):
        return func.attrname == "sample"
    if isinstance(func, astroid.Name):
        return func.name == "sample"
    return False
github life4 / deal / deal / linter / _contract.py View on Github external
def _resolve_name(contract):
        if not isinstance(contract, astroid.Name):
            return contract
        definitions = contract.lookup(contract.name)[1]
        if not definitions:
            return contract
        definition = definitions[0]
        if isinstance(definition, astroid.FunctionDef):
            return definition
        if isinstance(definition, astroid.AssignName):
            return definition.parent.value
        # resolved into something tricky, live with it
        return contract  # pragma: no cover
github PyCQA / pylint / pylint / checkers / variables.py View on Github external
return

        if not isinstance(type_annotation, astroid.Subscript):
            return

        if (
            isinstance(type_annotation.value, astroid.Attribute)
            and isinstance(type_annotation.value.expr, astroid.Name)
            and type_annotation.value.expr.name == TYPING_MODULE
        ):
            self._type_annotation_names.append(TYPING_MODULE)
            return

        self._type_annotation_names.extend(
            annotation.name
            for annotation in type_annotation.nodes_of_class(astroid.Name)
        )
github PyCQA / astroid / astroid / brain / brain_numpy_utils.py View on Github external
def looks_like_numpy_member(
    member_name: str, node: astroid.node_classes.NodeNG
) -> bool:
    """
    Returns True if the node is a member of numpy whose
    name is member_name.

    :param member_name: name of the member
    :param node: node to test
    :return: True if the node is a member of numpy
    """
    return (
        isinstance(node, astroid.Attribute)
        and node.attrname == member_name
        and isinstance(node.expr, astroid.Name)
        and _is_a_numpy_module(node.expr)
    )
github pyta-uoft / pyta / python_ta / transforms / type_inference_visitor.py View on Github external
def _populate_local_env_attrs(self, node: NodeNG) -> None:
        """Store in TypeStore the attributes of any unresolved class names"""
        for attr_node in chain(node.nodes_of_class(astroid.Attribute), node.nodes_of_class(astroid.AssignAttr)):
            if isinstance(attr_node.expr, astroid.Name) and attr_node.expr.name in node.type_environment.locals:
                class_type = node.type_environment.lookup_in_env(attr_node.expr.name)
                if isinstance(class_type, TypeVar):
                    self.type_store.classes[class_type.__name__]['__mro'] = [class_type.__name__]
                    if not attr_node.attrname in self.type_store.classes[class_type.__name__]:
                        self.type_store.classes[class_type.__name__][attr_node.attrname] = \
                            [(self.type_constraints.fresh_tvar(attr_node), 'attribute')]
github svn2github / chromium-depot-tools / third_party / pylint / checkers / base.py View on Github external
def visit_function(self, node):
        if not redefined_by_decorator(node):
            self._check_redefinition(node.is_method() and 'method' or 'function', node)
        # checks for max returns, branch, return in __init__
        returns = node.nodes_of_class(astroid.Return,
                                      skip_klass=(astroid.Function, astroid.Class))
        if node.is_method() and node.name == '__init__':
            if node.is_generator():
                self.add_message('init-is-generator', node=node)
            else:
                values = [r.value for r in returns]
                # Are we returning anything but None from constructors
                if [v for v in values
                        if not (v is None or
                                (isinstance(v, astroid.Const) and v.value is None) or
                                (isinstance(v, astroid.Name)  and v.name == 'None')
                               )]:
                    self.add_message('return-in-init', node=node)
        elif node.is_generator():
            # make sure we don't mix non-None returns and yields
            if not PY33:
                for retnode in returns:
                    if isinstance(retnode.value, astroid.Const) and \
                           retnode.value.value is not None:
                        self.add_message('return-arg-in-generator', node=node,
                                         line=retnode.fromlineno)
        # Check for duplicate names
        args = set()
        for name in node.argnames():
            if name in args:
                self.add_message('duplicate-argument-name', node=node, args=(name,))
            else:
github PyCQA / astroid / astroid / brain / brain_functools.py View on Github external
def _looks_like_functools_member(node, member):
    """Check if the given Call node is a functools.partial call"""
    if isinstance(node.func, astroid.Name):
        return node.func.name == member
    elif isinstance(node.func, astroid.Attribute):
        return (
            node.func.attrname == member
            and isinstance(node.func.expr, astroid.Name)
            and node.func.expr.name == "functools"
        )
github readthedocs / sphinx-autoapi / autoapi / mappers / python / astroid_utils.py View on Github external
def is_decorated_with_property(node):
    """Check if the function is decorated as a property.

    :param node: The node to check.
    :type node: astroid.nodes.FunctionDef

    :returns: True if the function is a property, False otherwise.
    :rtype: bool
    """
    if not node.decorators:
        return False

    for decorator in node.decorators.nodes:
        if not isinstance(decorator, astroid.Name):
            continue

        try:
            if _is_property_decorator(decorator):
                return True
        except astroid.InferenceError:
            pass

    return False
github PyCQA / pylint / pylint / checkers / python3.py View on Github external
"""
    parent = node.parent
    # Since a call can't be the loop variant we only need to know if the node's
    # parent is a 'for' loop to know it's being used as the iterator for the
    # loop.
    if isinstance(parent, astroid.For):
        return True
    # Need to make sure the use of the node is in the iterator part of the
    # comprehension.
    elif isinstance(parent, astroid.Comprehension):
        if parent.iter == node:
            return True
    # Various built-ins can take in an iterable or list and lead to the same
    # value.
    elif isinstance(parent, astroid.CallFunc):
        if isinstance(parent.func, astroid.Name):
            parent_scope = parent.func.lookup(parent.func.name)[0]
            if _is_builtin(parent_scope) and parent.func.name in _accepts_iterator:
                return True
        elif isinstance(parent.func, astroid.Getattr):
            if parent.func.attrname == 'join':
                return True
    # If the call is in an unpacking, there's no need to warn,
    # since it can be considered iterating.
    elif (isinstance(parent, astroid.Assign) and
          isinstance(parent.targets[0], (astroid.List, astroid.Tuple))):
        if len(parent.targets[0].elts) > 1:
            return True
    return False