コード例 #1
0
 def _check_unique_assignment(
     self,
     node: AnyAssign,
     names: List[str],
 ) -> None:
     for used_name, count in Counter(names).items():
         if count > 1:
             self.add_violation(
                 ReassigningVariableToItselfViolation(node,
                                                      text=used_name), )
コード例 #2
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))
コード例 #3
0
    def _check_assignment(self, node: ast.Assign) -> None:
        target_names = self._create_target_names(node.targets)

        if isinstance(node.value, ast.Tuple):
            node_values = node.value.elts
            values_names = tuple(
                getattr(node_value, 'id', None) for node_value in node_values)
        else:
            values_names = getattr(node.value, 'id', None)
        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))
コード例 #4
0
    def _check_reassignment(
        self,
        node: AnyAssign,
        names: List[str],
    ) -> None:
        if not node.value:
            return

        var_values = name_nodes.get_variables_from_node(node.value)
        for var_name, var_value in itertools.zip_longest(names, var_values):
            if var_name == var_value:
                self.add_violation(
                    ReassigningVariableToItselfViolation(node,
                                                         text=var_name), )