Module rulebook-pylint.rulebook_pylint.redundant_default

Functions

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

Classes

class RedundantDefaultChecker (linter: PyLinter)
Expand source code
class RedundantDefaultChecker(RulebookChecker):
    """See detail: https://hanggrian.github.io/rulebook/rules/#redundant-default"""
    MSG: str = 'redundant-default'

    name: str = 'redundant-default'
    msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG)

    def visit_match(self, node: Match) -> None:
        # skip no default
        cases: list[MatchCase] = node.cases
        if not cases:
            return
        default: MatchCase = cases[-1]
        if not isinstance(default.pattern, MatchAs) or default.pattern.name:
            return

        # checks for violation
        if not all(has_jump_statement(node) for node in cases[:-1]):
            return
        self.add_message(self.MSG, node=default)

See detail: https://hanggrian.github.io/rulebook/rules/#redundant-default

Checker instances should have the linter as argument.

Ancestors

  • rulebook_pylint.checkers.RulebookChecker
  • pylint.checkers.base_checker.BaseChecker
  • pylint.config.arguments_provider._ArgumentsProvider

Class variables

var MSG : 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:
    # skip no default
    cases: list[MatchCase] = node.cases
    if not cases:
        return
    default: MatchCase = cases[-1]
    if not isinstance(default.pattern, MatchAs) or default.pattern.name:
        return

    # checks for violation
    if not all(has_jump_statement(node) for node in cases[:-1]):
        return
    self.add_message(self.MSG, node=default)