Module rulebook_pylint.checkers.case_separator

Functions

def register(linter: PyLinter)
Expand source code
def register(linter: 'PyLinter') -> None:
    linter.register_checker(CaseSeparatorChecker(linter))

Classes

class CaseSeparatorChecker (linter: PyLinter)
Expand source code
class CaseSeparatorChecker(RulebookFileChecker):
    """See detail: https://hanggrian.github.io/rulebook/rules/#case-separator"""
    MSG_MISSING: str = 'case-separator-missing'
    MSG_UNEXPECTED: str = 'case-separator-unexpected'

    name: str = 'case-separator'
    msgs: dict[str, MessageDefinitionTuple] = _Messages.of(MSG_MISSING, MSG_UNEXPECTED)

    def visit_match(self, node: Match) -> None:
        # collect cases
        match_cases: list[MatchCase] = node.cases
        if len(match_cases) == 0:
            return

        # checks for violation
        has_multiline = \
            any(
                _is_multiline(match_case) or
                _has_comment_above(self.lines, match_case) for
                match_case in match_cases
            )
        for (i, match_case) in enumerate(match_cases):
            if i == 0:
                continue
            last_match_case: MatchCase = match_cases[i - 1]
            match_case_fromlineno: int = \
                _get_fromlineno_before(self.lines, match_case, last_match_case)
            last_body: NodeNG = last_match_case.body[-1]
            if has_multiline:
                if last_body.tolineno - 1 != match_case_fromlineno - 2:
                    self.add_message(
                        self.MSG_MISSING,
                        line=last_body.lineno,
                        end_lineno=last_body.end_lineno,
                        col_offset=last_body.col_offset,
                        end_col_offset=last_body.end_col_offset,
                    )
            elif last_body.tolineno - 1 != match_case_fromlineno - 1:
                self.add_message(
                    self.MSG_UNEXPECTED,
                    line=last_body.lineno,
                    end_lineno=last_body.end_lineno,
                    col_offset=last_body.col_offset,
                    end_col_offset=last_body.end_col_offset,
                )

See detail: https://hanggrian.github.io/rulebook/rules/#case-separator

Checker instances should have the linter as argument.

Ancestors

  • RulebookFileChecker
  • pylint.checkers.base_checker.BaseRawFileChecker
  • pylint.checkers.base_checker.BaseChecker
  • pylint.config.arguments_provider._ArgumentsProvider
  • abc.ABC

Class variables

var MSG_MISSING : str

The type of the None singleton.

var MSG_UNEXPECTED : str

The type of the None singleton.

var msgs : dict[str, tuple[str, str, str] | tuple[str, str, str, pylint.typing.ExtraMessageOptions]]

The type of the None singleton.

var name : str

The type of the None singleton.

Methods

def visit_match(self, node: astroid.nodes.node_classes.Match) ‑> None
Expand source code
def visit_match(self, node: Match) -> None:
    # collect cases
    match_cases: list[MatchCase] = node.cases
    if len(match_cases) == 0:
        return

    # checks for violation
    has_multiline = \
        any(
            _is_multiline(match_case) or
            _has_comment_above(self.lines, match_case) for
            match_case in match_cases
        )
    for (i, match_case) in enumerate(match_cases):
        if i == 0:
            continue
        last_match_case: MatchCase = match_cases[i - 1]
        match_case_fromlineno: int = \
            _get_fromlineno_before(self.lines, match_case, last_match_case)
        last_body: NodeNG = last_match_case.body[-1]
        if has_multiline:
            if last_body.tolineno - 1 != match_case_fromlineno - 2:
                self.add_message(
                    self.MSG_MISSING,
                    line=last_body.lineno,
                    end_lineno=last_body.end_lineno,
                    col_offset=last_body.col_offset,
                    end_col_offset=last_body.end_col_offset,
                )
        elif last_body.tolineno - 1 != match_case_fromlineno - 1:
            self.add_message(
                self.MSG_UNEXPECTED,
                line=last_body.lineno,
                end_lineno=last_body.end_lineno,
                col_offset=last_body.col_offset,
                end_col_offset=last_body.end_col_offset,
            )

Inherited members