Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Hellebore opened this issue Nov 6, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Hellebore
Copy link
Collaborator

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 Hellebore added the enhancement New feature or request label Nov 6, 2023
@Hellebore
Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant