PyCQA / PyCQA/flake8-bugbear

New rule to catch multidimensional array multiplication

Open
#271 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python FAQ example linked in the issue and review flake8-bugbear's existing rule implementations and tests to understand how a new warning is specified. Reproduce the multidimensional list pattern, then define the rule's detectable scope and verify that tests cover the reported case and its expected warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.