Whole file
robsdedude/flake8-picky-parentheses
The author described this change as “Python 3.10: fix match/case (#24)”. It counts as a record because the checks below fail on the code as it stood at d2f530a2a and pass on 1384f6f9e, with nothing else changed between the two runs.
Fix saved2022-10-07
Sharing licenceApache-2.0 · LICENSE
Change size+21 −3
What the code was meant to do, written into the code itself as a save note
Python 3.10: fix match/case (#24)
The change
| 18 | 18 | "elif": "if True:\n pass\n", | |
| 19 | 19 | "except": "try:\n pass\n", | |
| 20 | 20 | "finally": "try:\n pass\n", | |
| 21 | + | "case": "match _:\n ", | |
| 21 | 22 | } | |
| 22 | 23 | ||
| 24 | + | AST_FIX_SPECIAL_BODIES = { | |
| 25 | + | "match": "\n case _:\n pass", | |
| 26 | + | } | |
| 27 | + | ||
| 23 | 28 | IGNORED_TYPES_FOR_PARENS = { | |
| 24 | 29 | tokenize.NL, | |
| 25 | 30 | tokenize.COMMENT, | |
| ⋯ | |||
| 191 | 196 | line += "\ndef f():" | |
| 192 | 197 | needs_body = True | |
| 193 | 198 | if needs_body: | |
| 194 | - | line += "\n pass" | |
| 199 | + | keyword = line.strip().split()[0] | |
| 200 | + | line += AST_FIX_SPECIAL_BODIES.get(keyword, "\n pass") | |
| 195 | 201 | if ast_fix_prefix: | |
| 202 | + | extra_indent = ast_fix_prefix.rsplit("\n", 1)[-1] | |
| 203 | + | if extra_indent.strip(): | |
| 204 | + | extra_indent = "" # contains not only whitespace | |
| 205 | + | if extra_indent: | |
| 206 | + | line = "\n".join(extra_indent + s for s in line.split("\n")) | |
| 207 | + | column_offset -= len(extra_indent) | |
| 208 | + | ast_fix_prefix = ast_fix_prefix[:-len(extra_indent)] | |
| 196 | 209 | line = ast_fix_prefix + line | |
| 197 | - | line_offset += ast_fix_prefix.count("\n") | |
| 198 | - | column_offset += len(ast_fix_prefix.rsplit("\n", 1)[-1]) | |
| 210 | + | line_offset -= ast_fix_prefix.count("\n") | |
| 199 | 211 | return LogicalLine( | |
| 200 | 212 | line=line, | |
| 201 | 213 | line_offset=line_offset, | |
| ⋯ | |||
| 343 | 355 | and isinstance(parents[0], ast.arguments) | |
| 344 | 356 | and node in parents[0].defaults | |
| 345 | 357 | and pos[0] != end[0] | |
| 358 | + | ): | |
| 359 | + | rewrite_buffer = ProblemRewrite(parens_coord.open_, None) | |
| 360 | + | last_exception_node = node | |
| 361 | + | elif ( | |
| 362 | + | sys.version_info >= (3, 10) | |
| 363 | + | and isinstance(node, ast.MatchSequence) | |
| 346 | 364 | ): | |
| 347 | 365 | rewrite_buffer = ProblemRewrite(parens_coord.open_, None) | |
| 348 | 366 | last_exception_node = node | |
The check that tells the two apart
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-((a))-case_problem_columns10]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-((a,))-case_problem_columns11]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-((a,),)-case_problem_columns12]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-([a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-(a)-case_problem_columns1]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-(a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-(a,)-case_problem_columns3]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-[(a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-[a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-[a]-case_problem_columns4]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-a,-case_problem_columns2]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[(foo)-match_problem_columns1-a-case_problem_columns0]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-((a))-case_problem_columns10]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-((a,))-case_problem_columns11]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-((a,),)-case_problem_columns12]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-([a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-(a)-case_problem_columns1]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-(a,
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-(a,)-case_problem_columns3]
fail→pass·tests/test_redundant_parentheses.py::test_match_case[foo-match_problem_columns0-[(a,
Check file tests/test_redundant_parentheses.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before itd2f530a2a846e8cf454015d75df10c9bf79613bf
Broken version dated2022-10-07
Moduleflake8_picky_parentheses._redundant_parentheses
Units changedPluginRedundantParentheses
Fingerprint73e437afff44109f
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.