sourcery icon indicating copy to clipboard operation
sourcery copied to clipboard

`assign-if-exp` can make code in a switch-like statement a bit less clear

Open Hellebore opened this issue 1 year ago • 1 comments

Description

Consider the following code:

    if nanoseconds < 1000:
        return "nanoseconds"
    if nanoseconds < 1000_000:
        return "microseconds"
    if nanoseconds < 1000_000_000:
        return "milliseconds"
    return "seconds"

sourcery will try to reformat it to the following:

def get_best_order(nanoseconds: float) -> str:
    if nanoseconds < 1000:
        return "nanoseconds"
    if nanoseconds < 1000_000:
        return "microseconds"
    return "milliseconds" if nanoseconds < 1000_000_000 else "seconds"

The issue is obvious; Previously each return was exactly one case, but now two of them get mangled together. Even though this is generally a good refactoring, that I try to follow, in cases that there is a "hidden switch" statement, it becomes annoying.

Hellebore avatar Nov 06 '23 11:11 Hellebore

This does look like a case whih could do with some examination/tweaking

Hellebore avatar Nov 06 '23 11:11 Hellebore