astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
28.49k stars 924 forks source link

linter: uncovered branches #11778

Open antonio-antuan opened 3 weeks ago

antonio-antuan commented 3 weeks ago

I'd like to have a linter that checks that all variants are covered by match expression. An example:

from enum import Enum

class Foo(Enum):
    A = 1
    B = 2

def foo(f: Foo):
    var: int
    match f:
        case Foo.A:
            var = 1
    print(var)

foo(Foo.B)

ruff check ./ reports that All checks are passed. However, if you run the code it raises UnboundLocalError. The way to solve it is simple (I guess): check that all branches are covered. Definitely there can be more complex expression, like case Foo.A | Foo.B, case _ which would be great to cover as well.

MichaReiser commented 3 weeks ago

That makes sense. I don't think Ruff is capable of running this today. It will require type inference to know what f evaluates to, to know which branches are possible. We're working on adding support for type-inference but it will take us a while.