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

F401 false positives when match statements are in the file #1989

Closed
kendangclubhouse opened this issue Jan 19, 2023 · 6 comments
Closed

F401 false positives when match statements are in the file #1989

kendangclubhouse opened this issue Jan 19, 2023 · 6 comments

Comments

@kendangclubhouse
Copy link

kendangclubhouse commented Jan 19, 2023

Im seeing that ruff is not finding F401's in files that have match statements.

Code snippet

import datetime

x = 5
match x:
    case _:
        print("hello")

You should expect that import datetime should cause a F401 but when runing ruff /path/to/file --select F401 nothing is returned. When I remove the match statement, ruff will return the F401 issue.

❯ ruff ~ruff_bug.py --select F401
ruff_bug.py:1:8: F401 `datetime` imported but unused
  |
1 | import datetime
  |        ^^^^^^^^ F401
  |
  = help: Remove unused import: `datetime

Im running ruff 0.0.225

@charliermarsh
Copy link
Member

I believe this is due to our lack of support for match statements. It's the last big language feature that we don't yet support (#282). I assume that if you --select E999, you'll see a syntax error? That's probably blocking the F401 from being picked up.

@kendangclubhouse
Copy link
Author

I find it odd that I cannot ignore the E999. Does E999 have to block all other errors in the file?

@charliermarsh
Copy link
Member

The issue is that if we can't parse the code, then we can't get a valid AST, and so we can't enforce a large portion of the rule set. It's a limitation of the parser right now: (1) it can't parse match statements, and (2) it can't recover from errors gracefully. (Even if it could, you would still end up with false positives or negative -- for example, if an import was used only in a match statement, we'd have no way of knowing.)

@kendangclubhouse
Copy link
Author

I see, that make a lot of sense. Thank you so much for explaining. 🙂

@charliermarsh
Copy link
Member

No prob! I know it’s not a satisfying answer, but hopefully it makes some sense. The real fix here is to get match statements working so that it’s a non-issue!

@charliermarsh
Copy link
Member

Fixed as of v0.0.250.

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

No branches or pull requests

2 participants