-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
False positive F401 "imported but unused" when use is cast() inside a lambda #9534
Labels
bug
Something isn't working
Comments
Thank you, very much a bug! |
charliermarsh
added a commit
that referenced
this issue
Jan 16, 2024
## Summary This is effectively the same problem as #9175. And this just papers over it again, though I'm gonna try a more holistic fix in a follow-up PR. The _real_ fix here is that we need to continue to visit deferred items until they're exhausted since, e.g., we still get this case wrong (flagging `re` as unused): ```python import re cast(lambda: re.match, 1) ``` Closes #9534.
charliermarsh
added a commit
that referenced
this issue
Jan 16, 2024
## Summary This PR is a more holistic fix for #9534 and #9159. When we visit the AST, we track nodes that we need to visit _later_ (deferred nodes). For example, when visiting a function, we defer the function body, since we don't want to visit the body until we've visited the rest of the statements in the containing scope. However, deferred nodes can themselves contain deferred nodes... For example, a function body can contain a lambda (which contains a deferred body). And then there are rarer cases, like a lambda inside of a type annotation. The aforementioned issues were fixed by reordering the deferral visits to catch common cases. But even with those fixes, we still fail on cases like: ```python from __future__ import annotations import re from typing import cast cast(lambda: re.match, 1) ``` Since we don't expect lambdas to appear inside of type definitions. This PR modifies the `Checker` to keep visiting until all the deferred stacks are empty. We _already_ do this for any one kind of deferred node; now, we do it for _all_ of them at a level above.
Thanks for the quick turnaround on this! It gives me a lot of confidence in the project |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found a fun edgy edge case running this one my code base. Ruff cannot detect use of classes in calls to
cast()
inside a lambda withfrom __future__ import annotations
on.Minimal reproducer
Running ruff results in an error:
Expected behavior
I expected Ruff to detect the use of the imported class in the call to
cast()
, but it looks like the combination offrom __future__ import annotations
and having the cast call in alambda
expression obscures that use from Ruff. Note that with the annotations future feature off this does not occur, nor does using a cast in non-lambda code seem to cause any issue.Info & versions
Ruff: 0.1.13
Python: 3.11.5
OS: Fedora 38
The text was updated successfully, but these errors were encountered: