-
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
Implement B036 check for except BaseException
without re-raising
#438
Changes from 1 commit
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 |
---|---|---|
|
@@ -407,6 +407,12 @@ def visit_ExceptHandler(self, node): | |
maybe_error = _check_redundant_excepthandlers(names, node) | ||
if maybe_error is not None: | ||
self.errors.append(maybe_error) | ||
if "BaseException" in names and not any( | ||
isinstance(subnode, ast.Raise) and subnode.exc is None | ||
for subnode in node.body | ||
): | ||
self.errors.append(B036(node.lineno, node.col_offset)) | ||
|
||
self.generic_visit(node) | ||
|
||
def visit_UAdd(self, node): | ||
|
@@ -668,7 +674,7 @@ def check_for_b017(self, node): | |
and isinstance(item_context.func.value, ast.Name) | ||
and item_context.func.value.id == "pytest" | ||
and "match" | ||
not in [kwd.arg for kwd in item_context.keywords] | ||
not in (kwd.arg for kwd in item_context.keywords) | ||
) | ||
) | ||
) | ||
|
@@ -677,7 +683,7 @@ def check_for_b017(self, node): | |
and item_context.func.id == "raises" | ||
and isinstance(item_context.func.ctx, ast.Load) | ||
and "pytest.raises" in self._b005_imports | ||
and "match" not in [kwd.arg for kwd in item_context.keywords] | ||
and "match" not in (kwd.arg for kwd in item_context.keywords) | ||
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. Just making tuples since they don't need to be immutable? 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. Actually not a tuple, just parenthesizing the generator:
Just a tiny efficiency improvement, rather than having iterate the generator to construct a list, then iterate through the list to find the thing... just iterate the generator directly |
||
) | ||
) | ||
and len(item_context.args) == 1 | ||
|
@@ -1953,6 +1959,9 @@ def visit_Lambda(self, node): | |
) | ||
B035 = Error(message="B035 Static key in dict comprehension {!r}.") | ||
|
||
B036 = Error( | ||
message="B036 Don't except `BaseException` unless you plan to re-raise it." | ||
) | ||
|
||
# Warnings disabled by default. | ||
B901 = Error( | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||||||||||||||||
|
||||||||||||||||||||||
try: | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
except BaseException: # bad | ||||||||||||||||||||||
print("aaa") | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
Comment on lines
+2
to
+6
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. Can we add and make this work easily? If not, no worries.
Suggested change
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. Look like bare except already caught by both flake8 and bugbear: try:
pass
except:
pass gives both:
Should this be consolidated with B001, rather than a new error? 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. Hmm - that's a good question. I'd like that, but I wonder if that would upset people? I vote yes, but lets see if any other contributors / maintainers weigh in here ... 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 guess keeping separate also gives you the flexibility to enable/disable either one? Maybe that's the safer option? 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. Yeah ok, I like this now the more I think about it. |
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
try: | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
except BaseException as ex: # bad | ||||||||||||||||||||||
print(ex) | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
try: | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
except ValueError: | ||||||||||||||||||||||
raise | ||||||||||||||||||||||
except BaseException: # bad | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
try: | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
except BaseException: # ok - reraised | ||||||||||||||||||||||
print("aaa") | ||||||||||||||||||||||
raise | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
try: | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
except BaseException as ex: # bad - raised something else | ||||||||||||||||||||||
print("aaa") | ||||||||||||||||||||||
raise KeyError from ex |
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.
Do we also catch a bare
except
here and error?