flake8-bugbear
flake8-bugbear copied to clipboard
Proposed Check: Unintentional str concat
One of the common mistakes I can make is to miss out a comma in collections of literal strings, resulting in the strings being concatenated.
my_list = ["a", "b", "c" "d"] # note the missing comma between 'c' and 'd'
print(my_list) # ["a", "b", "cd"]
I've not come across a time I've ever used this intentionally, and will either explicitly write "c" + "d"
or use a format string "{a} {b}
when a
and b
are variables.
If you intend this using brackets might be a fine way. my_list = ["a", "b", ("c" "d")]
. Similar to the trailing comma tuple mistake where flake8-comma flags my_int = 5,
but allows the more explicit my_int = (5,)
.
The above would mean multiline strings could be concerted still as:
my_multiline = (
"part one "
"part two"
)
As soon as a comma is introduced though it becomes a tuple, and I'd argue each str literal should have a comma between it or none should.
I have also personally hit this and would love this added to bugbear.
I like the using parens "()" to make this non + concatting explicit, although I would prefer people just always use '+'. I bet this will cause a lot of noise to our users but will find bugs.
If you have the cycles, I'd take a PR, but would request very good unit testing here to ensure we're covering all of our edge cases please!
+1 for me
In the past, I've used https://github.com/Instagram/Fixit to identify things like this. Reducing the tools necessary would be great, and they may have an example implementation that can be used as a foundation.
Legacy rule they had for preventing implicit concatenation: https://github.com/Instagram/Fixit/blob/c3f8ff9e6cfa2199c6154eefea24ea39e5278dc6/legacy/rules/no_implicit_concat.py