-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement B035 check for static keys in dict-comprehension
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# OK - consts in regular dict | ||
regular_dict = {"a": 1, "b": 2} | ||
regular_nested_dict = {"a": 1, "nested": {"b": 2, "c": "three"}} | ||
|
||
# bad - const key in dict comprehension | ||
bad_const_key_str = {"a": i for i in range(3)} | ||
bad_const_key_int = {1: i for i in range(3)} | ||
|
||
# OK - const value in dict comp | ||
const_val = {i: "a" for i in range(3)} | ||
|
||
# OK - expression with const in dict comp | ||
key_expr_with_const = {i * i: i for i in range(3)} | ||
key_expr_with_const2 = {"a" * i: i for i in range(3)} | ||
|
||
# nested | ||
nested_bad_and_good = { | ||
"good": {"a": 1, "b": 2}, | ||
"bad": {"a": i for i in range(3)}, | ||
} | ||
|
||
CONST_KEY_VAR = "KEY" | ||
|
||
# bad | ||
bad_const_key_var = {CONST_KEY_VAR: i for i in range(3)} | ||
|
||
# OK - variable from tuple | ||
var_from_tuple = {k: v for k, v in {}.items()} | ||
|
||
# OK - variable from nested tuple | ||
var_from_nested_tuple = {v2: k for k, (v1, v2) in {"a": (1, 2)}.items()} | ||
|
||
# bad - variabe not from generator | ||
v3 = 1 | ||
bad_var_not_from_nested_tuple = {v3: k for k, (v1, v2) in {"a": (1, 2)}.items()} |
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