summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--doc/whatsnew/2.9.rst4
-rw-r--r--pylint/checkers/refactoring/refactoring_checker.py27
-rw-r--r--pylint/extensions/code_style.py58
-rw-r--r--pylintrc3
-rw-r--r--tests/functional/ext/code_style/consider_using_tuple.py (renamed from tests/functional/c/consider/consider_using_tuple.py)0
-rw-r--r--tests/functional/ext/code_style/consider_using_tuple.rc2
-rw-r--r--tests/functional/ext/code_style/consider_using_tuple.txt (renamed from tests/functional/c/consider/consider_using_tuple.txt)0
8 files changed, 69 insertions, 30 deletions
diff --git a/ChangeLog b/ChangeLog
index 7a13bb357..5ae496476 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -139,8 +139,9 @@ modules are added.
Closes #4545
-* New checker ``consider-using-tuple``. Emitted when an in-place defined
- list or set can be replaced by a tuple.
+* Add new extension ``CodeStyleChecker``. It includes checkers that can improve code
+ consistency. As such they don't necessarily provide a performance benefit
+ and are often times opinionated.
What's New in Pylint 2.8.3?
diff --git a/doc/whatsnew/2.9.rst b/doc/whatsnew/2.9.rst
index 59321d8d3..d267d8ba3 100644
--- a/doc/whatsnew/2.9.rst
+++ b/doc/whatsnew/2.9.rst
@@ -39,7 +39,9 @@ New checkers
* New checker ``await-outside-async``: Emitted when await is used outside an async function.
-* ``consider-using-tuple``: Emitted when an in-place defined list or set can be replaced by a tuple.
+* Add new extension ``CodeStyleChecker``. It includes checkers that can improve code
+ consistency. As such they don't necessarily provide a performance benefit
+ and are often times opinionated.
Other Changes
diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py
index 5a3e34fb2..7a57584ab 100644
--- a/pylint/checkers/refactoring/refactoring_checker.py
+++ b/pylint/checkers/refactoring/refactoring_checker.py
@@ -360,11 +360,6 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"consider-using-namedtuple-or-dataclass",
"Emitted when dictionary values can be replaced by namedtuples or dataclass instances.",
),
- "R1735": (
- "Consider using an in-place tuple%s",
- "consider-using-tuple",
- "Emitted when an in-place defined list or set can be replaced by a tuple.",
- ),
}
options = (
(
@@ -556,12 +551,10 @@ class RefactoringChecker(checkers.BaseTokenChecker):
"redefined-argument-from-local",
"too-many-nested-blocks",
"unnecessary-dict-index-lookup",
- "consider-using-tuple",
)
def visit_for(self, node):
self._check_nested_blocks(node)
self._check_unnecessary_dict_index_lookup(node)
- self._check_consider_tuple_iterator(node)
for name in node.target.nodes_of_class(astroid.AssignName):
self._check_redefined_argument_from_local(name)
@@ -1355,15 +1348,10 @@ class RefactoringChecker(checkers.BaseTokenChecker):
def visit_augassign(self, node):
self._check_consider_using_join(node)
- @utils.check_messages(
- "unnecessary-comprehension",
- "unnecessary-dict-index-lookup",
- "consider-using-tuple",
- )
+ @utils.check_messages("unnecessary-comprehension", "unnecessary-dict-index-lookup")
def visit_comprehension(self, node: astroid.Comprehension) -> None:
self._check_unnecessary_comprehension(node)
self._check_unnecessary_dict_index_lookup(node)
- self._check_consider_tuple_iterator(node)
def _check_unnecessary_comprehension(self, node: astroid.Comprehension) -> None:
if (
@@ -1775,19 +1763,6 @@ class RefactoringChecker(checkers.BaseTokenChecker):
args=("1".join(value.as_string().rsplit("0", maxsplit=1)),),
)
- def _check_consider_tuple_iterator(
- self, node: Union[astroid.For, astroid.Comprehension]
- ) -> None:
- """Check if inplace defined iterator can be replaced by a tuple."""
- if isinstance(node.iter, (astroid.List, astroid.Set)) and not any(
- isinstance(item, astroid.Starred) for item in node.iter.elts
- ):
- self.add_message(
- "consider-using-tuple",
- node=node.iter,
- args=(f" instead of {node.iter.__class__.__qualname__.lower()}"),
- )
-
@utils.check_messages("consider-using-namedtuple-or-dataclass")
def visit_dict(self, node: astroid.Dict) -> None:
self._check_dict_consider_namedtuple(node)
diff --git a/pylint/extensions/code_style.py b/pylint/extensions/code_style.py
new file mode 100644
index 000000000..e670b41ed
--- /dev/null
+++ b/pylint/extensions/code_style.py
@@ -0,0 +1,58 @@
+from typing import Union
+
+import astroid
+
+from pylint.checkers import BaseChecker
+from pylint.checkers.utils import check_messages
+from pylint.interfaces import IAstroidChecker
+from pylint.lint import PyLinter
+
+
+class CodeStyleChecker(BaseChecker):
+ """Checkers that can improve code consistency.
+
+ As such they don't necessarily provide a performance benefit and
+ are often times opinionated.
+ """
+
+ __implements__ = (IAstroidChecker,)
+
+ name = "code_style"
+ priority = -1
+ msgs = {
+ "R6102": (
+ "Consider using an in-place tuple%s",
+ "consider-using-tuple",
+ "Emitted when an in-place defined list or set can be "
+ "replaced by a slightly faster tuple.",
+ ),
+ }
+
+ def __init__(self, linter: PyLinter) -> None:
+ """Initialize checker instance."""
+ super().__init__(linter=linter)
+
+ @check_messages("consider-using-tuple")
+ def visit_for(self, node: astroid.For) -> None:
+ self._check_inplace_defined_list_set(node)
+
+ @check_messages("consider-using-tuple")
+ def visit_comprehension(self, node: astroid.Comprehension) -> None:
+ self._check_inplace_defined_list_set(node)
+
+ def _check_inplace_defined_list_set(
+ self, node: Union[astroid.For, astroid.Comprehension]
+ ) -> None:
+ """Check if inplace defined list / set can be replaced by a tuple."""
+ if isinstance(node.iter, (astroid.List, astroid.Set)) and not any(
+ isinstance(item, astroid.Starred) for item in node.iter.elts
+ ):
+ self.add_message(
+ "consider-using-tuple",
+ node=node.iter,
+ args=(f" instead of {node.iter.__class__.__qualname__.lower()}"),
+ )
+
+
+def register(linter: PyLinter) -> None:
+ linter.register_checker(CodeStyleChecker(linter))
diff --git a/pylintrc b/pylintrc
index a9be565fe..e488b1354 100644
--- a/pylintrc
+++ b/pylintrc
@@ -18,7 +18,8 @@ persistent=yes
# usually to register additional checkers.
load-plugins=
pylint.extensions.check_elif,
- pylint.extensions.bad_builtin
+ pylint.extensions.bad_builtin,
+ pylint.extensions.code_style,
# Use multiple processes to speed up Pylint.
jobs=1
diff --git a/tests/functional/c/consider/consider_using_tuple.py b/tests/functional/ext/code_style/consider_using_tuple.py
index 679ef77d2..679ef77d2 100644
--- a/tests/functional/c/consider/consider_using_tuple.py
+++ b/tests/functional/ext/code_style/consider_using_tuple.py
diff --git a/tests/functional/ext/code_style/consider_using_tuple.rc b/tests/functional/ext/code_style/consider_using_tuple.rc
new file mode 100644
index 000000000..47767a206
--- /dev/null
+++ b/tests/functional/ext/code_style/consider_using_tuple.rc
@@ -0,0 +1,2 @@
+[MASTER]
+load-plugins=pylint.extensions.code_style
diff --git a/tests/functional/c/consider/consider_using_tuple.txt b/tests/functional/ext/code_style/consider_using_tuple.txt
index df796b683..df796b683 100644
--- a/tests/functional/c/consider/consider_using_tuple.txt
+++ b/tests/functional/ext/code_style/consider_using_tuple.txt