sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.51k stars 65 forks source link

List comprehension may change the original result #311

Closed luke396 closed 1 year ago

luke396 commented 1 year ago

Checklist

Description

Code snippet that reproduces issue

row = ['_'] * 3
weird_board = []
for _ in range(3):
    weird_board.append(row)
weird_board[1][2] = 'X'
weird_board # [['_', '_', 'X'], ['_', '_', 'X'], ['_', '_', 'X']]
board = []
for _ in range(3):
    row = ['_'] * 3
    board.append(row)
board[1][2] = 'X'
board # [['_', '_', '_'], ['_', '_', 'X'], ['_', '_', '_']]

For these two snippets, Sourcery gives the same quick fix, but the it changes the result.

row = ['_'] * 3
weird_board = [row for _ in range(3)]
weird_board[1][2] = 'X'
weird_board # [['_', '_', 'X'], ['_', '_', 'X'], ['_', '_', 'X']]
row = ['_'] * 3
board = [row for _ in range(3)]
board[1][2] = 'X'
board # [['_', '_', 'X'], ['_', '_', 'X'], ['_', '_', 'X']]

Debug Information

IDE Version: VSCode 1.74.2

Sourcery Version: Sourcery extension v1.0.2

Operating system and Version: Windows 11 WSL 2

Hellebore commented 1 year ago

Now fixed - will be in the next release.

Hellebore commented 1 year ago

Released with 1.0.4