Skip to content

Commit 5a810b4

Browse files
committed
Extended consider-using-tuple check to cover 'in' comparisons
1 parent c00b07b commit 5a810b4

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

ChangeLog

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ Pylint's ChangeLog
33
------------------
44

55

6-
76
What's New in Pylint 2.10.0?
87
============================
98
Release date: TBA
109

11-
* Added ``ignored-parents`` option to the design checker to ignore specific
12-
classes from the ``too-many-ancestors`` check (R0901).
13-
14-
Partially closes #3057
15-
1610
..
1711
Put new features here and also in 'doc/whatsnew/2.10.rst'
1812
..
1913
Put bug fixes that should not wait for a new minor version here
2014

15+
* Added ``ignored-parents`` option to the design checker to ignore specific
16+
classes from the ``too-many-ancestors`` check (R0901).
17+
18+
Partially closes #3057
19+
2120
* Added ``unspecified-encoding``: Emitted when open() is called without specifying an encoding
2221

2322
Closes #3826
@@ -37,13 +36,15 @@ Release date: TBA
3736

3837
Closes #3878
3938

39+
* ``CodeStyleChecker``
40+
41+
* Extended ``consider-using-tuple`` check to cover ``in`` comparisons.
4042

4143

4244
What's New in Pylint 2.9.6?
4345
===========================
4446
Release date: 2021-07-28
4547

46-
4748
* Fix a false positive ``undefined-variable`` when variable name in decoration
4849
matches function argument
4950

doc/whatsnew/2.10.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ New checkers
1717
Closes #3826
1818

1919

20+
Extensions
21+
==========
22+
23+
* ``CodeStyleChecker``
24+
25+
* Extended ``consider-using-tuple`` check to cover ``in`` comparisons.
26+
27+
2028
Other Changes
2129
=============
2230

23-
2431
* Performance of the Similarity checker has been improved.
2532

2633
* Added ``time.clock`` to deprecated functions/methods for python 3.3

pylint/extensions/code_style.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Set, Tuple, Type, Union
1+
from typing import List, Set, Tuple, Type
22

33
import astroid
44
from astroid.node_classes import NodeNG
@@ -53,11 +53,18 @@ def visit_dict(self, node: astroid.Dict) -> None:
5353

5454
@check_messages("consider-using-tuple")
5555
def visit_for(self, node: astroid.For) -> None:
56-
self._check_inplace_defined_list_set(node)
56+
self._check_inplace_defined_list_set(node.iter)
5757

5858
@check_messages("consider-using-tuple")
5959
def visit_comprehension(self, node: astroid.Comprehension) -> None:
60-
self._check_inplace_defined_list_set(node)
60+
self._check_inplace_defined_list_set(node.iter)
61+
62+
@check_messages("consider-using-tuple")
63+
def visit_compare(self, node: astroid.Compare) -> None:
64+
for op, comparator in node.ops:
65+
if op != "in":
66+
continue
67+
self._check_inplace_defined_list_set(comparator)
6168

6269
def _check_dict_consider_namedtuple_dataclass(self, node: astroid.Dict) -> None:
6370
"""Check if dictionary values can be replaced by Namedtuple or Dataclass."""
@@ -128,17 +135,15 @@ def _check_dict_consider_namedtuple_dataclass(self, node: astroid.Dict) -> None:
128135
self.add_message("consider-using-namedtuple-or-dataclass", node=node)
129136
return
130137

131-
def _check_inplace_defined_list_set(
132-
self, node: Union[astroid.For, astroid.Comprehension]
133-
) -> None:
138+
def _check_inplace_defined_list_set(self, node: NodeNG) -> None:
134139
"""Check if inplace defined list / set can be replaced by a tuple."""
135-
if isinstance(node.iter, (astroid.List, astroid.Set)) and not any(
136-
isinstance(item, astroid.Starred) for item in node.iter.elts
140+
if isinstance(node, (astroid.List, astroid.Set)) and not any(
141+
isinstance(item, astroid.Starred) for item in node.elts
137142
):
138143
self.add_message(
139144
"consider-using-tuple",
140-
node=node.iter,
141-
args=(f" instead of {node.iter.__class__.__qualname__.lower()}"),
145+
node=node,
146+
args=(f" instead of {node.__class__.__qualname__.lower()}",),
142147
)
143148

144149

tests/functional/ext/code_style/code_style_consider_using_tuple.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,30 @@
2727

2828
[x for x in [*var, 2]]
2929
[x for x in {*var, 2}]
30+
31+
32+
# -----
33+
# Suggest tuple for `in` comparisons
34+
x in var
35+
x in (1, 2, 3)
36+
x in [1, 2, 3] # [consider-using-tuple]
37+
38+
if x in var:
39+
pass
40+
if x in (1, 2, 3):
41+
pass
42+
if x in [1, 2, 3]: # [consider-using-tuple]
43+
pass
44+
if x in {1, 2, 3}: # [consider-using-tuple]
45+
pass
46+
47+
42 if x in [1, 2, 3] else None # [consider-using-tuple]
48+
assert x in [1, 2, 3] # [consider-using-tuple]
49+
(x for x in var if x in [1, 2, 3]) # [consider-using-tuple]
50+
while x in [1, 2, 3]: # [consider-using-tuple]
51+
break
52+
53+
# Stacked operators, rightmost pair is evaluated first
54+
# Doesn't make much sense in practice since `in` will only return `bool`
55+
True == x in [1, 2, 3] # [consider-using-tuple] # noqa: E712
56+
1 >= x in [1, 2, 3] # [consider-using-tuple] # noqa: E712
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
consider-using-tuple:9:9::Consider using an in-place tuple instead of list
2-
consider-using-tuple:14:12::Consider using an in-place tuple instead of list
3-
consider-using-tuple:15:12::Consider using an in-place tuple instead of set
4-
consider-using-tuple:19:12::Consider using an in-place tuple instead of list
1+
consider-using-tuple:9:9::Consider using an in-place tuple instead of list:HIGH
2+
consider-using-tuple:14:12::Consider using an in-place tuple instead of list:HIGH
3+
consider-using-tuple:15:12::Consider using an in-place tuple instead of set:HIGH
4+
consider-using-tuple:19:12::Consider using an in-place tuple instead of list:HIGH
5+
consider-using-tuple:36:5::Consider using an in-place tuple instead of list:HIGH
6+
consider-using-tuple:42:8::Consider using an in-place tuple instead of list:HIGH
7+
consider-using-tuple:44:8::Consider using an in-place tuple instead of set:HIGH
8+
consider-using-tuple:47:11::Consider using an in-place tuple instead of list:HIGH
9+
consider-using-tuple:48:12::Consider using an in-place tuple instead of list:HIGH
10+
consider-using-tuple:49:24::Consider using an in-place tuple instead of list:HIGH
11+
consider-using-tuple:50:11::Consider using an in-place tuple instead of list:HIGH
12+
consider-using-tuple:55:13::Consider using an in-place tuple instead of list:HIGH
13+
consider-using-tuple:56:10::Consider using an in-place tuple instead of list:HIGH

0 commit comments

Comments
 (0)