From 211d8220bb6be7f8d41d4c3765c9dc047eaeda71 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Fri, 16 Sep 2022 19:50:38 +0100 Subject: [PATCH] Teach the E265 fixer to fix only E265 Previously it would end up changing E266 as well, which the user may not want it to do (for example if they've disabled E266). This also adds a battery of tests to excercise the differences between these errors. --- autopep8.py | 7 +++++-- test/test_autopep8.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/autopep8.py b/autopep8.py index f93273aa..e5b73c11 100755 --- a/autopep8.py +++ b/autopep8.py @@ -825,9 +825,12 @@ def fix_e265(self, result): target = self.source[result['line'] - 1] indent = _get_indentation(target) - comment = target.lstrip(' \t#') + line = target.lstrip(' \t') + pos = next((index for index, c in enumerate(line) if c != '#')) + hashes = line[:pos] + comment = line[pos:].lstrip(' \t') - fixed = indent + ('# ' + comment if comment.strip() else '\n') + fixed = indent + hashes + (' ' + comment if comment.strip() else '\n') self.source[result['line'] - 1] = fixed diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 7db4a246..7aa8ca0a 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -2232,12 +2232,36 @@ def test_e265(self): with autopep8_context(line) as result: self.assertEqual(fixed, result) + def test_e265_only(self): + line = "##A comment\n#B comment\n123\n" + fixed = "## A comment\n# B comment\n123\n" + with autopep8_context(line, options=['--select=E265']) as result: + self.assertEqual(fixed, result) + + def test_ignore_e265(self): + line = "## A comment\n#B comment\n123\n" + fixed = "# A comment\n#B comment\n123\n" + with autopep8_context(line, options=['--ignore=E265']) as result: + self.assertEqual(fixed, result) + def test_e266(self): line = "## comment\n123\n" fixed = "# comment\n123\n" with autopep8_context(line) as result: self.assertEqual(fixed, result) + def test_e266_only(self): + line = "## A comment\n#B comment\n123\n" + fixed = "# A comment\n#B comment\n123\n" + with autopep8_context(line, options=['--select=E266']) as result: + self.assertEqual(fixed, result) + + def test_ignore_e266(self): + line = "##A comment\n#B comment\n123\n" + fixed = "## A comment\n# B comment\n123\n" + with autopep8_context(line, options=['--ignore=E266']) as result: + self.assertEqual(fixed, result) + def test_e271(self): line = 'True and False\n' fixed = 'True and False\n'