-
Notifications
You must be signed in to change notification settings - Fork 179
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
Warn on Unassigned ==
Expression as Statement
#237
Comments
Original comment by jayvdb (@jayvdb?) on Launchpad: A minimal fix detecting ast.Compare within ast.Expr is simple. After a little playing, I think the best vector to approach this is to add checking of ast.Expr children nodes. One significant problem is the pyflakes test suite extensively uses code snippets that include unused expressions so the snippet is simple, so a good fix will need to alter lots of tests so the code tested is more like real code. |
Original comment by jayvdb (@jayvdb?) on Launchpad: "..like Attribute and Name which should have side effects but often do.." should have been "..like Attribute and Name which should not have side effects but often do.." I've put up a PR for an even more aggressive solution that Scott proposed, reporting at any Expr which is likely unused. It doesnt introduce many tests explicitly about the new error condition; at present I am mostly interested in whether any sensible use of Expr are being prevented with this new error. That still allows plain Also allowed is |
Original comment by jayvdb (@jayvdb?) on Launchpad: Agreed. However many of the side-effects of "a[b] == c", most notably defaultdict population, could be obtained by using "a[b]" instead. "a[b]" can be permitted by adding ast.Subscript to the list of allowed Expr node types. If pyflakes really needs to permit "a[b] == c", could we introduce a 'strict' mode that reports extremely dubious cases. Worth noting there are only four hits in the cpython tip The other three of them are code introduced for http://bugs.python.org/issue23780 , which is intentionally triggering an exception. |
Original report by scoutoss on Launchpad:
Working with some NumPy code recently, I accidentally wrote this:
After a few minutes of debugging my NaN-less array, I realized that I had accidentally written
==
instead of=
, turning my assignment statement into no-op comparison operator.I'd be nice if PyFlakes gave a warning for a statement that's just an expression performing an equality comparison. There are very few cases where I'd expect that something like this is doing what the author intended.
A more aggressive version of this check would be to warn on any expression that's just a binary operator. The assumption there would be that the "statement"
a + b
is at best dead code, and at worst an operator with a highly non-obvious side-effect.I'd be happy to work on either version of this if there's support for it.
The text was updated successfully, but these errors were encountered: