Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Clearly indicate what is counted as a branch #11423

Merged
merged 4 commits into from
May 16, 2024
Merged
Changes from all commits
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
76 changes: 75 additions & 1 deletion crates/ruff_linter/src/rules/pylint/rules/too_many_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::identifier::Identifier;

/// ## What it does
/// Checks for functions or methods with too many branches.
/// Checks for functions or methods with too many branches, including (nested)
/// `if`, `elif`, and `else` branches, `for` loops, `try`-`except` clauses, and
/// `match` and `case` statements.
///
/// By default, this rule allows up to 12 branches. This can be configured
/// using the [`lint.pylint.max-branches`] option.
Expand All @@ -15,6 +17,7 @@ use ruff_python_ast::identifier::Identifier;
/// and maintain than functions or methods with fewer branches.
///
/// ## Example
/// Given:
/// ```python
/// def capital(country):
jaap3 marked this conversation as resolved.
Show resolved Hide resolved
/// if country == "Australia":
Expand Down Expand Up @@ -66,6 +69,77 @@ use ruff_python_ast::identifier::Identifier;
/// return city
/// ```
///
/// Given:
/// ```python
/// def grades_to_average_number(grades):
/// numbers = []
/// for grade in grades: # 1st branch
/// if len(grade) not in {1, 2}:
/// raise ValueError(f"Invalid grade: {grade}")
///
/// if len(grade) == 2 and grade[1] not in {"+", "-"}:
/// raise ValueError(f"Invalid grade: {grade}")
///
/// letter = grade[0]
///
/// if letter in {"F", "E"}:
/// number = 0.0
/// elif letter == "D":
/// number = 1.0
/// elif letter == "C":
/// number = 2.0
/// elif letter == "B":
/// number = 3.0
/// elif letter == "A":
/// number = 4.0
/// else:
/// raise ValueError(f"Invalid grade: {grade}")
///
/// modifier = 0.0
/// if letter != "F" and grade[-1] == "+":
/// modifier = 0.3
/// elif letter != "F" and grade[-1] == "-":
/// modifier = -0.3
///
/// numbers.append(max(0.0, min(number + modifier, 4.0)))
///
/// try:
/// return sum(numbers) / len(numbers)
/// except ZeroDivisionError: # 13th branch
/// return 0
/// ```
///
/// Use instead:
/// ```python
/// def grades_to_average_number(grades):
/// grade_values = {"F": 0.0, "E": 0.0, "D": 1.0, "C": 2.0, "B": 3.0, "A": 4.0}
/// modifier_values = {"+": 0.3, "-": -0.3}
///
/// numbers = []
/// for grade in grades:
/// if len(grade) not in {1, 2}:
/// raise ValueError(f"Invalid grade: {grade}")
///
/// letter = grade[0]
/// if letter not in grade_values:
/// raise ValueError(f"Invalid grade: {grade}")
/// number = grade_values[letter]
///
/// if len(grade) == 2 and grade[1] not in modifier_values:
/// raise ValueError(f"Invalid grade: {grade}")
/// modifier = modifier_values.get(grade[-1], 0.0)
///
/// if letter == "F":
/// numbers.append(0.0)
/// else:
/// numbers.append(max(0.0, min(number + modifier, 4.0)))
///
/// try:
/// return sum(numbers) / len(numbers)
/// except ZeroDivisionError:
/// return 0
/// ```
///
/// ## Options
/// - `lint.pylint.max-branches`
#[violation]
Expand Down
Loading