def _count_slots_items( self, node: types.AnyAssign, elements: ast.Tuple, ) -> None: fields: List[str] = [] for tuple_item in elements.elts: if not isinstance(tuple_item, ast.Str): self.add_violation(oop.WrongSlotsViolation(node)) return fields.append(tuple_item.s) for _, counter in Counter(fields).items(): if counter > 1: self.add_violation(oop.WrongSlotsViolation(node)) return
def _count_slots_items( self, node: types.AnyAssign, elements: ast.Tuple, ) -> None: fields: List[str] = [] for tuple_item in elements.elts: slot_name = self._slot_item_name(tuple_item) if not slot_name: self.add_violation(oop.WrongSlotsViolation(node)) return fields.append(slot_name) for slot, counter in Counter(fields).items(): if not self._is_correct_slot(slot) or counter > 1: self.add_violation(oop.WrongSlotsViolation(node)) return
def _count_slots_items( self, node: types.AnyAssign, elements: ast.Tuple, ) -> None: fields: DefaultDict[str, List[ast.AST]] = defaultdict(list) for tuple_item in elements.elts: slot_name = self._slot_item_name(tuple_item) if not slot_name: self.add_violation(oop.WrongSlotsViolation(tuple_item)) return fields[slot_name].append(tuple_item) for slots in fields.values(): if not self._are_correct_slots(slots) or len(slots) > 1: self.add_violation(oop.WrongSlotsViolation(node)) return
def _check_slots(self, node: types.AnyAssign) -> None: if not isinstance(nodes.get_context(node), ast.ClassDef): return if not self._contains_slots_assign(node): return if not isinstance(node.value, self._whitelisted_slots_nodes): self.add_violation(oop.WrongSlotsViolation(node)) return if isinstance(node.value, ast.Tuple): self._count_slots_items(node, node.value)