-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR Review: refactor checker messages, test cases added: empty commit case, comments row case. unittest - pathlib replaces os.path. python3.5 fix for pathlib library use refactor `comment_ending` to `empty_comment` empty-comment test fixed (after Rebase)
- Loading branch information
1 parent
98d2c06
commit d66cd83
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from pylint.checkers import BaseChecker | ||
from pylint.interfaces import IRawChecker | ||
|
||
|
||
def is_line_commented(line): | ||
""" Checks if a `# symbol that is not part of a string was found in line""" | ||
|
||
comment_idx = line.find(b"#") | ||
if comment_idx == -1: | ||
return False | ||
if comment_part_of_string(line, comment_idx): | ||
return is_line_commented(line[:comment_idx] + line[comment_idx + 1 :]) | ||
return True | ||
|
||
|
||
def comment_part_of_string(line, comment_idx): | ||
""" checks if the symbol at comment_idx is part of a string """ | ||
|
||
if ( | ||
line[:comment_idx].count(b"'") % 2 == 1 | ||
and line[comment_idx:].count(b"'") % 2 == 1 | ||
) or ( | ||
line[:comment_idx].count(b'"') % 2 == 1 | ||
and line[comment_idx:].count(b'"') % 2 == 1 | ||
): | ||
return True | ||
return False | ||
|
||
|
||
class CommentChecker(BaseChecker): | ||
__implements__ = IRawChecker | ||
|
||
name = "refactoring" | ||
msgs = { | ||
"R2044": ( | ||
"Line with empty comment", | ||
"empty-comment", | ||
( | ||
"Used when a # symbol appears on a line not followed by an actual comment" | ||
), | ||
) | ||
} | ||
options = () | ||
priority = -1 # low priority | ||
|
||
def process_module(self, node): | ||
with node.stream() as stream: | ||
for (line_num, line) in enumerate(stream): | ||
line = line.rstrip() | ||
if line.endswith(b"#"): | ||
if not is_line_commented(line[:-1]): | ||
self.add_message("empty-comment", line=line_num) | ||
|
||
|
||
def register(linter): | ||
linter.register_checker(CommentChecker(linter)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""empty-comment test-case""" | ||
A = 5 # | ||
# | ||
A = '#' + '1' | ||
print(A) # | ||
print("A=", A) # should not be an error# | ||
A = "#pe\0ace#love#" # | ||
A = "peace#love" # \0 peace'#'''' love#peace'''-'#love'-"peace#love"# | ||
####### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import pylint.extensions.empty_comment as empty_comment | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def checker(): | ||
return empty_comment.CommentChecker | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def enable(): | ||
return ["empty-comment"] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def disable(): | ||
return ["all"] | ||
|
||
|
||
def test_comment_base_case(linter): | ||
comment_test = str(Path(__file__).parent.joinpath("data", "empty_comment.py")) | ||
linter.check([comment_test]) | ||
msgs = linter.reporter.messages | ||
assert len(msgs) == 4 | ||
for msg in msgs: | ||
assert msg.symbol == "empty-comment" | ||
assert msg.msg == "Line with empty comment" | ||
assert msgs[0].line == 1 | ||
assert msgs[1].line == 2 | ||
assert msgs[2].line == 4 | ||
assert msgs[3].line == 6 |