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

Clean up unused variables after fixes #6254

Open
tylerlaprade opened this issue Aug 1, 2023 · 9 comments
Open

Clean up unused variables after fixes #6254

tylerlaprade opened this issue Aug 1, 2023 · 9 comments
Labels
fixes Related to suggested fixes for violations

Comments

@tylerlaprade
Copy link
Contributor

x = None
try:
    x = func_that_might_throw()
except AttributeError:
    pass
return x

is getting simplified to

x = None  # Variable "x" is not accessed
with contextlib.suppress(AttributeError):
    return func_that_might_throw()

If this is the first time we've seen x, can we clean up the null assignment?

@zanieb zanieb added fixes Related to suggested fixes for violations accepted Ready for implementation labels Aug 1, 2023
@zanieb
Copy link
Member

zanieb commented Aug 1, 2023

Thanks for the report! Seems reasonable to me although it may be non-trivial.

@charliermarsh
Copy link
Member

I believe this works as expected, assuming you enable the right set of rules: ruff check foo.py --select SIM --select RET --select F -n --fix.

We like to treat each fix as independent. Here, the SIM105 rule gives you:

import contextlib
def f():
    x = None
    with contextlib.suppress(AttributeError):
        x = func_that_might_throw()

    return x

Then the RET rule simplifies to:

import contextlib
def f():
    x = None
    with contextlib.suppress(AttributeError):
        return func_that_might_throw()

Then the F rule identifies the unused variable:

import contextlib
def f():
    with contextlib.suppress(AttributeError):
        return func_that_might_throw()

(This all happens in one ruff invocation, I'm just breaking it down for clarity.)

Perhaps you have F841 disabled?

@tylerlaprade
Copy link
Contributor Author

Ahh, @charliermarsh is correct.

unfixable = ["B007", "ERA001", "F841", "RUF100"]

We disabled autofix on F841 because variables are often temporarily unused while rearranging a file. We don't want an errant ⌘ + S to accidentally delete useful code. In this case, though, there's no danger since it's only unused as a result of the other (safe) autofixes.

@zanieb
Copy link
Member

zanieb commented Aug 1, 2023

@tylerlaprade I agree that seems like a fair use-case. We probably don't want to resolve this just for this specific rule though, instead we should work on a general solution that checks for unused variables introduced by an autofix.

Would you mind if I retitle this issue?

@tylerlaprade
Copy link
Contributor Author

Yes, please do! I'm glad this could potentially turn out to be more generally applicable than just a single rule.

@zanieb zanieb changed the title SIM105 should clean up unused variable after refactor Clean up unused variables after fixes Aug 1, 2023
@zanieb
Copy link
Member

zanieb commented Aug 1, 2023

In the meantime, for this use-case, I'd recommend leaving F841 enabled in your configuration and only disabling it in the flags passed when used by the IDE. Would that help?

@zanieb zanieb removed the accepted Ready for implementation label Aug 1, 2023
@tylerlaprade
Copy link
Contributor Author

Yes, that's a good suggestion, thank you Zanie!

@tylerlaprade
Copy link
Contributor Author

Ah, no luck - that same flag is used for the recursive fixes, so ⌘ + S still has the same behavior.

@charliermarsh
Copy link
Member

I think the suggestion was to leave you with a setup such that unused variables would be removed when running via the CLI, but not when running in the editor (which could happen "by accident").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

No branches or pull requests

3 participants