flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Detect unintended comma at the end of expression

Open xuhcc opened this issue 7 years ago • 6 comments
trafficstars

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,).

xuhcc avatar Mar 20 '18 09:03 xuhcc

That would be neat, indeed. Sadly, it's not trivial since our AST doesn't include this information.

ambv avatar Mar 20 '18 20:03 ambv

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.

carljm avatar Mar 20 '18 20:03 carljm

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.

ambv avatar Mar 20 '18 20:03 ambv

Not to deter adding this to bugbear, but flake8_tuple finds unintended tuples.

gaul avatar Jun 13 '18 18:06 gaul

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.

ambv avatar Jun 13 '18 19:06 ambv

https://pypi.org/project/flake8-commas/ is another option.

DylanYoung avatar Jul 03 '20 20:07 DylanYoung