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

Consider irrefutable pattern similar to if .. else for C901 #11565

Merged
merged 6 commits into from
May 27, 2024
Merged
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, ExceptHandler, Stmt};
use ruff_python_ast::{self as ast, ExceptHandler, Pattern, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -100,6 +100,28 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
complexity += 1;
complexity += get_complexity_number(&case.body);
}
if let Some(last_case) = cases.last() {
if let Pattern::MatchAs(match_as_pattern) = &last_case.pattern {
if match_as_pattern.pattern.is_none() {
// The complexity of an irrefutable pattern is similar to an `else` block of an `if` statement.
Copy link
Member

Choose a reason for hiding this comment

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

Actually, there are more cases to consider here:

  1. Make sure that the guard field is None (last_case.guard.is_none())
  2. Consider sequence pattern where if any pattern in the sequence is _ or named capture, the entire sequence is considered irrefutable

Copy link
Member

Choose a reason for hiding this comment

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

(Sorry for sending this review after the approval)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Done.

// This is either a wildcard pattern or a named catch-all pattern.
//
// For example:
// ```python
// match subject:
// case 1: ...
// case _: ...
//
// match subject:
// case 1: ...
// case foo: ...
// ```
//
// Irrefutable pattern: https://peps.python.org/pep-0634/#irrefutable-case-blocks
complexity -= 1;
}
}
}
}
Stmt::Try(ast::StmtTry {
body,
Expand Down Expand Up @@ -431,17 +453,49 @@ def with_lock():
}

#[test]
fn match_case() -> Result<()> {
fn simple_match_case() -> Result<()> {
let source = r"
def f():
match a:
case 30:
match subject:
case 2:
print('foo')
case _:
print('bar')
";
let stmts = parse_suite(source)?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}

#[test]
fn multiple_match_case() -> Result<()> {
let source = r"
def f():
match subject:
case 2:
print('foo')
case 2:
print('bar')
case _:
print('baz')
";
let stmts = parse_suite(source)?;
assert_eq!(get_complexity_number(&stmts), 3);
Ok(())
}

#[test]
fn named_catch_all_match_case() -> Result<()> {
let source = r"
def f():
match subject:
case 2:
print('hello')
case x:
print(x)
";
let stmts = parse_suite(source)?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
}
Loading