-
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
E731 auto-fix is too aggressive #5421
Comments
@adampauls can you please include the code after the autofix and the pyright complaint? |
Done |
@adampauls that fix looks fine to me and runs correctly > func(Foo(my_lambda=lambda: 1), True)
1
> func(Foo(my_lambda=lambda: 1), False)
5 What's the bug / pyright complaint? |
Pyright says
I guess you can argue that |
This might be a case where there could be another suggestion for refactoring the code. Is it possible to share a more realistic snippet? |
Here's a link to the original code |
Thanks :) Hmm, perhaps we could avoid flagging E731 when the variable already exists as an annotation? E.g., based on the |
FWIW I don't even mind the lint error, I like opinionated linters. I just don't like opinionated autofixers, since you might not even realize that code was changed behind the scenes if you're running it over lots of code. If it were me, I would disable the autofix if the variable is assigned or declared anywhere other than the place where it is assigned to a lambda (before or after). |
I think that's reasonable. We could mark it as a manual fix in that case. |
## Summary This PR makes E731 a "manual" fix in one other context: when the lambda is shadowing another variable in the scope. Function declarations (with shadowing) cause issues for type checkers, and so rewriting an annotation, e.g., in branches of an `if` statement can lead to failures. Closes #5421.
Thanks! |
Consider the following code:
Ruff will autofix the
noqa
line, but that introduces a bug, at least I think it does. Certainlypyright
complains.EDIT:
After the autofix, the code is
The text was updated successfully, but these errors were encountered: