Closed
Description
Bug description
unnecessary-comprehension
provides an incorrect suggestion when the collection is modified after the new list is created. The result of a comprehension is a new object and hence the suggestion should also always result in a new object to avoid code regressions.
I saved the following code in a file called test.py
to produce the output below.
#pylint: disable=missing-module-docstring
a = [1, 2, 3]
b = [x for x in a]
b[0] = 0
print(a) # [1, 2, 3]
After changing b = [x for x in a]
to b = a
based on the suggestion, the script now prints [0, 2, 3]
. The correct suggestion should be use list(a)
to preserve the original behavior.
Configuration
No response
Command used
pylint test.py
Pylint output
$ pylint test.py
************* Module test
test.py:3:4: R1721: Unnecessary use of a comprehension, use a instead. (unnecessary-comprehension)
Expected behavior
$ pylint test.py
************* Module test
test.py:3:4: R1721: Unnecessary use of a comprehension, use list(a) instead. (unnecessary-comprehension)
Pylint version
pylint 2.17.6
astroid 2.15.7
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]
OS / Environment
Windows 10
Additional dependencies
No response