Skip to content

Commit a96dc99

Browse files
authored
Use match statement in checkers (6) (#10552)
1 parent 47861c3 commit a96dc99

13 files changed

+375
-411
lines changed

pylint/checkers/classes/class_checker.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ def _is_trivial_super_delegation(function: nodes.FunctionDef) -> bool:
167167
return False
168168

169169
call = statement.value
170-
if (
171-
not isinstance(call, nodes.Call)
172-
# Not a super() attribute access.
173-
or not isinstance(call.func, nodes.Attribute)
174-
):
175-
return False
170+
match call := statement.value:
171+
case nodes.Call(func=nodes.Attribute(expr=expr)):
172+
pass
173+
case _:
174+
# Not a super() attribute access.
175+
return False
176176

177177
# Anything other than a super call is non-trivial.
178-
super_call = safe_infer(call.func.expr)
178+
super_call = safe_infer(expr)
179179
if not isinstance(super_call, astroid.objects.Super):
180180
return False
181181

@@ -1844,33 +1844,27 @@ def _check_classmethod_declaration(self, node: nodes.Assign) -> None:
18441844
is defined.
18451845
`node` is an assign node.
18461846
"""
1847-
if not isinstance(node.value, nodes.Call):
1848-
return
1849-
18501847
# check the function called is "classmethod" or "staticmethod"
1851-
func = node.value.func
1852-
if not isinstance(func, nodes.Name) or func.name not in (
1853-
"classmethod",
1854-
"staticmethod",
1855-
):
1856-
return
1848+
# Check if the arg passed to classmethod is a class member
1849+
match node.value:
1850+
case nodes.Call(
1851+
func=nodes.Name(name="classmethod" | "staticmethod" as name),
1852+
args=[nodes.Name(name=method_name), *_],
1853+
):
1854+
pass
1855+
case _:
1856+
return
18571857

18581858
msg = (
18591859
"no-classmethod-decorator"
1860-
if func.name == "classmethod"
1860+
if name == "classmethod"
18611861
else "no-staticmethod-decorator"
18621862
)
18631863
# assignment must be at a class scope
18641864
parent_class = node.scope()
18651865
if not isinstance(parent_class, nodes.ClassDef):
18661866
return
18671867

1868-
# Check if the arg passed to classmethod is a class member
1869-
classmeth_arg = node.value.args[0]
1870-
if not isinstance(classmeth_arg, nodes.Name):
1871-
return
1872-
1873-
method_name = classmeth_arg.name
18741868
if any(method_name == member.name for member in parent_class.mymethods()):
18751869
self.add_message(msg, node=node.targets[0])
18761870

@@ -2383,16 +2377,16 @@ def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool:
23832377
first_attr = self._first_attrs[-1]
23842378
else:
23852379
# It's possible the function was already unregistered.
2386-
closest_func = utils.get_node_first_ancestor_of_type(
2380+
match closest_func := utils.get_node_first_ancestor_of_type(
23872381
node, nodes.FunctionDef
2388-
)
2389-
if closest_func is None:
2390-
return False
2391-
if not closest_func.is_bound():
2392-
return False
2393-
if not closest_func.args.args:
2394-
return False
2395-
first_attr = closest_func.args.args[0].name
2382+
):
2383+
case nodes.FunctionDef(
2384+
args=nodes.Arguments(args=[nodes.AssignName(name=first_attr), *_])
2385+
) if closest_func.is_bound():
2386+
pass
2387+
case _:
2388+
return False
2389+
# pylint: disable=possibly-used-before-assignment
23962390
return isinstance(node, nodes.Name) and node.name == first_attr
23972391

23982392

pylint/checkers/deprecated.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,12 @@ def check_deprecated_method(self, node: nodes.Call, inferred: nodes.NodeNG) -> N
251251
if not isinstance(inferred, ACCEPTABLE_NODES):
252252
return
253253

254-
if isinstance(node.func, nodes.Attribute):
255-
func_name = node.func.attrname
256-
elif isinstance(node.func, nodes.Name):
257-
func_name = node.func.name
258-
else:
259-
# Not interested in other nodes.
260-
return
254+
match node.func:
255+
case nodes.Attribute(attrname=func_name) | nodes.Name(name=func_name):
256+
pass
257+
case _:
258+
# Not interested in other nodes.
259+
return
261260

262261
qnames = {inferred.qname(), func_name}
263262
if any(name in self.deprecated_methods() for name in qnames):

pylint/checkers/design_analysis.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,11 @@ def _is_exempt_from_public_methods(node: astroid.ClassDef) -> bool:
203203
for decorator in node.decorators.nodes:
204204
if isinstance(decorator, astroid.Call):
205205
decorator = decorator.func
206-
if not isinstance(decorator, (astroid.Name, astroid.Attribute)):
207-
continue
208-
if isinstance(decorator, astroid.Name):
209-
name = decorator.name
210-
else:
211-
name = decorator.attrname
206+
match decorator:
207+
case astroid.Name(name=name) | astroid.Attribute(attrname=name):
208+
pass
209+
case _:
210+
continue
212211
if name in DATACLASSES_DECORATORS and (
213212
root_locals.intersection(DATACLASSES_DECORATORS)
214213
or DATACLASS_IMPORT in root_locals

pylint/checkers/newstyle.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,17 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
5959
if not isinstance(expr, nodes.Attribute):
6060
continue
6161

62-
call = expr.expr
63-
# skip the test if using super
64-
if not (
65-
isinstance(call, nodes.Call)
66-
and isinstance(call.func, nodes.Name)
67-
and call.func.name == "super"
68-
):
69-
continue
70-
71-
# super first arg should not be the class
72-
if not call.args:
73-
continue
62+
match call := expr.expr:
63+
case nodes.Call(func=nodes.Name(name="super"), args=[arg0, *_]):
64+
pass
65+
case _:
66+
# skip the test if using super
67+
# super first arg should not be the class
68+
continue
7469

7570
# calling super(type(self), self) can lead to recursion loop
7671
# in derived classes
77-
match call.args[0]:
72+
match arg0:
7873
case nodes.Call(func=nodes.Name(name="type")):
7974
self.add_message("bad-super-call", node=call, args=("type",))
8075
continue

pylint/checkers/refactoring/recommendation_checker.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,14 @@ def _check_consider_using_enumerate(self, node: nodes.For) -> None:
210210
return
211211

212212
# Is it a proper len call?
213-
if not isinstance(node.iter.args[-1], nodes.Call):
214-
return
215-
second_func = node.iter.args[-1].func
216-
if not self._is_builtin(second_func, "len"):
217-
return
218-
len_args = node.iter.args[-1].args
219-
if not len_args or len(len_args) != 1:
220-
return
221-
iterating_object = len_args[0]
213+
match node.iter.args:
214+
case [
215+
*_,
216+
nodes.Call(func=second_func, args=[iterating_object]),
217+
] if self._is_builtin(second_func, "len"):
218+
pass
219+
case _:
220+
return
222221
if isinstance(iterating_object, nodes.Name):
223222
expected_subscript_val_type = nodes.Name
224223
elif isinstance(iterating_object, nodes.Attribute):

0 commit comments

Comments
 (0)