Module rulebook-pylint.rulebook_pylint.empty_parentheses_clip

Functions

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

Classes

class EmptyParenthesesClipChecker (linter: PyLinter)
Expand source code
class EmptyParenthesesClipChecker(RulebookTokenChecker):
    """See detail: https://hanggrian.github.io/rulebook/rules/#empty-parentheses-clip"""
    MSG: str = 'empty-parentheses-clip'

    PARENTHESES: dict[str, str] = {
        '{': '}',
        '(': ')',
        '[': ']',
    }

    name: str = 'empty-parentheses-clip'
    msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG)

    def process_tokens(self, tokens: list[TokenInfo]) -> None:
        for i, token in enumerate(tokens):
            # find opening parenthesis
            if token.type == OP and token.string in {'{', '(', '['}:
                self._process(tokens, i, token)

    def _process(self, tokens: list[TokenInfo], i: int, token: TokenInfo) -> None:
        j: int = i + 1
        end_parenthesis: str = self.PARENTHESES[token.string]

        # compare position when there is only whitespace between parentheses
        if j < len(tokens):
            next_token: TokenInfo = tokens[j]
            if next_token.type == OP and \
                next_token.string == end_parenthesis:
                # checks for violation
                if token.end[1] != next_token.start[1]:
                    self.add_message(
                        self.MSG,
                        line=token.start[0],
                        col_offset=token.start[1],
                        end_lineno=next_token.end[0],
                        end_col_offset=next_token.end[1],
                        args=token.string + end_parenthesis,
                    )
                return

        # otherwise iterate to determine newline
        has_newline: bool = False
        while j < len(tokens):
            current_token: TokenInfo = tokens[j]
            # checks for violation
            if current_token.type == NL:
                has_newline = True
            elif current_token.type == OP and \
                current_token.string == end_parenthesis:
                if has_newline:
                    self.add_message(
                        self.MSG,
                        line=token.start[0],
                        col_offset=token.start[1],
                        end_lineno=current_token.end[0],
                        end_col_offset=current_token.end[1],
                        args=token.string + end_parenthesis,
                    )
                    return
            else:
                return
            j += 1

See detail: https://hanggrian.github.io/rulebook/rules/#empty-parentheses-clip

Checker instances should have the linter as argument.

Ancestors

  • rulebook_pylint.checkers.RulebookTokenChecker
  • pylint.checkers.base_checker.BaseTokenChecker
  • pylint.checkers.base_checker.BaseChecker
  • pylint.config.arguments_provider._ArgumentsProvider
  • abc.ABC

Class variables

var MSG : str

The type of the None singleton.

var PARENTHESES : dict[str, 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 process_tokens(self, tokens: list[tokenize.TokenInfo]) ‑> None
Expand source code
def process_tokens(self, tokens: list[TokenInfo]) -> None:
    for i, token in enumerate(tokens):
        # find opening parenthesis
        if token.type == OP and token.string in {'{', '(', '['}:
            self._process(tokens, i, token)

Should be overridden by subclasses.