forked from pylint-dev/pylint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented new check unnecessary-dict-index-lookup (pylint-dev#4485)
- Loading branch information
Showing
5 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/functional/u/unnecessary/unnecessary_dict_index_lookup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# pylint: disable=missing-docstring, too-few-public-methods, expression-not-assigned, line-too-long | ||
|
||
a_dict = dict() | ||
b_dict = dict() | ||
|
||
for k, v in a_dict.items(): | ||
print(a_dict[k]) # [unnecessary-dict-index-lookup] | ||
print(b_dict[k]) # Should not emit warning, accessing other dictionary | ||
a_dict[k] = 123 # Should not emit warning, key access necessary | ||
a_dict[k] += 123 # Should not emit warning, key access necessary | ||
print(a_dict[k]) # [unnecessary-dict-index-lookup] | ||
k = "another key" | ||
print(a_dict[k]) # This is fine, key reassigned | ||
|
||
|
||
# Tests on comprehensions | ||
{v: 1 for k, v in a_dict.items() if a_dict[k]} # [unnecessary-dict-index-lookup] | ||
{v: 1 for k, v in a_dict.items() if k} # This is fine, no indexing | ||
{a_dict[k]: 1 for k, v in a_dict.items() if k} # [unnecessary-dict-index-lookup] | ||
{a_dict[k]: 1 for k, v in a_dict.items() if a_dict[k]} # [unnecessary-dict-index-lookup, unnecessary-dict-index-lookup] | ||
|
||
[v for k, v in a_dict.items() if a_dict[k]] # [unnecessary-dict-index-lookup] | ||
[v for k, v in a_dict.items() if k] # This is fine, no indexing | ||
[a_dict[k] for k, v in a_dict.items() if k] # [unnecessary-dict-index-lookup] | ||
[a_dict[k] for k, v in a_dict.items() if a_dict[k]] # [unnecessary-dict-index-lookup, unnecessary-dict-index-lookup] | ||
|
||
|
||
# Tests on dict attribute of a class | ||
class Foo: | ||
c_dict = {} | ||
|
||
for k, v in Foo.c_dict.items(): | ||
print(b_dict[k]) # Should not emit warning, accessing other dictionary | ||
print(Foo.c_dict[k]) # [unnecessary-dict-index-lookup] | ||
Foo.c_dict[k] += Foo.c_dict[k] # [unnecessary-dict-index-lookup] | ||
Foo.c_dict[k] += v # key access necessary | ||
|
||
# Tests on comprehensions | ||
{v: 1 for k, v in Foo.c_dict.items() if Foo.c_dict[k]} # [unnecessary-dict-index-lookup] | ||
{v: 1 for k, v in Foo.c_dict.items() if k} # This is fine, no indexing | ||
{Foo.c_dict[k]: 1 for k, v in Foo.c_dict.items() if k} # [unnecessary-dict-index-lookup] | ||
{Foo.c_dict[k]: 1 for k, v in Foo.c_dict.items() if Foo.c_dict[k]} # [unnecessary-dict-index-lookup, unnecessary-dict-index-lookup] | ||
|
||
[v for k, v in Foo.c_dict.items() if Foo.c_dict[k]] # [unnecessary-dict-index-lookup] | ||
[v for k, v in Foo.c_dict.items() if k] # This is fine, no indexing | ||
[Foo.c_dict[k] for k, v in Foo.c_dict.items() if k] # [unnecessary-dict-index-lookup] | ||
[Foo.c_dict[k] for k, v in Foo.c_dict.items() if Foo.c_dict[k]] # [unnecessary-dict-index-lookup, unnecessary-dict-index-lookup] | ||
|
||
# Test assigning d.items() to a single variable | ||
d = {1: "a", 2: "b"} | ||
for item in d.items(): | ||
print(item[0]) | ||
print(d[item[0]]) # [unnecessary-dict-index-lookup] | ||
|
||
[item[0] for item in d.items()] | ||
[d[item[0]] for item in d.items()] # [unnecessary-dict-index-lookup] | ||
|
||
# Reassigning single var | ||
for item in d.items(): | ||
print(item[0]) | ||
print(d[item[0]]) # [unnecessary-dict-index-lookup] | ||
item = (2, "b") | ||
print(d[item[0]]) # This is fine, no warning thrown as key has been reassigned |
23 changes: 23 additions & 0 deletions
23
tests/functional/u/unnecessary/unnecessary_dict_index_lookup.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
unnecessary-dict-index-lookup:7:10::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:11:10::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:17:36::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:19:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:20:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:20:44::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:22:33::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:24:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:25:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:25:41::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:34:10::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:35:21::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:39:40::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:41:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:42:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:42:52::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:44:37::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:46:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:47:1::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:47:49::Unnecessary dictionary index lookup, use 'v' instead | ||
unnecessary-dict-index-lookup:53:10::Unnecessary dictionary index lookup, use 'item[1]' instead | ||
unnecessary-dict-index-lookup:56:1::Unnecessary dictionary index lookup, use 'item[1]' instead | ||
unnecessary-dict-index-lookup:61:10::Unnecessary dictionary index lookup, use 'item[1]' instead |