flake8-bugbear
flake8-bugbear copied to clipboard
Detect unintended comma at the end of expression
I would be great to have a rule that will detect cases such this:
instance = MyClass(arg1, arg2),
As it is most likely a typo.
If someone wants a tuple, the preferred form would be (item,).
That would be neat, indeed. Sadly, it's not trivial since our AST doesn't include this information.
It is possible, though, since flake8 can give access to the raw source, and then can use Python's parser module to get a parse tree and work on that.
I'm only worried about the additional overhead for just this one check. lib2to3 is in Python so it's going to slow down Bugbear a bit. We can do it and measure how bad that slowdown is but I have a feeling this is a better job for Black to always insert parentheses around single-element tuples to make this obvious.
Not to deter adding this to bugbear, but flake8_tuple finds unintended tuples.
If you use http://github.com/ambv/black/, it will helpfully wrap one-tuples in parentheses to make this more obvious. So if this wasn't intentional, it will be easier to spot.
https://pypi.org/project/flake8-commas/ is another option.