From dfd2520c7c62564888588ed1157ce8fa0ef4cb06 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 21 Oct 2022 16:02:25 -0600 Subject: [PATCH 1/5] fix: Ignore pass two lines after docstring with flag --- autoflake.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/autoflake.py b/autoflake.py index 674c1c4..21f50a2 100755 --- a/autoflake.py +++ b/autoflake.py @@ -753,7 +753,7 @@ def useless_pass_line_numbers( previous_token_type = None last_pass_row = None last_pass_indentation = None - previous_line = "" + previous_lines = collections.deque(("", ""), maxlen=2) for token in tokenize.generate_tokens(sio.readline): token_type = token[0] start_row = token[2][0] @@ -776,12 +776,18 @@ def useless_pass_line_numbers( is_trailing_pass = ( previous_token_type != tokenize.INDENT - and not previous_line.rstrip().endswith("\\") + and not previous_lines[1].rstrip().endswith("\\") ) is_pass_after_docstring = ( + # previous line is the end of a docstring previous_token_type == tokenize.NEWLINE - and previous_line.rstrip().endswith('"""') + and previous_lines[1].rstrip().endswith(("'''", '"""')) + ) or ( + # previous line contains only space and the line before that is + # the end of a docstring + previous_lines[1].strip() == "" + and previous_lines[0].rstrip().endswith(("'''", '"""')) ) # Trailing "pass". @@ -792,7 +798,7 @@ def useless_pass_line_numbers( yield start_row previous_token_type = token_type - previous_line = line + previous_lines.append(line) def filter_useless_pass( From 247def11c5f22cf5992f678f383aad44e864a9a5 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 21 Oct 2022 16:02:51 -0600 Subject: [PATCH 2/5] Update test_autoflake.py --- test_autoflake.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test_autoflake.py b/test_autoflake.py index 47362de..c0bf381 100755 --- a/test_autoflake.py +++ b/test_autoflake.py @@ -1418,6 +1418,10 @@ def test_fix_code_keeps_pass_statements(self): else: def foo(): \"\"\" A docstring. \"\"\" + pass + def foo2(): + \"\"\" A docstring. \"\"\" + pass def bar(): # abc @@ -1447,6 +1451,10 @@ def test_fix_code_keeps_passes_after_docstrings(self): else: def foo(): \"\"\" A docstring. \"\"\" + pass + def foo2(): + \"\"\" A docstring. \"\"\" + pass def bar(): # abc @@ -1466,6 +1474,10 @@ def blah(): else: def foo(): \"\"\" A docstring. \"\"\" + pass + def foo2(): + \"\"\" A docstring. \"\"\" + pass def bar(): # abc From 23114786025a1273e000af24b3ec72a2acd16b06 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 21 Oct 2022 16:10:37 -0600 Subject: [PATCH 3/5] Use -2, -1 instead of 0, 1 for readability --- autoflake.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autoflake.py b/autoflake.py index 21f50a2..d10dbf8 100755 --- a/autoflake.py +++ b/autoflake.py @@ -776,18 +776,18 @@ def useless_pass_line_numbers( is_trailing_pass = ( previous_token_type != tokenize.INDENT - and not previous_lines[1].rstrip().endswith("\\") + and not previous_lines[-1].rstrip().endswith("\\") ) is_pass_after_docstring = ( # previous line is the end of a docstring previous_token_type == tokenize.NEWLINE - and previous_lines[1].rstrip().endswith(("'''", '"""')) + and previous_lines[-1].rstrip().endswith(("'''", '"""')) ) or ( # previous line contains only space and the line before that is # the end of a docstring - previous_lines[1].strip() == "" - and previous_lines[0].rstrip().endswith(("'''", '"""')) + previous_lines[-1].strip() == "" + and previous_lines[-2].rstrip().endswith(("'''", '"""')) ) # Trailing "pass". From ebea160e8b0f4864e1af33a97a08379e664912c5 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 23 Oct 2022 02:20:55 +0000 Subject: [PATCH 4/5] feat: support for any number of empty lines --- autoflake.py | 20 +++++++------------- test_autoflake.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/autoflake.py b/autoflake.py index d10dbf8..022ffb7 100755 --- a/autoflake.py +++ b/autoflake.py @@ -753,7 +753,8 @@ def useless_pass_line_numbers( previous_token_type = None last_pass_row = None last_pass_indentation = None - previous_lines = collections.deque(("", ""), maxlen=2) + previous_line = "" + previous_non_empty_line = "" for token in tokenize.generate_tokens(sio.readline): token_type = token[0] start_row = token[2][0] @@ -776,19 +777,10 @@ def useless_pass_line_numbers( is_trailing_pass = ( previous_token_type != tokenize.INDENT - and not previous_lines[-1].rstrip().endswith("\\") + and not previous_line.rstrip().endswith("\\") ) - is_pass_after_docstring = ( - # previous line is the end of a docstring - previous_token_type == tokenize.NEWLINE - and previous_lines[-1].rstrip().endswith(("'''", '"""')) - ) or ( - # previous line contains only space and the line before that is - # the end of a docstring - previous_lines[-1].strip() == "" - and previous_lines[-2].rstrip().endswith(("'''", '"""')) - ) + is_pass_after_docstring = previous_non_empty_line.rstrip().endswith(("'''", '"""')) # Trailing "pass". if is_trailing_pass: @@ -798,7 +790,9 @@ def useless_pass_line_numbers( yield start_row previous_token_type = token_type - previous_lines.append(line) + previous_line = line + if line.strip(): + previous_non_empty_line = line def filter_useless_pass( diff --git a/test_autoflake.py b/test_autoflake.py index c0bf381..e53e279 100755 --- a/test_autoflake.py +++ b/test_autoflake.py @@ -1422,6 +1422,11 @@ def foo(): def foo2(): \"\"\" A docstring. \"\"\" + pass + def foo3(): + \"\"\" A docstring. \"\"\" + + pass def bar(): # abc @@ -1455,6 +1460,11 @@ def foo(): def foo2(): \"\"\" A docstring. \"\"\" + pass + def foo3(): + \"\"\" A docstring. \"\"\" + + pass def bar(): # abc @@ -1478,6 +1488,11 @@ def foo(): def foo2(): \"\"\" A docstring. \"\"\" + pass + def foo3(): + \"\"\" A docstring. \"\"\" + + pass def bar(): # abc From bbe2f1dbc9d10b830c8cbe314948f634ab99aa87 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 23 Oct 2022 02:28:00 +0000 Subject: [PATCH 5/5] style: pre-commit run --- autoflake.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autoflake.py b/autoflake.py index 022ffb7..c71146a 100755 --- a/autoflake.py +++ b/autoflake.py @@ -780,7 +780,9 @@ def useless_pass_line_numbers( and not previous_line.rstrip().endswith("\\") ) - is_pass_after_docstring = previous_non_empty_line.rstrip().endswith(("'''", '"""')) + is_pass_after_docstring = previous_non_empty_line.rstrip().endswith( + ("'''", '"""'), + ) # Trailing "pass". if is_trailing_pass: