Skip to content

Commit 813aab5

Browse files
Upgrade to pylint 2.12 and enable for_any_all checker (#1277)
* Enable for_any_all check * Upgrade to pylint 2.12 in pre-commit configuration * Fix new Pylint warnings * Upgrade the regex for Mixin Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 47e860f commit 813aab5

File tree

10 files changed

+24
-40
lines changed

10 files changed

+24
-40
lines changed

astroid/brain/brain_numpy_utils.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ def _is_a_numpy_module(node: Name) -> bool:
5959
potential_import_target = [
6060
x for x in node.lookup(module_nickname)[1] if isinstance(x, Import)
6161
]
62-
for target in potential_import_target:
63-
if ("numpy", module_nickname) in target.names or (
64-
"numpy",
65-
None,
66-
) in target.names:
67-
return True
68-
return False
62+
return any(
63+
("numpy", module_nickname) in target.names or ("numpy", None) in target.names
64+
for target in potential_import_target
65+
)
6966

7067

7168
def looks_like_numpy_member(member_name: str, node: NodeNG) -> bool:

astroid/modutils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,8 @@ def is_standard_module(modname, std_path=None):
555555
return False
556556
if std_path is None:
557557
std_path = STD_LIB_DIRS
558-
for path in std_path:
559-
if filename.startswith(_cache_normalize_path(path)):
560-
return True
561-
return False
558+
559+
return any(filename.startswith(_cache_normalize_path(path)) for path in std_path)
562560

563561

564562
def is_relative(modname, from_file):

astroid/nodes/node_classes.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,10 +2759,7 @@ def catch(self, exceptions: Optional[typing.List[str]]) -> bool:
27592759
"""
27602760
if self.type is None or exceptions is None:
27612761
return True
2762-
for node in self.type._get_name_nodes():
2763-
if node.name in exceptions:
2764-
return True
2765-
return False
2762+
return any(node.name in exceptions for node in self.type._get_name_nodes())
27662763

27672764

27682765
class ExtSlice(NodeNG):
@@ -3724,10 +3721,9 @@ def raises_not_implemented(self):
37243721
"""
37253722
if not self.exc:
37263723
return False
3727-
for name in self.exc._get_name_nodes():
3728-
if name.name == "NotImplementedError":
3729-
return True
3730-
return False
3724+
return any(
3725+
name.name == "NotImplementedError" for name in self.exc._get_name_nodes()
3726+
)
37313727

37323728
def get_children(self):
37333729
if self.exc is not None:
@@ -5571,10 +5567,7 @@ def const_factory(value):
55715567

55725568
def is_from_decorator(node):
55735569
"""Return True if the given node is the child of a decorator"""
5574-
for parent in node.node_ancestors():
5575-
if isinstance(parent, Decorators):
5576-
return True
5577-
return False
5570+
return any(isinstance(parent, Decorators) for parent in node.node_ancestors())
55785571

55795572

55805573
def _get_if_statement_ancestor(node: NodeNG) -> Optional[If]:

astroid/nodes/node_ng.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ def parent_of(self, node):
276276
False otherwise.
277277
:rtype: bool
278278
"""
279-
for parent in node.node_ancestors():
280-
if self is parent:
281-
return True
282-
return False
279+
return any(self is parent for parent in node.node_ancestors())
283280

284281
@overload
285282
def statement(
@@ -448,7 +445,7 @@ def _fixed_source_line(self) -> Optional[int]:
448445
We need this method since not all nodes have :attr:`lineno` set.
449446
"""
450447
line = self.lineno
451-
_node: Optional[NodeNG] = self
448+
_node: Optional[NodeNG] = self # pylint: disable = used-before-assignment
452449
try:
453450
while line is None:
454451
_node = next(_node.get_children())

astroid/nodes/scoped_nodes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,10 +2398,8 @@ def is_subtype_of(self, type_name, context=None):
23982398
"""
23992399
if self.qname() == type_name:
24002400
return True
2401-
for anc in self.ancestors(context=context):
2402-
if anc.qname() == type_name:
2403-
return True
2404-
return False
2401+
2402+
return any(anc.qname() == type_name for anc in self.ancestors(context=context))
24052403

24062404
def _infer_type_call(self, caller, context):
24072405
try:

pylintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ load-plugins=
2828
pylint.extensions.code_style,
2929
pylint.extensions.set_membership,
3030
pylint.extensions.redefined_variable_type,
31+
pylint.extensions.for_any_all,
3132

3233
# Use multiple processes to speed up Pylint.
3334
jobs=1
@@ -300,6 +301,9 @@ ignored-modules=typed_ast.ast3
300301
# (useful for classes with attributes dynamically set).
301302
ignored-classes=SQLObject
302303

304+
# Regex pattern to define which classes are considered mixins.
305+
mixin-class-rgx=.*Mix[i|I]n
306+
303307
# List of members which are set dynamically and missed by pylint inference
304308
# system, and so shouldn't trigger E0201 when accessed. Python regular
305309
# expressions are accepted.

requirements_test_pre_commit.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
black==21.7b0
2-
pylint==2.11.1
2+
pylint==2.12.1
33
isort==5.9.2
44
flake8==4.0.1
55
flake8-typing-imports==1.11.0

tests/unittest_brain.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,10 +1238,7 @@ def streams_are_fine():
12381238
12391239
PY3 only
12401240
"""
1241-
for stream in (sys.stdout, sys.stderr, sys.stdin):
1242-
if not isinstance(stream, io.IOBase):
1243-
return False
1244-
return True
1241+
return all(isinstance(s, io.IOBase) for s in (sys.stdout, sys.stderr, sys.stdin))
12451242

12461243

12471244
class IOBrainTest(unittest.TestCase):

tests/unittest_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6469,7 +6469,7 @@ def test(self):
64696469
assert isinstance(inferred.args, nodes.Arguments)
64706470
# This line used to crash because property generated functions
64716471
# did not have args properly set
6472-
assert list(inferred.nodes_of_class(nodes.Const)) == []
6472+
assert not list(inferred.nodes_of_class(nodes.Const))
64736473

64746474

64756475
def test_infer_list_of_uninferables_does_not_crash() -> None:

tests/unittest_nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,12 +1717,12 @@ def test_match_simple():
17171717

17181718
assert isinstance(case2.pattern, nodes.MatchSingleton)
17191719
assert case2.pattern.value is None
1720-
assert list(case2.pattern.get_children()) == []
1720+
assert not list(case2.pattern.get_children())
17211721

17221722
assert isinstance(case3.pattern, nodes.MatchAs)
17231723
assert case3.pattern.name is None
17241724
assert case3.pattern.pattern is None
1725-
assert list(case3.pattern.get_children()) == []
1725+
assert not list(case3.pattern.get_children())
17261726

17271727
@staticmethod
17281728
def test_match_sequence():

0 commit comments

Comments
 (0)