Merged
refactor(scripts): Split check_slice_spacing to lower cyclomatic complexity #6517
chaizhengtongAdd on Sep 20
refactor(scripts): Split check_slice_spacing to lower cyclomatic complexity #6517
Merged
Merged fromchai-hw/pypto_chaifix_precommit_9.2.0intocann/pypto9.2.0
Total: 1 files changed+69-36
| @@ -31,6 +31,70 @@ def _in_subscript(bracket_stack): | |||
| 31 | return False | 31 | return False |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | +def _update_bracket_stack(bracket_stack, tok_string): | ||
| 35 | + """Update *bracket_stack* for an opening/closing bracket. | ||
| 36 | + | ||
| 37 | + Return True if *tok_string* is a bracket token. | ||
| 38 | + """ | ||
| 39 | + if tok_string in ("[", "{", "("): | ||
| 40 | + bracket_stack.append(tok_string) | ||
| 41 | + return True | ||
| 42 | + if tok_string in ("]", "}", ")"): | ||
| 43 | + if bracket_stack: | ||
| 44 | + bracket_stack.pop() | ||
| 45 | + return True | ||
| 46 | + return False | ||
| 47 | + | ||
| 48 | + | ||
| 49 | +def _prev_token(tokens, i): | ||
| 50 | + return tokens[i - 1] if i > 0 else None | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def _next_token(tokens, i): | ||
| 54 | + return tokens[i + 1] if i + 1 < len(tokens) else None | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +def _colon_space_fixes(tokens, i, tok): | ||
| 58 | + """Strip spaces around a subscript colon, except after a comma.""" | ||
| 59 | + row, col = tok.start | ||
| 60 | + end_col = tok.end[1] | ||
| 61 | + prev_tok = _prev_token(tokens, i) | ||
| 62 | + next_tok = _next_token(tokens, i) | ||
| 63 | + fixes = [] | ||
| 64 | + # Remove space before colon, unless the previous token is a comma: | ||
| 65 | + # a comma in a subscript must keep a following space (e.g. a[1:2, :]), | ||
| 66 | + # which is handled by the comma rule below. | ||
| 67 | + if ( | ||
| 68 | + prev_tok | ||
| 69 | + and prev_tok.string != "," | ||
| 70 | + and prev_tok.end[0] == row | ||
| 71 | + and prev_tok.end[1] < col | ||
| 72 | + ): | ||
| 73 | + fixes.append((row - 1, prev_tok.end[1], col, "")) | ||
| 74 | + if next_tok and next_tok.start[0] == row and end_col < next_tok.start[1]: | ||
| 75 | + fixes.append((row - 1, end_col, next_tok.start[1], "")) | ||
| 76 | + return fixes | ||
| 77 | + | ||
| 78 | + | ||
| 79 | +def _comma_space_fixes(tokens, i, tok): | ||
| 80 | + """Insert a space after a subscript comma when the next token is adjacent.""" | ||
| 81 | + row = tok.start[0] | ||
| 82 | + end_col = tok.end[1] | ||
| 83 | + next_tok = _next_token(tokens, i) | ||
| 84 | + # Ensure a space after the comma when the next token is on the same | ||
| 85 | + # line and is not a closing bracket or a line break | ||
| 86 | + # (e.g. a[1:2,:] -> a[1:2, :]). | ||
| 87 | + if ( | ||
| 88 | + next_tok | ||
| 89 | + and next_tok.type not in (tokenize.NL, tokenize.NEWLINE) | ||
| 90 | + and next_tok.start[0] == row | ||
| 91 | + and next_tok.string not in ("]", "}", ")") | ||
| 92 | + and end_col == next_tok.start[1] | ||
| 93 | + ): | ||
| 94 | + return [(row - 1, end_col, end_col, " ")] | ||
| 95 | + return [] | ||
| 96 | + | ||
| 97 | + | ||
| 34 | def _find_fixes(source): | 98 | def _find_fixes(source): |
| 35 | """Return ``(line_idx, start_col, end_col, replacement)`` edits to apply.""" | 99 | """Return ``(line_idx, start_col, end_col, replacement)`` edits to apply.""" |
| 36 | tokens = list(tokenize.generate_tokens(io.StringIO(source).readline)) | 100 | tokens = list(tokenize.generate_tokens(io.StringIO(source).readline)) |
| @@ -40,43 +104,12 @@ def _find_fixes(source): | |||
| 40 | for i, tok in enumerate(tokens): | 104 | for i, tok in enumerate(tokens): |
| 41 | if tok.type != tokenize.OP: | 105 | if tok.type != tokenize.OP: |
| 42 | continue | 106 | continue |
| 43 | - if tok.string in ("[", "{", "("): | 107 | + if _update_bracket_stack(bracket_stack, tok.string): |
| 44 | - bracket_stack.append(tok.string) | 108 | + continue |
| 45 | - elif tok.string in ("]", "}", ")"): | 109 | + if tok.string == ":" and _in_subscript(bracket_stack): |
| 46 | - if bracket_stack: | 110 | + fixes.extend(_colon_space_fixes(tokens, i, tok)) |
| 47 | - bracket_stack.pop() | ||
| 48 | - elif tok.string == ":" and _in_subscript(bracket_stack): | ||
| 49 | - row, col = tok.start | ||
| 50 | - end_col = tok.end[1] | ||
| 51 | - prev_tok = tokens[i - 1] if i > 0 else None | ||
| 52 | - next_tok = tokens[i + 1] if i + 1 < len(tokens) else None | ||
| 53 | - # Remove space before colon, unless the previous token is a comma: | ||
| 54 | - # a comma in a subscript must keep a following space (e.g. a[1:2, :]), | ||
| 55 | - # which is handled by the comma rule below. | ||
| 56 | - if ( | ||
| 57 | - prev_tok | ||
| 58 | - and prev_tok.string != "," | ||
| 59 | - and prev_tok.end[0] == row | ||
| 60 | - and prev_tok.end[1] < col | ||
| 61 | - ): | ||
| 62 | - fixes.append((row - 1, prev_tok.end[1], col, "")) | ||
| 63 | - if next_tok and next_tok.start[0] == row and end_col < next_tok.start[1]: | ||
| 64 | - fixes.append((row - 1, end_col, next_tok.start[1], "")) | ||
| 65 | elif tok.string == "," and _in_subscript(bracket_stack): | 111 | elif tok.string == "," and _in_subscript(bracket_stack): |
| 66 | - row, col = tok.start | 112 | + fixes.extend(_comma_space_fixes(tokens, i, tok)) |
| 67 | - end_col = tok.end[1] | ||
| 68 | - next_tok = tokens[i + 1] if i + 1 < len(tokens) else None | ||
| 69 | - # Ensure a space after the comma when the next token is on the same | ||
| 70 | - # line and is not a closing bracket or a line break | ||
| 71 | - # (e.g. a[1:2,:] -> a[1:2, :]). | ||
| 72 | - if ( | ||
| 73 | - next_tok | ||
| 74 | - and next_tok.type not in (tokenize.NL, tokenize.NEWLINE) | ||
| 75 | - and next_tok.start[0] == row | ||
| 76 | - and next_tok.string not in ("]", "}", ")") | ||
| 77 | - and end_col == next_tok.start[1] | ||
| 78 | - ): | ||
| 79 | - fixes.append((row - 1, end_col, end_col, " ")) | ||
| 80 | return fixes | 113 | return fixes |
| 81 | 114 | ||
| 82 | 115 | ||