Skip to content

Commit 1e8198f

Browse files
committed
Use match statement in checkers (5)
1 parent dad4124 commit 1e8198f

File tree

10 files changed

+141
-141
lines changed

10 files changed

+141
-141
lines changed

pylint/checkers/base/comparison_checker.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,19 @@ def _check_nan_comparison(
145145
) -> None:
146146
def _is_float_nan(node: nodes.NodeNG) -> bool:
147147
try:
148-
if isinstance(node, nodes.Call) and len(node.args) == 1:
149-
if (
150-
node.args[0].value.lower() == "nan"
151-
and node.inferred()[0].pytype() == "builtins.float"
148+
match node:
149+
case nodes.Call(args=[nodes.Const(value=str(value))]) if (
150+
value.lower() == "nan"
152151
):
153-
return True
152+
return node.inferred()[0].pytype() == "builtins.float" # type: ignore[no-any-return]
154153
return False
155154
except AttributeError:
156155
return False
157156

158157
def _is_numpy_nan(node: nodes.NodeNG) -> bool:
159-
if isinstance(node, nodes.Attribute) and node.attrname == "NaN":
160-
if isinstance(node.expr, nodes.Name):
161-
return node.expr.name in {"numpy", "nmp", "np"}
158+
match node:
159+
case nodes.Attribute(attrname="NaN", expr=nodes.Name(name=name)):
160+
return name in {"numpy", "nmp", "np"}
162161
return False
163162

164163
def _is_nan(node: nodes.NodeNG) -> bool:

pylint/checkers/classes/class_checker.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,20 +1981,20 @@ def _is_called_inside_special_method(node: nodes.NodeNG) -> bool:
19811981
return frame_name and frame_name in PYMETHODS
19821982

19831983
def _is_type_self_call(self, expr: nodes.NodeNG) -> bool:
1984-
return (
1985-
isinstance(expr, nodes.Call)
1986-
and isinstance(expr.func, nodes.Name)
1987-
and expr.func.name == "type"
1988-
and len(expr.args) == 1
1989-
and self._is_mandatory_method_param(expr.args[0])
1990-
)
1984+
match expr:
1985+
case nodes.Call(func=nodes.Name(name="type"), args=[arg]):
1986+
return self._is_mandatory_method_param(arg)
1987+
return False
19911988

19921989
@staticmethod
19931990
def _is_classmethod(func: LocalsDictNodeNG) -> bool:
19941991
"""Check if the given *func* node is a class method."""
1995-
return isinstance(func, nodes.FunctionDef) and (
1996-
func.type == "classmethod" or func.name == "__class_getitem__"
1997-
)
1992+
match func:
1993+
case nodes.FunctionDef(type="classmethod") | nodes.FunctionDef(
1994+
name="__class_getitem__"
1995+
):
1996+
return True
1997+
return False
19981998

19991999
@staticmethod
20002000
def _is_inferred_instance(expr: nodes.NodeNG, klass: nodes.ClassDef) -> bool:

pylint/checkers/classes/special_methods_checker.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,42 +257,60 @@ def _is_int(node: InferenceResult) -> bool:
257257
if SpecialMethodsChecker._is_wrapped_type(node, "int"):
258258
return True
259259

260-
return isinstance(node, nodes.Const) and isinstance(node.value, int)
260+
match node:
261+
case nodes.Const(value=int()):
262+
return True
263+
return False
261264

262265
@staticmethod
263266
def _is_str(node: InferenceResult) -> bool:
264267
if SpecialMethodsChecker._is_wrapped_type(node, "str"):
265268
return True
266269

267-
return isinstance(node, nodes.Const) and isinstance(node.value, str)
270+
match node:
271+
case nodes.Const(value=str()):
272+
return True
273+
return False
268274

269275
@staticmethod
270276
def _is_bool(node: InferenceResult) -> bool:
271277
if SpecialMethodsChecker._is_wrapped_type(node, "bool"):
272278
return True
273279

274-
return isinstance(node, nodes.Const) and isinstance(node.value, bool)
280+
match node:
281+
case nodes.Const(value=bool()):
282+
return True
283+
return False
275284

276285
@staticmethod
277286
def _is_bytes(node: InferenceResult) -> bool:
278287
if SpecialMethodsChecker._is_wrapped_type(node, "bytes"):
279288
return True
280289

281-
return isinstance(node, nodes.Const) and isinstance(node.value, bytes)
290+
match node:
291+
case nodes.Const(value=bytes()):
292+
return True
293+
return False
282294

283295
@staticmethod
284296
def _is_tuple(node: InferenceResult) -> bool:
285297
if SpecialMethodsChecker._is_wrapped_type(node, "tuple"):
286298
return True
287299

288-
return isinstance(node, nodes.Const) and isinstance(node.value, tuple)
300+
match node:
301+
case nodes.Const(value=tuple()):
302+
return True
303+
return False
289304

290305
@staticmethod
291306
def _is_dict(node: InferenceResult) -> bool:
292307
if SpecialMethodsChecker._is_wrapped_type(node, "dict"):
293308
return True
294309

295-
return isinstance(node, nodes.Const) and isinstance(node.value, dict)
310+
match node:
311+
case nodes.Const(value=dict()):
312+
return True
313+
return False
296314

297315
@staticmethod
298316
def _is_iterator(node: InferenceResult) -> bool:

pylint/checkers/logging.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ def visit_call(self, node: nodes.Call) -> None:
192192
"""Checks calls to logging methods."""
193193

194194
def is_logging_name() -> bool:
195-
return (
196-
isinstance(node.func, nodes.Attribute)
197-
and isinstance(node.func.expr, nodes.Name)
198-
and node.func.expr.name in self._logging_names
199-
)
195+
match node.func:
196+
case nodes.Attribute(expr=nodes.Name(name=name)):
197+
return name in self._logging_names
198+
return False
200199

201200
def is_logger_class() -> tuple[bool, str | None]:
202201
for inferred in infer_all(node.func):

pylint/checkers/modified_iterating_checker.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ def _modified_iterating_check(
9999

100100
@staticmethod
101101
def _is_node_expr_that_calls_attribute_name(node: nodes.NodeNG) -> bool:
102-
return (
103-
isinstance(node, nodes.Expr)
104-
and isinstance(node.value, nodes.Call)
105-
and isinstance(node.value.func, nodes.Attribute)
106-
and isinstance(node.value.func.expr, nodes.Name)
107-
)
102+
match node:
103+
case nodes.Expr(value=nodes.Call(func=nodes.Attribute(expr=nodes.Name()))):
104+
return True
105+
return False
108106

109107
@staticmethod
110108
def _common_cond_list_set(
@@ -123,10 +121,10 @@ def _common_cond_list_set(
123121

124122
@staticmethod
125123
def _is_node_assigns_subscript_name(node: nodes.NodeNG) -> bool:
126-
return isinstance(node, nodes.Assign) and (
127-
isinstance(node.targets[0], nodes.Subscript)
128-
and (isinstance(node.targets[0].value, nodes.Name))
129-
)
124+
match node:
125+
case nodes.Assign(targets=[nodes.Subscript(value=nodes.Name()), *_]):
126+
return True
127+
return False
130128

131129
def _modified_iterating_list_cond(
132130
self, node: nodes.NodeNG, iter_obj: nodes.Name | nodes.Attribute

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,10 @@ def _dummy_rgx(self) -> Pattern[str]:
558558

559559
@staticmethod
560560
def _is_bool_const(node: nodes.Return | nodes.Assign) -> bool:
561-
return isinstance(node.value, nodes.Const) and isinstance(
562-
node.value.value, bool
563-
)
561+
match node.value:
562+
case nodes.Const(value=bool()):
563+
return True
564+
return False
564565

565566
def _is_actual_elif(self, node: nodes.If | nodes.Try) -> bool:
566567
"""Check if the given node is an actual elif.
@@ -826,14 +827,13 @@ def _check_superfluous_else_continue(self, node: nodes.If) -> None:
826827

827828
@staticmethod
828829
def _type_and_name_are_equal(node_a: Any, node_b: Any) -> bool:
829-
if isinstance(node_a, nodes.Name) and isinstance(node_b, nodes.Name):
830-
return node_a.name == node_b.name # type: ignore[no-any-return]
831-
if isinstance(node_a, nodes.AssignName) and isinstance(
832-
node_b, nodes.AssignName
833-
):
834-
return node_a.name == node_b.name # type: ignore[no-any-return]
835-
if isinstance(node_a, nodes.Const) and isinstance(node_b, nodes.Const):
836-
return node_a.value == node_b.value # type: ignore[no-any-return]
830+
match (node_a, node_b):
831+
case (
832+
[nodes.Name(name=a), nodes.Name(name=b)]
833+
| [nodes.AssignName(name=a), nodes.AssignName(name=b)]
834+
| [nodes.Const(value=a), nodes.Const(value=b)]
835+
):
836+
return a == b # type: ignore[no-any-return]
837837
return False
838838

839839
def _is_dict_get_block(self, node: nodes.If) -> bool:
@@ -1568,12 +1568,10 @@ def visit_boolop(self, node: nodes.BoolOp) -> None:
15681568

15691569
@staticmethod
15701570
def _is_simple_assignment(node: nodes.NodeNG | None) -> bool:
1571-
return (
1572-
isinstance(node, nodes.Assign)
1573-
and len(node.targets) == 1
1574-
and isinstance(node.targets[0], nodes.AssignName)
1575-
and isinstance(node.value, nodes.Name)
1576-
)
1571+
match node:
1572+
case nodes.Assign(targets=[nodes.AssignName()], value=nodes.Name()):
1573+
return True
1574+
return False
15771575

15781576
def _check_swap_variables(self, node: nodes.Return | nodes.Assign) -> None:
15791577
if not node.next_sibling() or not node.next_sibling().next_sibling():
@@ -1901,16 +1899,12 @@ def _is_and_or_ternary(node: nodes.NodeNG | None) -> bool:
19011899
19021900
All of: condition, true_value and false_value should not be a complex boolean expression
19031901
"""
1904-
return (
1905-
isinstance(node, nodes.BoolOp)
1906-
and node.op == "or"
1907-
and len(node.values) == 2
1908-
and isinstance(node.values[0], nodes.BoolOp)
1909-
and not isinstance(node.values[1], nodes.BoolOp)
1910-
and node.values[0].op == "and"
1911-
and not isinstance(node.values[0].values[1], nodes.BoolOp)
1912-
and len(node.values[0].values) == 2
1913-
)
1902+
match node:
1903+
case nodes.BoolOp(
1904+
op="or", values=[nodes.BoolOp(op="and", values=[_, v1]), v2]
1905+
) if not (isinstance(v2, nodes.BoolOp) or isinstance(v1, nodes.BoolOp)):
1906+
return True
1907+
return False
19141908

19151909
@staticmethod
19161910
def _and_or_ternary_arguments(
@@ -2093,10 +2087,12 @@ def _is_function_def_never_returning(
20932087
except AttributeError:
20942088
return False # the BoundMethod proxy may be a lambda without a returns
20952089

2096-
return (
2097-
isinstance(returns, nodes.Attribute)
2098-
and returns.attrname in {"NoReturn", "Never"}
2099-
) or (isinstance(returns, nodes.Name) and returns.name in {"NoReturn", "Never"})
2090+
match returns:
2091+
case nodes.Attribute(attrname="NoReturn" | "Never") | nodes.Name(
2092+
name="NoReturn" | "Never"
2093+
):
2094+
return True
2095+
return False
21002096

21012097
def _check_return_at_the_end(self, node: nodes.FunctionDef) -> None:
21022098
"""Check for presence of a *single* return statement at the end of a

pylint/checkers/typecheck.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,14 +1291,14 @@ def _is_ignored_function(
12911291

12921292
@staticmethod
12931293
def _is_builtin_no_return(node: nodes.Assign) -> bool:
1294-
return (
1295-
isinstance(node.value, nodes.Call)
1296-
and isinstance(node.value.func, nodes.Attribute)
1297-
and bool(inferred := utils.safe_infer(node.value.func.expr))
1298-
and isinstance(inferred, bases.Instance)
1299-
and node.value.func.attrname
1300-
in BUILTINS_IMPLICIT_RETURN_NONE.get(inferred.pytype(), ())
1301-
)
1294+
match node.value:
1295+
case nodes.Call(func=nodes.Attribute(expr=expr, attrname=attr)):
1296+
return (
1297+
bool(inferred := utils.safe_infer(expr))
1298+
and isinstance(inferred, bases.Instance)
1299+
and attr in BUILTINS_IMPLICIT_RETURN_NONE.get(inferred.pytype(), ())
1300+
)
1301+
return False
13021302

13031303
def _check_dundername_is_string(self, node: nodes.Assign) -> None:
13041304
"""Check a string is assigned to self.__name__."""

0 commit comments

Comments
 (0)