diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-06-20 12:37:36 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-06-20 12:47:33 +0200 |
commit | 61fa3eceb32d8f176618f65d20fcd4227cce6979 (patch) | |
tree | f3a959383f7b49ddd51ac30d40686be6fd458283 /pylint | |
parent | 7e859597e5b8cafd1ecfbd339f8c05cee720c7bd (diff) | |
download | pylint-git-61fa3eceb32d8f176618f65d20fcd4227cce6979.tar.gz |
Add a check `consider-using-set-comprehension` which is emitted if for set initialization
the old style with list comprehensions is used.
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/checkers/refactoring.py | 23 | ||||
-rw-r--r-- | pylint/test/functional/consider_using_set_comprehension.py | 9 | ||||
-rw-r--r-- | pylint/test/functional/consider_using_set_comprehension.txt | 1 |
3 files changed, 26 insertions, 7 deletions
diff --git a/pylint/checkers/refactoring.py b/pylint/checkers/refactoring.py index e775dfbb4..96dc278e8 100644 --- a/pylint/checkers/refactoring.py +++ b/pylint/checkers/refactoring.py @@ -167,6 +167,13 @@ class RefactoringChecker(checkers.BaseTokenChecker): 'Also it is faster since you don\'t need to create another ' 'transient list', ), + 'R1718': ('Consider using a set comprehension', + 'consider-using-set-comprehension', + 'Although there is nothing syntactically wrong with this code, ' + 'it is hard to read and can be simplified to a set comprehension.' + 'Also it is faster since you don\'t need to create another ' + 'transient list', + ), } options = (('max-nested-blocks', {'default': 5, 'type': 'int', 'metavar': '<int>', @@ -441,18 +448,20 @@ class RefactoringChecker(checkers.BaseTokenChecker): stopiteration_qname = '{}.StopIteration'.format(utils.EXCEPTIONS_MODULE) return any(_class.qname() == stopiteration_qname for _class in exc.mro()) - def _check_consider_using_dict_comprehension(self, node): + def _check_consider_using_comprehension_constructor(self, node): if (isinstance(node.func, astroid.Name) and - node.func.name == 'dict' and - node.args and - isinstance(node.args[0], astroid.ListComp)): - self.add_message('consider-using-dict-comprehension', node=node) + node.args + and node.func.name in {'dict', 'set'} + and isinstance(node.args[0], astroid.ListComp)): + message_name = 'consider-using-{}-comprehension'.format(node.func.name) + self.add_message(message_name, node=node) @utils.check_messages('stop-iteration-return', - 'consider-using-dict-comprehension') + 'consider-using-dict-comprehension', + 'consider-using-set-comprehension') def visit_call(self, node): self._check_raising_stopiteration_in_generator_next_call(node) - self._check_consider_using_dict_comprehension(node) + self._check_consider_using_comprehension_constructor(node) def _check_raising_stopiteration_in_generator_next_call(self, node): """Check if a StopIteration exception is raised by the call to next function diff --git a/pylint/test/functional/consider_using_set_comprehension.py b/pylint/test/functional/consider_using_set_comprehension.py new file mode 100644 index 000000000..2c42ef2fd --- /dev/null +++ b/pylint/test/functional/consider_using_set_comprehension.py @@ -0,0 +1,9 @@ +# pylint: disable=missing-docstring, invalid-name + +numbers = [1, 2, 3, 4, 5, 6] + +set() + +set([]) + +set([number for number in numbers]) # [consider-using-set-comprehension] diff --git a/pylint/test/functional/consider_using_set_comprehension.txt b/pylint/test/functional/consider_using_set_comprehension.txt new file mode 100644 index 000000000..225d37ae8 --- /dev/null +++ b/pylint/test/functional/consider_using_set_comprehension.txt @@ -0,0 +1 @@ +consider-using-set-comprehension:9::Consider using a set comprehension
\ No newline at end of file |