Skip to content

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

Open
@Hellebore

Description

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.

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions