From cd1e07f37c75e99dbb07807b037bbc70fe652420 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 21 Nov 2022 13:30:36 -0500 Subject: [PATCH] Avoid incrementing McCabe complexity for class methods (#857) --- resources/test/fixtures/C901.py | 30 +++++++ src/mccabe/checks.rs | 1 - ...ruff__mccabe__tests__max_complexity_0.snap | 79 ++++++++++++++++++- ...ruff__mccabe__tests__max_complexity_3.snap | 11 +++ 4 files changed, 119 insertions(+), 2 deletions(-) diff --git a/resources/test/fixtures/C901.py b/resources/test/fixtures/C901.py index 4028f300df..54fde20a3e 100644 --- a/resources/test/fixtures/C901.py +++ b/resources/test/fixtures/C901.py @@ -106,3 +106,33 @@ async def foobar(a, b, c): # Complexity = 1 def annotated_assign(): x: Any = None + + +# Complexity = 9 +class Class: + def handle(self, *args, **options): + if args: + return + + class ServiceProvider: + def a(self): + pass + + def b(self, data): + if not args: + pass + + class Logger: + def c(*args, **kwargs): + pass + + def error(self, message): + pass + + def info(self, message): + pass + + def exception(self): + pass + + return ServiceProvider(Logger()) diff --git a/src/mccabe/checks.rs b/src/mccabe/checks.rs index 1c78e5f866..2d20fa98bb 100644 --- a/src/mccabe/checks.rs +++ b/src/mccabe/checks.rs @@ -46,7 +46,6 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize { complexity += get_complexity_number(body); } StmtKind::ClassDef { body, .. } => { - complexity += 1; complexity += get_complexity_number(body); } _ => {} diff --git a/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_0.snap b/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_0.snap index 2f5343cc90..295880f510 100644 --- a/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_0.snap +++ b/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_0.snap @@ -164,7 +164,84 @@ expression: checks row: 107 column: 0 end_location: - row: 109 + row: 112 column: 0 fix: ~ +- kind: + FunctionIsTooComplex: + - handle + - 9 + location: + row: 113 + column: 4 + end_location: + row: 139 + column: 0 + fix: ~ +- kind: + FunctionIsTooComplex: + - a + - 1 + location: + row: 118 + column: 12 + end_location: + row: 121 + column: 12 + fix: ~ +- kind: + FunctionIsTooComplex: + - b + - 2 + location: + row: 121 + column: 12 + end_location: + row: 125 + column: 8 + fix: ~ +- kind: + FunctionIsTooComplex: + - c + - 1 + location: + row: 126 + column: 12 + end_location: + row: 129 + column: 12 + fix: ~ +- kind: + FunctionIsTooComplex: + - error + - 1 + location: + row: 129 + column: 12 + end_location: + row: 132 + column: 12 + fix: ~ +- kind: + FunctionIsTooComplex: + - info + - 1 + location: + row: 132 + column: 12 + end_location: + row: 135 + column: 12 + fix: ~ +- kind: + FunctionIsTooComplex: + - exception + - 1 + location: + row: 135 + column: 12 + end_location: + row: 138 + column: 8 + fix: ~ diff --git a/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_3.snap b/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_3.snap index 8a838f96b4..5638a6c38f 100644 --- a/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_3.snap +++ b/src/mccabe/snapshots/ruff__mccabe__tests__max_complexity_3.snap @@ -13,4 +13,15 @@ expression: checks row: 85 column: 0 fix: ~ +- kind: + FunctionIsTooComplex: + - handle + - 9 + location: + row: 113 + column: 4 + end_location: + row: 139 + column: 0 + fix: ~