def get_reverse_bracket(bracket: tokenize.TokenInfo) -> int: """ Returns the reverse closing bracket for an opening token. >>> import tokenize >>> import token >>> bracket = tokenize.TokenInfo(token.RPAR, ")", 6, 7, "(a, b)") >>> get_reverse_bracket(bracket) == token.LPAR True """ index = list(MATCHING_BRACKETS.values()).index(bracket.exact_type) return list(MATCHING_BRACKETS.keys())[index]
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
def _check_individual_line(self, tokens: List[tokenize.TokenInfo]) -> None: for index, token in enumerate(tokens): if token.exact_type in MATCHING.values(): self._check_closing(token, index, tokens) if index == 0: self._check_empty_line_wrap(token, delta=-1) elif token.exact_type in MATCHING and last_bracket(tokens, index): self._check_empty_line_wrap(token, delta=1)