Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new check B910 to suggest using Counter() instead of defaultdict(int) #489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ on the first line and urls or paths that are on their own line::
"https://some-super-long-domain-name.com/with/some/very/long/paths"
)

**B910**: Use Counter() instead of defaultdict(int) to avoid excessive memory use as the default dict will record missing keys with the default value when accessed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an unwanted trailing double quote?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 😕
Fixed


How to enable opinionated warnings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
15 changes: 15 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def visit_Call(self, node) -> None:
self.check_for_b034(node)
self.check_for_b039(node)
self.check_for_b905(node)
self.check_for_b910(node)

# no need for copying, if used in nested calls it will be set to None
current_b040_caught_exception = self.b040_caught_exception
Expand Down Expand Up @@ -1706,6 +1707,16 @@ def check_for_b909(self, node: ast.For) -> None:
):
self.errors.append(B909(mutation.lineno, mutation.col_offset))

def check_for_b910(self, node: ast.Call) -> None:
if (
isinstance(node.func, ast.Name)
and node.func.id == "defaultdict"
and len(node.args) > 0
and isinstance(node.args[0], ast.Name)
and node.args[0].id == "int"
):
self.errors.append(B910(node.lineno, node.col_offset))


def compose_call_path(node):
if isinstance(node, ast.Attribute):
Expand Down Expand Up @@ -2380,6 +2391,9 @@ def visit_Lambda(self, node) -> None:
"B909 editing a loop's mutable iterable often leads to unexpected results/bugs"
)
)
B910 = Error(
message="B910 Use Counter() instead of defaultdict(int) to avoid excessive memory use"
)
B950 = Error(message="B950 line too long ({} > {} characters)")


Expand All @@ -2392,5 +2406,6 @@ def visit_Lambda(self, node) -> None:
"B906",
"B908",
"B909",
"B910",
"B950",
]
8 changes: 8 additions & 0 deletions tests/b910.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from collections import defaultdict

a = defaultdict(int)
b = defaultdict(float)
c = defaultdict(bool)
d = defaultdict(str)
e = defaultdict()
f = defaultdict(int)
12 changes: 12 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
B907,
B908,
B909,
B910,
B950,
BugBearChecker,
BugBearVisitor,
Expand Down Expand Up @@ -1042,6 +1043,17 @@ def test_b909(self):
]
self.assertEqual(errors, self.errors(*expected))

def test_b910(self):
filename = Path(__file__).absolute().parent / "b910.py"
mock_options = Namespace(select=[], extend_select=["B910"])
bbc = BugBearChecker(filename=str(filename), options=mock_options)
errors = list(bbc.run())
expected = [
B910(3, 4),
B910(8, 4),
]
self.assertEqual(errors, self.errors(*expected))


class TestFuzz(unittest.TestCase):
from hypothesis import HealthCheck, given, settings
Expand Down