Avoid incrementing McCabe complexity for class methods (#857)

This commit is contained in:
Charlie Marsh
2022-11-21 13:30:36 -05:00
committed by GitHub
parent 7bd6db62d9
commit cd1e07f37c
4 changed files with 119 additions and 2 deletions

View File

@@ -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())

View File

@@ -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);
}
_ => {}

View File

@@ -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: ~

View File

@@ -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: ~