dosisod / refurb

A tool for refurbishing and modernizing Python codebases
GNU General Public License v3.0
2.48k stars 54 forks source link

[Bug]: FURB126 shouldn't catch matches on complex expressions #336

Open mscheifer opened 6 months ago

mscheifer commented 6 months ago

Has your issue already been fixed?

The Bug

The following code:

def expensive_func_or_complex_expression(a):
    # <snip logic...>
    return a

def foo(bar):
    match expensive_func_or_complex_expression(bar):
        case "a":
            return 123
        case result:
            return result

Emits the following error:

$ refurb testcase.py
testcase.py:11:13 [FURB126]: Replace `case _: return x` with `return x`

Run `refurb --explain ERR` to further explain an error. Use `--quiet` to silence this message

But it should not be emitting an error instance because the match is on an expression that I don't want to repeat.

The most straightforward interpretation of the suggested fix would be doing:

def foo(bar):
    match expensive_func_or_complex_expression(bar):
        case "a":
            return 123
    return expensive_func_or_complex_expression(bar)

but that's bad.

Doing

def foo(bar):
    result = expensive_func_or_complex_expression(bar)
    match result:
        case "a":
            return 123
    return result

is not too bad but I prefer the original form. This also isn't a super obvious change to make based on the error suggestion as it mentions case _ but I'm not using the underscore pattern.

Version Info

Refurb: v2.0.0
Mypy: v1.9.0

Python Version

Python 3.10.12

Config File

# N/A

Extra Info

None