-
Notifications
You must be signed in to change notification settings - Fork 109
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
B904: ensure the raise is in the same context with the except #191
Changes from 4 commits
f066f82
168480f
e37c285
e0f8f54
f5da337
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,19 @@ | |
__version__ = "21.9.2" | ||
|
||
LOG = logging.getLogger("flake8.bugbear") | ||
CONTEXTFUL_NODES = ( | ||
ast.Module, | ||
ast.ClassDef, | ||
ast.AsyncFunctionDef, | ||
ast.FunctionDef, | ||
ast.Lambda, | ||
ast.ListComp, | ||
ast.SetComp, | ||
ast.DictComp, | ||
ast.GeneratorExp, | ||
) | ||
|
||
Context = namedtuple("Context", ["node", "stack"]) | ||
|
||
|
||
@attr.s(hash=False) | ||
|
@@ -168,6 +181,7 @@ class BugBearVisitor(ast.NodeVisitor): | |
node_window = attr.ib(default=attr.Factory(list)) | ||
errors = attr.ib(default=attr.Factory(list)) | ||
futures = attr.ib(default=attr.Factory(set)) | ||
context = attr.ib(default=attr.Factory(list)) | ||
|
||
NODE_WINDOW_SIZE = 4 | ||
|
||
|
@@ -178,13 +192,30 @@ def __getattr__(self, name): | |
print(name) | ||
return self.__getattribute__(name) | ||
|
||
@property | ||
def node_stack(self): | ||
if len(self.contexts) == 0: | ||
return [] | ||
|
||
context, stack = self.contexts[-1] | ||
return stack | ||
|
||
def visit(self, node): | ||
is_contextful = isinstance(node, CONTEXTFUL_NODES) | ||
|
||
if is_contextful: | ||
context = Context(node, []) | ||
self.contexts.append(context) | ||
|
||
self.node_stack.append(node) | ||
self.node_window.append(node) | ||
self.node_window = self.node_window[-self.NODE_WINDOW_SIZE :] | ||
super().visit(node) | ||
self.node_stack.pop() | ||
|
||
if is_contextful: | ||
self.contexts.pop() | ||
|
||
def visit_ExceptHandler(self, node): | ||
if node.type is None: | ||
self.errors.append( | ||
|
@@ -500,9 +531,12 @@ def check_for_b901(self, node): | |
break | ||
|
||
def check_for_b902(self, node): | ||
if not isinstance(self.node_stack[-2], ast.ClassDef): | ||
if len(self.contexts) < 2 or not isinstance( | ||
self.contexts[-2].node, ast.ClassDef | ||
): | ||
return | ||
|
||
cls = self.contexts[-2].node | ||
decorators = NameFinder() | ||
decorators.visit(node.decorator_list) | ||
|
||
|
@@ -511,7 +545,7 @@ def check_for_b902(self, node): | |
# `cls`? | ||
return | ||
|
||
bases = {b.id for b in self.node_stack[-2].bases if isinstance(b, ast.Name)} | ||
bases = {b.id for b in cls.bases if isinstance(b, ast.Name)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleaner, but it's not really used anywhere else, but I do agree this makes it easier to know what it is, if you know what cls generally means ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
if "type" in bases: | ||
if ( | ||
"classmethod" in decorators.names | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this all that is needed? How did this ever work if so :|