ruff
ruff copied to clipboard
Add convert exit() to sys.exit() rule
- [ ] Fix
- [x] It fixes when
- [x]
import sys - [x]
import sys as sys2 - [x] ~
import sys.exit~ does not work - [x]
from sys import exit as exit2
- [x]
- [ ] It does not fix when not it has to import
sys. Is there an example of how to add an import while keeping it sorted ifIis selected?
- [x] It fixes when
- [ ]
exit()is not detected inside functions. What did I do wrong? Is itcurrent_scope()that is not enough? Do I need to iterate over every scope?
It does not fix when not it has to import
sys.
Hmm, no, we don't do that anywhere yet. Let's try for that later, as it would require fixes with multiple edits that change multiple parts of the tree.
exit() is not detected inside functions. What did I do wrong? Is it current_scope() that is not enough? Do I need to iterate over every scope?
Yeah, yeah you have to iterate over every scope in reverse, like:
for scope_index in self.scope_stack.iter().rev() {
let scope = &mut self.scopes[*scope_index];
...
}
We might want to add a .current_scope()-like method to Checker to expose that iterate to plugins.
@charliermarsh,
It does not fix when not it has to import
sys.Hmm, no, we don't do that anywhere yet. Let's try for that later, as it would require fixes with multiple edits that change multiple parts of the tree.
Let's open an issue once this PR is merged.
exit() is not detected inside functions. What did I do wrong? Is it current_scope() that is not enough? Do I need to iterate over every scope?
Yeah, yeah you have to iterate over every scope in reverse, like:
for scope_index in self.scope_stack.iter().rev() { let scope = &mut self.scopes[*scope_index]; ... }We might want to add a
.current_scope()-like method toCheckerto expose that iterate to plugins.
I added Checker.current_scopes(). But, even if I iterate over every scope, it still does not work.
Or maybe I made an error.
@JonathanPlasse - I think your code is actually right! But the test case is a bit off. Right now, you have this:
...
def main():
exit(6) # Is not detected
...
def exit(e):
print(e)
...
So, you're re-defining exit at the bottom, and when the main() with exit(6) is evaluated, exit is no longer the built-in. If you remove the def exit, the error is picked up -- which matches Python's semantics.
I think you'll need to split into a few different test files to evaluate all of those cases.
@JonathanPlasse - Feel free to mark as "ready for review" when you're happy with this!
I am ready for review :)
Is it possible to make a pre-commit release?
Is it normal that I have to specify --fixable in ruff --fix --fixable RUF --select RUFF file.py for the fix to work?
Great! Will review tonight!
@JonathanPlasse - No, --fixable shouldn't be necessary. That's a bug, will PR, one sec...
(Fixed in https://github.com/charliermarsh/ruff/pull/838.)