-
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
PLR0912: Pylint counts try: ... except:
statements as single branch
#11205
Comments
The rule was added in #2550 but there's no conversation in that PR nor is there any change to the rule explaining if that divergence is indeed intentional. @chanman3388 as author of the rule. What's your take on this? To me, counting |
I don't believe that this divergence was intentional, pylint does some odd things, I'd be inclined to agree with @MichaReiser that is is its own branch or brances, but I guess the question to me is whether we should remain faithful to pylint's implementation which looks like: def visit_try(self, node: nodes.Try) -> None:
"""Increments the branches counter."""
branches = len(node.handlers)
if node.orelse:
branches += 1
if node.finalbody:
branches += 1
self._inc_branch(node, branches)
self._inc_all_stmts(branches) or if we should go with our own interpretation. Looking at the python AST I can't tell if this was oversight or if the intention was to consider exceptions differently? |
It looks like the difference is whether the try body counts as a branch? I'd say it should, probably. Although it's perhaps strange that |
I'd be fine to remove the body from the count though. It's a bit more consistent with Pylint and is pretty marginal either way. |
Related: #11421 |
PLR0912 is not equivalent to pylint. The following function is accepted by pylint but rejected by ruff:
Pylint only detects 12 branches, while ruff counts 13. Is this a bug or a feature? If the latter, it should probably be documented. I can also imagine a setting, enabling/disabling this behavior
The text was updated successfully, but these errors were encountered: