-
Notifications
You must be signed in to change notification settings - Fork 896
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
F401 has started being incorrectly missed #3378
Comments
It should have been in the changelogs, and no, I don't like the change. It wasn't broken. If you import something, you should use it (that's the point of the check). If you only want to ask if something is there, that's what What about just making the error message smarter in the case it's inside the block, giving the |
|
Yeah I hear you, it is in the changelog, but it's marked as a bugfix rather than a breaking change (https://github.com/charliermarsh/ruff/releases/tag/v0.0.254 -- search for I think you're right that we can do better here. Just to explain the reasoning a bit more: prior to this change, the autofix was unsafe in those cases, because the import itself affected the If we revert, and add a more refined error message, would you agree that we should still avoid autofixing, since the import in that case does have a side-effect? (Aside: we could also look at the |
Ahh, I was searching for the error code. I agree about the autofix; I've had that happen once, and it's a bit annoying. But all auto fixes that remove lines of code can be - T201 can delete all I'd agree that keeping it from being autofixed would be very helpful. In fact, I think the problem is that it's the wrong autofix, since this structure is not a no-op. It would be better to instead autofix it into the The |
Yeah it's tricky. If the user has: try:
import something
except ModuleNotFound:
pass And try:
importlib.util.find_spec("something") # I can't remember the exact API
except ModuleNotFound:
... That could also be unexpected, but of course it's hard to know, we don't have enough information there to confidently infer the intent. As a compromise, then, what we could do is:
|
It's: if importlib.util.find_spec("something") is None:
... ;) Also, autofixing always has this issue. Deleting unused variables, etc all can really make a mess if you make a mistake. I even have a few checks I usually suggest marking unfixable - see https://scikit-hep.org/developer/style#ruff - just because of how easy it is to have them be a bit surprising. But, IMO, that's completely okay - autofixers have permission to change code - if you didn't like it doing that, you wouldn't enable I'm not very seriously suggesting an autofix to this, though a suggestion including |
I still intend to change this. |
This pattern suddenly has stopped triggering F401:
If
something
is never used, the F401 is correct. It's helpful to tell the reader that this is not going to be used later, and it's a hint that this is actually often an incorrect pattern (the correct one in Python 3.4+ is to useimportlib.utils.find_spec("something")
, which won't trigger the error and will be faster, since it doesn't have to run theLoader
, and won't trigger side effects).I couldn't find anything in the changelogs or PRs about it, but happened between v0.0.253 and v0.0.254. scikit-hep/hist#482 and pypa/build#585 are two recent cases where this is happening.
The text was updated successfully, but these errors were encountered: