sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.5k stars 65 forks source link

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

Open Hellebore opened 8 months ago

Hellebore commented 8 months ago

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 commented 8 months ago

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