Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Consider with statements for too many branches lint #11321

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Test for too many branches.
Taken from the pylint source 2023-02-03
"""
from contextlib import suppress

# pylint: disable=using-constant-test
def wrong(): # [too-many-branches]
""" Has too many branches. """
Expand Down Expand Up @@ -70,3 +72,33 @@ def nested_1():
pass
elif 7:
pass

def with_statement_wrong():
"""statements inside the with statement should get counted"""
with suppress(Exception):
if 1:
pass
elif 1:
pass
elif 1:
pass
elif 1:
pass
elif 1:
pass
elif 1:
pass
try:
pass
finally:
pass
if 2:
pass
while True:
pass
if 1:
pass
elif 2:
pass
elif 3:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you include additional test cases in this file for with statements? They will be at the bottom of this file and they specifically tests the num_branches function.

Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn num_branches(stmts: &[Stmt]) -> usize {
.map(|case| num_branches(&case.body))
.sum::<usize>()
}
Stmt::With(ast::StmtWith { body, .. }) => num_branches(body),
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
Stmt::For(ast::StmtFor { body, orelse, .. })
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
1 + num_branches(body)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
too_many_branches.py:6:5: PLR0912 Too many branches (13 > 12)
|
4 | """
5 | # pylint: disable=using-constant-test
6 | def wrong(): # [too-many-branches]
| ^^^^^ PLR0912
7 | """ Has too many branches. """
8 | if 1:
|

too_many_branches.py:8:5: PLR0912 Too many branches (13 > 12)
|
7 | # pylint: disable=using-constant-test
8 | def wrong(): # [too-many-branches]
| ^^^^^ PLR0912
9 | """ Has too many branches. """
10 | if 1:
|

too_many_branches.py:76:5: PLR0912 Too many branches (13 > 12)
|
74 | pass
75 |
76 | def with_statement_wrong():
| ^^^^^^^^^^^^^^^^^^^^ PLR0912
77 | """statements inside the with statement should get counted"""
78 | with suppress(Exception):
|
Loading