-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make the mccabe checker ignore the case of a single top-level elif chain or match statement #3597
Comments
I'd like to work on this. |
Confirming my understanding: are you describing something akin to a top-level switch statement? Like: if foo == "bar":
return 1
elif foo == "baz:
return 2:
else ... |
Yes, exactly. There is a clear difference in complexity between a match statement with 10 cases, and a piece of code that has 3 nested for loops with if-clauses randomly distributed inside. In the case where you are doing the switch based on type you can of course split it up with the functools.dispatch decorator or methods, and in the case where you just return a value you can use a dictionary, but apart from those cases, one big match with any nested behaviour split off into a separate function is usually simpler than any replacement. |
@saolof I agree, and I think it can sometimes be detrimental to try and break apart code like this. The result isn't likely to be easier for a human to reason about, and is likely to lower cohesion. Single-level structures are the exception to the rule, because they are already easy to reason about and follow. |
Sorry, I haven't had much time to spend on this. I might get to it at some point, but if someone else is keen to work on this, please go ahead. |
This is a somewhat common edge case where there shouldn't be a need to use noqa comments. Non-looping functions with a nesting depth of 1 should be excluded from the mccabe checker, or there should be a separate error code for this particular kind of pattern if you want to lint or ignore it separately, such as "elif chain exceeds mccabe complexity".
Incidentally, while discussing code complexity KPIs, being able to set a maximum allowed nesting depth would be nice.
The text was updated successfully, but these errors were encountered: