From ce8a334b11b3a5967de42e84ea664feae456eb0d Mon Sep 17 00:00:00 2001 From: Florian Best Date: Fri, 17 Sep 2021 00:30:45 +0200 Subject: [PATCH] Issue #582: disallow tabs before inline comments --- pycodestyle.py | 6 ++++-- testsuite/E26.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 68d31d40f..300367b7a 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1092,9 +1092,11 @@ def whitespace_before_comment(logical_line, tokens): prev_end = (0, 0) for token_type, text, start, end, line in tokens: if token_type == tokenize.COMMENT: - inline_comment = line[:start[1]].strip() + inline_comment = line[:start[1]].strip(' ') if inline_comment: - if prev_end[0] == start[0] and start[1] < prev_end[1] + 2: + contains_tab = '\t' in line[prev_end[1]: prev_end[1] + 2] + nottwo = prev_end[0] == start[0] and start[1] < prev_end[1] + 2 + if contains_tab or nottwo: yield (prev_end, "E261 at least two spaces before inline comment") symbol, sp, comment = text.partition(' ') diff --git a/testsuite/E26.py b/testsuite/E26.py index c3537ff50..fea612a86 100644 --- a/testsuite/E26.py +++ b/testsuite/E26.py @@ -1,8 +1,16 @@ #: E261:1:5 pass # an inline comment +#: E261:1:5 +pass # an inline comment +#: E261:1:5 +pass # an inline comment +#: E261:1:5 +pass # an inline comment #: E262:1:12 x = x + 1 #Increment x #: E262:1:12 +x = x + 1 # Increment x +#: E262:1:12 x = x + 1 # Increment x #: E262:1:12 x = y + 1 #: Increment x