-
Notifications
You must be signed in to change notification settings - Fork 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
Local variable is assigned but never used (F841) cannot be ignored with dummy-variable-rgx in except block #6391
Comments
Interestingly, Flake8 doesn't respect the dummy-variable-rgx here either. What's the use-case? Did this occur in a real example, where you want to assign the exception but not use it? |
Hm, that is interesting. Originally I had written this in a project: try:
1 + 1
except:
logger.exception("Cannot add numbers :(") And then I got feedback that it would be nice to be able to capture the caught exception in a variable for easy access in pdb and other debuggers. So I thought it would be simple to rewrite as: try:
1 + 1
except Exception as _err:
logger.exception("Cannot add numbers :(") and was surprised when try:
1 + 1
except Exception as err:
logger.exception("Cannot add numbers :(", exc_info=err) Maybe there is a slight benefit in this final solution, as So it's an easy workaround and maybe worthwhile to keep the exact same behavior as flake8 in this scenario. |
This is the tail wagging the dog, IMHO. My team complained about |
Yeah, hmm... IMO it's strange to use that |
@tylerlaprade if it makes you feel any better for holding firm, there are other ways to expose that return value. In Pycharm you can check the 'Show Return Values' option In pdb, you can use |
## Summary This PR respects our unused variable regex when flagging bound exceptions, so that you no longer get a violation for, e.g.: ```python def f(): try: pass except Exception as _: pass ``` This is an odd pattern, but I think it's surprising that the regex _isn't_ respected here. Closes #6391 ## Test Plan `cargo test`
## Summary This PR respects our unused variable regex when flagging bound exceptions, so that you no longer get a violation for, e.g.: ```python def f(): try: pass except Exception as _: pass ``` This is an odd pattern, but I think it's surprising that the regex _isn't_ respected here. Closes astral-sh#6391 ## Test Plan `cargo test`
I have a similar use case by for walrus assignment: while _has_not_timed_out := (last_try_epoch_sec - start_epoch_sec) < timeout_sec:
if _reached_retry_interval := (time() - last_try_epoch_sec) >= retry_interval_sec: |
Example:
Ruff always triggers
F841
on theexcept Exception as _err
line:Ruff version 0.0.282
The text was updated successfully, but these errors were encountered: