-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from malthejorgensen/groupby-multiple-usages
Add B031: Warn when using `groupby()` result multiple times
- Loading branch information
Showing
4 changed files
with
144 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,64 @@ | ||
""" | ||
Should emit: | ||
B030 - on lines 29, 33, 43 | ||
""" | ||
import itertools | ||
from itertools import groupby | ||
|
||
shoppers = ["Jane", "Joe", "Sarah"] | ||
items = [ | ||
("lettuce", "greens"), | ||
("tomatoes", "greens"), | ||
("cucumber", "greens"), | ||
("chicken breast", "meats & fish"), | ||
("salmon", "meats & fish"), | ||
("ice cream", "frozen items"), | ||
] | ||
|
||
carts = {shopper: [] for shopper in shoppers} | ||
|
||
|
||
def collect_shop_items(shopper, items): | ||
# Imagine this an expensive database query or calculation that is | ||
# advantageous to batch. | ||
carts[shopper] += items | ||
|
||
|
||
# Group by shopping section | ||
for _section, section_items in groupby(items, key=lambda p: p[1]): | ||
for shopper in shoppers: | ||
collect_shop_items(shopper, section_items) | ||
|
||
for _section, section_items in groupby(items, key=lambda p: p[1]): | ||
collect_shop_items("Jane", section_items) | ||
collect_shop_items("Joe", section_items) | ||
|
||
|
||
for _section, section_items in groupby(items, key=lambda p: p[1]): | ||
# This is ok | ||
collect_shop_items("Jane", section_items) | ||
|
||
for _section, section_items in itertools.groupby(items, key=lambda p: p[1]): | ||
for shopper in shoppers: | ||
collect_shop_items(shopper, section_items) | ||
|
||
for group in groupby(items, key=lambda p: p[1]): | ||
# This is bad, but not detected currently | ||
collect_shop_items("Jane", group[1]) | ||
collect_shop_items("Joe", group[1]) | ||
|
||
|
||
# Make sure we ignore - but don't fail on more complicated invocations | ||
for _key, (_value1, _value2) in groupby( | ||
[("a", (1, 2)), ("b", (3, 4)), ("a", (5, 6))], key=lambda p: p[1] | ||
): | ||
collect_shop_items("Jane", group[1]) | ||
collect_shop_items("Joe", group[1]) | ||
|
||
# Make sure we ignore - but don't fail on more complicated invocations | ||
for (_key1, _key2), (_value1, _value2) in groupby( | ||
[(("a", "a"), (1, 2)), (("b", "b"), (3, 4)), (("a", "a"), (5, 6))], | ||
key=lambda p: p[1], | ||
): | ||
collect_shop_items("Jane", group[1]) | ||
collect_shop_items("Joe", group[1]) |
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