flake8-bugbear
flake8-bugbear copied to clipboard
New rule to catch multidimensional array multiplication
So recently I've come across some code that looks like this:
grid = [[0] * 3] * 3
Turns out this innocent looking piece of code does not do what I wanted, and the reason is when a sequence is multiplied, it's values are shallow copied, so all 3 rows will be pointing to the exact same copies in the created grid.
I've search all over the internet and for the life of me I cannot find a single python linter that will catch this. It'd be nice if bugbear can add a rule to deal with this mistake.
https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
I understand implementing this rule correctly can potentially be rather complex and slow as you may need to chase down the type of the array values, which could come from a variable, so as an alternative, perhaps a by default disabled rule to warn about sequence multiplication will be a good start.