pylint-dev/pylint

Unnecessary use of a comprehension: Wrong fix

Closed

#8,256 opened on Feb 10, 2023

 (3 comments) (0 reactions) (0 assignees)Python (1,059 forks)batch import
Bug :beetle:Good first issueMinor 💅Needs PR

Repository metrics

Stars
 (4,978 stars)
PR merge metrics
 (Avg merge 12h 9m) (29 merged PRs in 30d)

Description

Bug description

# pylint: disable=missing-docstring
dict1 = {(1, 2): 3}
dict2 = {a: b for a, b in dict1}

Configuration

No response

Command used

pylint x.py

Pylint output

************* Module a
a.py:x:3: [R1721(unnecessary-comprehension), ] Unnecessary use of a comprehension, use dict(dict1) instead.

Expected behavior

unnecessary-comprehension is correct, but since dict1 already is a dict, dict(dict1) would simply create a copy of dict1.

The correct fix here would be: dict(dict1.keys())

Pylint version

pylint 2.13.9
astroid 2.11.7
Python 3.6.8 (default, Mar 18 2021, 08:58:41) 
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

OS / Environment

Red Hat 8

Additional dependencies

No response

Contributor guide