From 249fc89565f563f0f4541d5caaa030192b048f86 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 7 Sep 2025 00:39:58 +0200 Subject: [PATCH 1/3] Count match cases for too-many-branches --- doc/whatsnew/fragments/10542.false_negative | 3 +++ pylint/checkers/design_analysis.py | 4 ++++ tests/functional/t/too/too_many_branches.py | 9 +++++---- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 doc/whatsnew/fragments/10542.false_negative diff --git a/doc/whatsnew/fragments/10542.false_negative b/doc/whatsnew/fragments/10542.false_negative new file mode 100644 index 0000000000..bd523e53bc --- /dev/null +++ b/doc/whatsnew/fragments/10542.false_negative @@ -0,0 +1,3 @@ +Count match cases for ``too-many-branches`` check. + +Refs #10542 diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py index 5c1adbc888..81022a4b63 100644 --- a/pylint/checkers/design_analysis.py +++ b/pylint/checkers/design_analysis.py @@ -693,6 +693,10 @@ def visit_while(self, node: nodes.While) -> None: visit_for = visit_while + def visit_match(self, node: nodes.Match) -> None: + """Increments the branches counter.""" + self._inc_branch(node, len(node.cases)) + def _inc_branch(self, node: nodes.NodeNG, branchesnum: int = 1) -> None: """Increments the branches counter.""" self._branches[node.scope()] += branchesnum diff --git a/tests/functional/t/too/too_many_branches.py b/tests/functional/t/too/too_many_branches.py index d7a3913e41..ad2d2fb119 100644 --- a/tests/functional/t/too/too_many_branches.py +++ b/tests/functional/t/too/too_many_branches.py @@ -10,10 +10,6 @@ def wrong(): # [too-many-branches] pass elif 1: pass - elif 1: - pass - elif 1: - pass try: pass except TypeError: @@ -24,6 +20,11 @@ def wrong(): # [too-many-branches] pass while True: pass + match 1: + case 1: + pass + case 2: + pass if 1: pass elif 2: From 9e10c4ef257fb72f4ae19ebbefdb04a82dfb7684 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 7 Sep 2025 11:24:28 +0200 Subject: [PATCH 2/3] Increment statement counter --- pylint/checkers/design_analysis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py index 81022a4b63..efadbd8c45 100644 --- a/pylint/checkers/design_analysis.py +++ b/pylint/checkers/design_analysis.py @@ -695,6 +695,7 @@ def visit_while(self, node: nodes.While) -> None: def visit_match(self, node: nodes.Match) -> None: """Increments the branches counter.""" + self._inc_all_stmts(1) self._inc_branch(node, len(node.cases)) def _inc_branch(self, node: nodes.NodeNG, branchesnum: int = 1) -> None: From dc80e65c66d2dcdf411a088bcfb5801938be68bc Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 7 Sep 2025 00:42:53 +0200 Subject: [PATCH 3/3] Add pylint disable comment --- pylint/checkers/format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py index 51cf4e07ea..3ce61af133 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -272,7 +272,7 @@ def new_line(self, tokens: TokenWrapper, line_end: int, line_start: int) -> None def process_module(self, node: nodes.Module) -> None: pass - # pylint: disable-next = too-many-return-statements + # pylint: disable-next = too-many-return-statements, too-many-branches def _check_keyword_parentheses( self, tokens: list[tokenize.TokenInfo], start: int ) -> None: