Skip to content

Commit

Permalink
Use flake8 builtin file_tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-bagerard committed May 25, 2021
1 parent 4483ce2 commit 50f30fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 9 additions & 5 deletions flake8_eradicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ast
import tokenize
from typing import Iterable, Iterator, Tuple, Type
from typing import Iterable, Iterator, List, Tuple, Type

import pkg_resources
from eradicate import Eradicator
Expand All @@ -26,16 +26,21 @@ class Checker(object):

options = None

def __init__(self, tree: ast.AST, filename: str):
def __init__(
self, tree: ast.AST,
filename: str,
file_tokens: List[tokenize.TokenInfo],
):
"""
``flake8`` plugin constructor.
Arguments:
tree: the file abstract syntax tree.
filename: the name of the file to process
file_tokens: file tokens
"""
self.filename = filename
self.file_tokens = file_tokens

self._options = {
'aggressive': self.options.eradicate_aggressive, # type: ignore
Expand Down Expand Up @@ -133,10 +138,9 @@ def _lines_with_commented_out_code(self) -> Iterable[int]:
# 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
for token in file_tokens
for token in self.file_tokens
)

if comment_in_file:
Expand Down
2 changes: 0 additions & 2 deletions tests/fixtures/correct_no_comment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

class Some(object):

property_name = 1
Expand Down
20 changes: 17 additions & 3 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import subprocess
import sys
import tokenize
from collections import namedtuple

from flake8_eradicate import Checker

PY_GTE_36 = sys.version_info >= (3, 6)


def _get_file_tokens(filename: str):
with open(filename) as f:
return list(tokenize.generate_tokens(f.readline))


def test_correct_fixture(absolute_path):
"""End-to-End test to check that correct code works."""
filename = absolute_path('fixtures', 'correct.py')
Expand Down Expand Up @@ -144,15 +150,19 @@ def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path):
eradicate_whitelist_extend=False,
)

checker = Checker(tree=None, filename=filename)
checker = Checker(
tree=None,
filename=filename,
file_tokens=_get_file_tokens(filename),
)
output = list(checker._lines_with_commented_out_code())
if PY_GTE_36:
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(
def test_lines_with_commented_out_code_file_no_comment(
absolute_path,
):
"""Make sure file without comment are ignored."""
Expand All @@ -168,6 +178,10 @@ def test_lines_with_commented_out_code_file_without_comment_output(
eradicate_whitelist_extend=False,
)

checker = Checker(tree=None, filename=filename)
checker = Checker(
tree=None,
filename=filename,
file_tokens=_get_file_tokens(filename),
)
output = list(checker._lines_with_commented_out_code())
assert output == []

0 comments on commit 50f30fa

Please sign in to comment.