-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added checker for multiline list comprehensions ban (#48)
- Loading branch information
Showing
5 changed files
with
61 additions
and
1 deletion.
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
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,23 @@ | ||
from astroid import nodes # type: ignore | ||
from pylint.checkers import BaseChecker | ||
|
||
|
||
class ReadabilityChecker(BaseChecker): | ||
name = "readability" | ||
msgs = { | ||
"R8923": ( | ||
"List comprehension spans multiple lines, rewrite as for loop", | ||
"rewrite-as-for-loop", | ||
"""List comprehensions in Python are typically used to create new lists by iterating over an existing | ||
iterable in a concise, one-line syntax. However, when a list comprehension becomes too complex or spans | ||
multiple lines, it may lose its readability and clarity, which are key advantages of Python's syntax.""", | ||
), | ||
} | ||
|
||
def visit_listcomp(self, node: nodes.ListComp) -> None: | ||
if node.lineno != node.end_lineno: | ||
self.add_message("rewrite-as-for-loop", node=node) | ||
|
||
|
||
def register(linter): | ||
linter.register_checker(ReadabilityChecker(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,16 @@ | ||
from databricks.labs.pylint.readability import ReadabilityChecker | ||
|
||
|
||
def test_single_line_list_comprehensions_allowed(lint_with): | ||
messages = lint_with(ReadabilityChecker) << """[x for x in range(10) if x % 2 == 0]""" | ||
assert not messages | ||
|
||
|
||
def test_multi_line_list_comprehensions_not_allowed(lint_with): | ||
messages = ( | ||
lint_with(ReadabilityChecker) | ||
<< """[ | ||
x for x in range(10) if x % 2 == 0 | ||
]""" | ||
) | ||
assert messages == {"[rewrite-as-for-loop] List comprehension spans multiple lines, rewrite as for loop"} |