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)
Example #2
0
    def _check_attributes_shadowing(self, node: ast.ClassDef) -> None:
        class_attributes, instance_attributes = classes.get_attributes(node)
        class_attribute_names = set(
            name_nodes.flat_variable_names(class_attributes), )

        for instance_attr in instance_attributes:
            if instance_attr.attr in class_attribute_names:
                self.add_violation(
                    oop.ShadowedClassAttributeViolation(
                        instance_attr,
                        text=instance_attr.attr,
                    ), )
Example #3
0
    def _check_public_attributes(self, node: ast.ClassDef) -> None:
        _, instance_attributes = classes.get_attributes(node)
        attrs_count = len({
            attr.attr
            for attr in instance_attributes if access.is_public(attr.attr)
        })

        if attrs_count > self.options.max_attributes:
            self.add_violation(
                TooManyPublicAttributesViolation(
                    node,
                    text=str(attrs_count),
                    baseline=self.options.max_attributes,
                ), )
def find_attributed_getters_and_setters(
    node: ast.ClassDef, ) -> Iterable[AnyFunctionDef]:
    """Returns nodes of attributed getter or setter methods."""
    class_attributes, instance_attributes = classes.get_attributes(
        node,
        include_annotated=True,
    )
    flat_class_attributes = name_nodes.flat_variable_names(class_attributes)

    attributes_stripped = {
        class_attribute.lstrip(UNUSED_PLACEHOLDER)
        for class_attribute in flat_class_attributes
    }.union({
        instance.attr.lstrip(UNUSED_PLACEHOLDER)
        for instance in instance_attributes
    })

    for method in _find_getters_and_setters(node):
        if method.name[GETTER_LENGTH:] in attributes_stripped:
            yield method
Example #5
0
    def _check_getters_setters_methods(self, node: ast.ClassDef) -> None:
        class_attributes, instance_attributes = classes.get_attributes(
            node,
            include_annotated=True,
        )
        flat_class_attributes = name_nodes.flat_variable_names(class_attributes)

        attributes_stripped = {
            class_attribute.lstrip(constants.UNUSED_PLACEHOLDER)
            for class_attribute in flat_class_attributes
        }.union({
            instance.attr.lstrip(constants.UNUSED_PLACEHOLDER)
            for instance in instance_attributes
        })

        for method in classes.find_getters_and_setters(node):
            if method.name[classes.GETTER_LENGTH:] in attributes_stripped:
                self.add_violation(
                    oop.UnpythonicGetterSetterViolation(
                        method,
                        text=method.name,
                    ),
                )