Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix performance issue due to search in tokens #210

Merged
merged 6 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions flake8_eradicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Checker(object):

options = None

def __init__(self, tree, filename: str):
def __init__(self, tree, filename: str): # noqa: D107
bagerard marked this conversation as resolved.
Show resolved Hide resolved
self.filename = filename

self._options = {
Expand Down Expand Up @@ -119,7 +119,10 @@ def _lines_with_commented_out_code(self) -> Iterable[int]:
"""
with open(self.filename) as f:
bagerard marked this conversation as resolved.
Show resolved Hide resolved
file_tokens = tokenize.generate_tokens(f.readline)
comment_in_file = any(token.type == tokenize.COMMENT for token in file_tokens)
comment_in_file = any(
token.type == tokenize.COMMENT
for token in file_tokens
)

if comment_in_file:
f.seek(0) # rewind file
Expand Down
14 changes: 11 additions & 3 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,19 @@ def test_incorrect_fixture_whitelist_extend(absolute_path):
assert b'# typed_property: int = 10' in stdout


def test__lines_with_commented_out_code_incorrect_fixture_output(absolute_path):
def test_lines_with_commented_out_code_incorrect_fixture_output(absolute_path):
"""Verify central underlying method is returning correct output."""
filename = absolute_path('fixtures', 'incorrect.py')

OptionsStub = namedtuple('Options', 'eradicate_aggressive eradicate_whitelist eradicate_whitelist_extend')
Checker.options = OptionsStub(eradicate_aggressive=True, eradicate_whitelist=False, eradicate_whitelist_extend=False)
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())
Expand Down