def _check_closing( self, token: tokenize.TokenInfo, index: int, tokens: List[tokenize.TokenInfo], ) -> None: tokens_before = tokens[:index] annotated = self._annotate_brackets(tokens_before) if annotated[get_reverse_bracket(token)] == 0: if not only_contains(tokens_before, ALLOWED_EMPTY_LINE_TOKENS): self.add_violation(WrongBracketPositionViolation(token))
def _annotate_brackets( self, tokens: List[tokenize.TokenInfo], ) -> Mapping[int, int]: """Annotates each opening bracket with the nested level index.""" brackets = {bracket: 0 for bracket in MATCHING} for token in tokens: if token.exact_type in MATCHING.keys(): brackets[token.exact_type] += 1 if token.exact_type in MATCHING.values(): reverse_bracket = get_reverse_bracket(token) if brackets[reverse_bracket] > 0: brackets[reverse_bracket] -= 1 return brackets