From 7e34ae4fb75c4143c7f2f537ddc97c18f4592f82 Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Fri, 21 May 2021 10:46:23 +0200 Subject: [PATCH] fix from review. --- flake8_eradicate.py | 23 +++++++++++++++++++---- tests/fixtures/correct_no_comment.py | 16 ++++++++++++++++ tests/test_comments.py | 25 +++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/correct_no_comment.py diff --git a/flake8_eradicate.py b/flake8_eradicate.py index 611140c..b4c15c1 100644 --- a/flake8_eradicate.py +++ b/flake8_eradicate.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- +import ast import tokenize -from typing import Iterable, Tuple +from typing import Iterable, Iterator, Tuple, Type import pkg_resources from eradicate import Eradicator @@ -25,7 +26,15 @@ class Checker(object): options = None - def __init__(self, tree, filename: str): # noqa: D107 + def __init__(self, tree: ast.AST, filename: str): + """ + ``flake8`` plugin constructor. + + Arguments: + tree: the file abstract syntax tree. + filename: the name of the file to process + + """ self.filename = filename self._options = { @@ -98,7 +107,7 @@ def parse_options(cls, options) -> None: """Parses registered options for providing them to each visitor.""" cls.options = options - def run(self) -> Iterable[Tuple[int, str]]: + def run(self) -> Iterator[Tuple[int, int, str, Type['Checker']]]: """Runs on each step of flake8.""" for line_no in self._lines_with_commented_out_code(): yield line_no, 0, self._error_template, type(self) @@ -118,6 +127,12 @@ def _lines_with_commented_out_code(self) -> Iterable[int]: when the tokens indicate a comment in the physical line. """ with open(self.filename) as f: + # Skip python commented encoding line + first_line = f.readline() + if not first_line.startswith('# -*- coding: utf-8 -*-'): + # rewind as we don't want to skip it during tokenization + f.seek(0) + file_tokens = tokenize.generate_tokens(f.readline) comment_in_file = any( token.type == tokenize.COMMENT @@ -125,7 +140,7 @@ def _lines_with_commented_out_code(self) -> Iterable[int]: ) if comment_in_file: - f.seek(0) # rewind file + f.seek(0) for line_no, line in enumerate(f.readlines(), start=1): filtered_source = ''.join( self._eradicator.filter_commented_out_code( diff --git a/tests/fixtures/correct_no_comment.py b/tests/fixtures/correct_no_comment.py new file mode 100644 index 0000000..f602be7 --- /dev/null +++ b/tests/fixtures/correct_no_comment.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +class Some(object): + + property_name = 1 + + other_property: int = 2 + + def some_method(self) -> None: + print('not True and not False') + + print(12 + 23 / 3) + + +def some_function(): + return "something" diff --git a/tests/test_comments.py b/tests/test_comments.py index 4ed8db7..c598614 100644 --- a/tests/test_comments.py +++ b/tests/test_comments.py @@ -136,12 +136,12 @@ def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path): OptionsStub = namedtuple( 'Options', - 'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend' + 'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend', ) Checker.options = OptionsStub( eradicate_aggressive=True, eradicate_whitelist=False, - eradicate_whitelist_extend=False + eradicate_whitelist_extend=False, ) checker = Checker(tree=None, filename=filename) @@ -150,3 +150,24 @@ def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path): assert output == [3, 4, 9, 10, 14, 15, 16, 18, 19, 21, 22, 24, 25] else: assert output == [3, 9, 10, 14, 15, 16, 18, 19, 21, 22, 24, 25] + + +def test_lines_with_commented_out_code_file_without_comment_output( + absolute_path, +): + """Verify central underlying method is returning correct output.""" + filename = absolute_path('fixtures', 'correct_no_comment.py') + + OptionsStub = namedtuple( + 'Options', + 'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend', + ) + Checker.options = OptionsStub( + eradicate_aggressive=True, + eradicate_whitelist=False, + eradicate_whitelist_extend=False, + ) + + checker = Checker(tree=None, filename=filename) + output = list(checker._lines_with_commented_out_code()) + assert output == []