Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PLR0912: branches inside with statement not counted #11313

Closed
Kakadus opened this issue May 6, 2024 · 2 comments · Fixed by #11321
Closed

PLR0912: branches inside with statement not counted #11313

Kakadus opened this issue May 6, 2024 · 2 comments · Fixed by #11321
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Kakadus
Copy link

Kakadus commented May 6, 2024

PLR0912(too-many-branches) does not count branches inside with: blocks. With pylint the following is detected, while ruff does not detect this rule violation

from contextlib import suppress
def capital(country):
    with suppress(Exception):
        if country == "Australia":
            return "Canberra"
        elif country == "Brazil":
            return "Brasilia"
        elif country == "Canada":
            return "Ottawa"
        elif country == "England":
            return "London"
        elif country == "France":
            return "Paris"
        elif country == "Germany":
            return "Berlin"
        elif country == "Poland":
            return "Warsaw"
        elif country == "Romania":
            return "Bucharest"
        elif country == "Spain":
            return "Madrid"
        elif country == "Thailand":
            return "Bangkok"
        elif country == "Turkey":
            return "Ankara"
        elif country == "Undefined":
            return None
        else:
            return "Some"

Ruff version: 0.4.3
command: ruff check test.py --select PLR0912 --isolated

@charliermarsh charliermarsh added the bug Something isn't working label May 6, 2024
@charliermarsh
Copy link
Member

Seems like a bug.

@dhruvmanila dhruvmanila added the good first issue Good for newcomers label May 7, 2024
@dhruvmanila
Copy link
Member

This seems like an easy fix which is to just include the Stmt::With in this function:

fn num_branches(stmts: &[Stmt]) -> usize {

And then to add some test cases.

charliermarsh pushed a commit that referenced this issue May 8, 2024
Resolves #11313

## Summary

PLR0912(too-many-branches) did not count branches inside with: blocks.
With this fix, the branches inside with statements are also counted.

## Test Plan

Added a new test case.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants