Skip to content

Commit

Permalink
add some comments, rename b008_extend_immutable_calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Jun 25, 2024
1 parent 70a8853 commit 6fee565
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def run(self):
self.load_file()

if self.options and hasattr(self.options, "extend_immutable_calls"):
b008_extend_immutable_calls = set(self.options.extend_immutable_calls)
b008_b039_extend_immutable_calls = set(self.options.extend_immutable_calls)
else:
b008_extend_immutable_calls = set()
b008_b039_extend_immutable_calls = set()

b902_classmethod_decorators: set[str] = B902_default_decorators
if self.options and hasattr(self.options, "classmethod_decorators"):
Expand All @@ -74,7 +74,7 @@ def run(self):
visitor = self.visitor(
filename=self.filename,
lines=self.lines,
b008_extend_immutable_calls=b008_extend_immutable_calls,
b008_b039_extend_immutable_calls=b008_b039_extend_immutable_calls,
b902_classmethod_decorators=b902_classmethod_decorators,
)
visitor.visit(self.tree)
Expand Down Expand Up @@ -360,7 +360,7 @@ def visit_ExceptHandler(self, node: ast.ExceptHandler):
class BugBearVisitor(ast.NodeVisitor):
filename = attr.ib()
lines = attr.ib()
b008_extend_immutable_calls = attr.ib(default=attr.Factory(set))
b008_b039_extend_immutable_calls = attr.ib(default=attr.Factory(set))
b902_classmethod_decorators = attr.ib(default=attr.Factory(set))
node_window = attr.ib(default=attr.Factory(list))
errors = attr.ib(default=attr.Factory(list))
Expand Down Expand Up @@ -657,7 +657,7 @@ def check_for_b005(self, node):

def check_for_b006_and_b008(self, node):
visitor = FunctionDefDefaultsVisitor(
B006, B008, self.b008_extend_immutable_calls
B006, B008, self.b008_b039_extend_immutable_calls
)
visitor.visit(node.args.defaults + node.args.kw_defaults)
self.errors.extend(visitor.errors)
Expand All @@ -681,7 +681,7 @@ def check_for_b039(self, node: ast.Call):
return

visitor = FunctionDefDefaultsVisitor(
B039, B039, self.b008_extend_immutable_calls
B039, B039, self.b008_b039_extend_immutable_calls
)
visitor.visit(kw.value)
self.errors.extend(visitor.errors)
Expand Down Expand Up @@ -1808,10 +1808,17 @@ def visit(self, node):


class FunctionDefDefaultsVisitor(ast.NodeVisitor):
"""Used by B006, B008, and B039. B039 is essentially B006+B008 but for ContextVar."""

def __init__(
self, error_code_calls, error_code_literals, b008_extend_immutable_calls=None
self,
error_code_calls, # B006 or B039
error_code_literals, # B008 or B039
b008_b039_extend_immutable_calls=None,
):
self.b008_extend_immutable_calls = b008_extend_immutable_calls or set()
self.b008_b039_extend_immutable_calls = (
b008_b039_extend_immutable_calls or set()
)
self.error_code_calls = error_code_calls
self.error_code_literals = error_code_literals
for node in B006.mutable_literals + B006.mutable_comprehensions:
Expand Down Expand Up @@ -1843,7 +1850,7 @@ def visit_Call(self, node):
self.generic_visit(node)
return

if call_path in B008.immutable_calls | self.b008_extend_immutable_calls:
if call_path in B008.immutable_calls | self.b008_b039_extend_immutable_calls:
self.generic_visit(node)
return

Expand Down Expand Up @@ -1959,6 +1966,8 @@ def visit_Lambda(self, node):
"between them."
)
)

# Note: these are also used by B039
B006.mutable_literals = ("Dict", "List", "Set")
B006.mutable_comprehensions = ("ListComp", "DictComp", "SetComp")
B006.mutable_calls = {
Expand Down Expand Up @@ -1989,6 +1998,8 @@ def visit_Lambda(self, node):
"use that variable as a default value."
)
)

# Note: these are also used by B039
B008.immutable_calls = {
"tuple",
"frozenset",
Expand Down

0 comments on commit 6fee565

Please sign in to comment.