def _flat_assign_names(self, nodes: List[AnyAssign]) -> Container[str]:
     flat_assigns = []
     for attribute in nodes:
         targets = get_assign_targets(attribute)
         flat_assigns.extend(
             [at.id for at in targets if isinstance(at, ast.Name)])
     return set(flat_assigns)
    def _contains_slots_assign(self, node: AnyAssign) -> bool:
        targets = get_assign_targets(node)

        for target in targets:
            if isinstance(target, ast.Name) and target.id == '__slots__':
                return True
        return False
示例#3
0
    def check_attribute_name(self, node: ast.ClassDef) -> None:
        top_level_assigns = [
            sub_node for sub_node in node.body
            if isinstance(sub_node, AssignNodes)
        ]

        for assignment in top_level_assigns:
            for target in get_assign_targets(assignment):
                self._ensure_case(target)
示例#4
0
    def check_attribute_name(self, node: ast.ClassDef) -> None:
        top_level_assigns = [
            sub for sub in ast.walk(node)
            if isinstance(sub, AssignNodes) and nodes.get_context(sub) is node
        ]

        for assignment in top_level_assigns:
            for target in get_assign_targets(assignment):
                self._ensure_case(target)
    def check_attribute_names(self, node: ast.ClassDef) -> None:
        class_attributes, _ = classes.get_attributes(
            node, include_annotated=True,
        )

        for assign in class_attributes:
            for target in get_assign_targets(assign):
                for attr_name in name_nodes.get_variables_from_node(target):
                    self._ensure_case(assign, attr_name)
示例#6
0
    def _check_metadata(self, node: AnyAssign) -> None:
        if not isinstance(nodes.get_parent(node), ast.Module):
            return

        targets = get_assign_targets(node)
        for target_node in targets:
            target_node_id = _get_name_from_node(target_node)
            if target_node_id in MODULE_METADATA_VARIABLES_BLACKLIST:
                self.add_violation(
                    WrongModuleMetadataViolation(node, text=target_node_id), )
示例#7
0
    def _check_assignment(self, node: AnyAssign) -> None:
        target_names = self._create_target_names(get_assign_targets(node), )

        if isinstance(node.value, ast.Tuple):
            node_values = node.value.elts
            values_names = tuple(
                _get_name_from_node(node_value) for node_value in node_values)
        else:
            values_names = _get_name_from_node(node.value)  # type: ignore
        has_repeatable_values = len(target_names) != len(set(target_names))
        if values_names in target_names or has_repeatable_values:
            self.add_violation(ReassigningVariableToItselfViolation(node))
示例#8
0
    def check_attribute_name(self, node: ast.ClassDef) -> None:
        top_level_assigns = [
            sub_node for sub_node in node.body
            if isinstance(sub_node, AssignNodes)
        ]

        for assignment in top_level_assigns:
            for target in get_assign_targets(assignment):
                if not isinstance(target, ast.Name):
                    continue

                name = _get_name_from_node(target)
                if name and logical.is_upper_case_name(name):
                    self._error_callback(
                        naming.UpperCaseAttributeViolation(target,
                                                           text=name), )
示例#9
0
    def _is_simplifiable_assign(
        self,
        node_body: List[ast.stmt],
    ) -> Optional[str]:
        wrong_length = len(node_body) != 1
        if wrong_length or not isinstance(node_body[0], AssignNodes):
            return None
        if not isinstance(node_body[0].value, ast.NameConstant):
            return None
        if node_body[0].value.value is None:
            return None

        targets = get_assign_targets(node_body[0])
        if len(targets) != 1:
            return None

        return source.node_to_string(targets[0])
示例#10
0
    def _check_metadata(self, node: AnyAssign) -> None:
        if not isinstance(nodes.get_parent(node), ast.Module):
            return

        targets = get_assign_targets(node)
        for target_node in targets:
            if not isinstance(target_node, ast.Name):
                continue

            if target_node.id not in MODULE_METADATA_VARIABLES_BLACKLIST:
                continue

            self.add_violation(
                best_practices.WrongModuleMetadataViolation(
                    node,
                    text=target_node.id,
                ), )
示例#11
0
def flat_variable_names(nodes: Iterable[AnyAssignWithWalrus]) -> Iterable[str]:
    """
    Returns flat variable names from several nodes.

    Use this function when you need to get list of string variable names
    from assign nodes.

    Here's an example:

    >>> import ast
    >>> tree = ast.parse('x: int = 0')
    >>> node = tree.body[0]
    >>> list(flat_variable_names([node]))
    ['x']

    >>> tree = ast.parse('z = y = 0')
    >>> node = tree.body[0]
    >>> list(flat_variable_names([node]))
    ['z', 'y']

    """
    return itertools.chain.from_iterable(
        (get_variables_from_node(target) for node in nodes
         for target in get_assign_targets(node)))
 def _is_assigned_target(self, node: ast.Subscript) -> bool:
     parent = nodes.get_parent(node)
     if not isinstance(parent, (*AssignNodes, ast.AugAssign)):
         return False
     return any(node == target for target in get_assign_targets(parent))