-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 flake8-simplify SIM105 rule #1621
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
def foo(): | ||
pass | ||
|
||
try: | ||
foo() | ||
except ValueError: # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except (ValueError, OSError): # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except: # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except ValueError: | ||
print('foo') | ||
except OSError: | ||
pass | ||
|
||
try: | ||
foo() | ||
except ValueError: | ||
pass | ||
else: | ||
print('bar') |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub use bool_ops::{a_and_not_a, a_or_not_a, and_false, or_true}; | ||
pub use key_in_dict::{key_in_dict_compare, key_in_dict_for}; | ||
pub use use_contextlib_suppress::use_contextlib_suppress; | ||
pub use yoda_conditions::yoda_conditions; | ||
|
||
mod bool_ops; | ||
mod key_in_dict; | ||
mod use_contextlib_suppress; | ||
mod yoda_conditions; |
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,38 @@ | ||
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Stmt, StmtKind}; | ||
|
||
use crate::ast::helpers; | ||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
/// SIM105 | ||
pub fn use_contextlib_suppress( | ||
checker: &mut Checker, | ||
stmt: &Stmt, | ||
handlers: &[Excepthandler], | ||
orelse: &[Stmt], | ||
) { | ||
if handlers.len() != 1 || !orelse.is_empty() { | ||
return; | ||
} | ||
let handler = &handlers[0]; | ||
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node; | ||
if body.len() == 1 { | ||
if matches!(body[0].node, StmtKind::Pass) { | ||
let handler_names: Vec<_> = helpers::extract_handler_names(handlers) | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
let exception = if handler_names.is_empty() { | ||
"Exception".to_string() | ||
} else { | ||
handler_names.join(", ") | ||
}; | ||
let check = Check::new( | ||
CheckKind::UseContextlibSuppress(exception), | ||
Range::from_located(stmt), | ||
); | ||
checker.add_check(check); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.py.snap
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,35 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
UseContextlibSuppress: ValueError | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UseContextlibSuppress: "ValueError, OSError" | ||
location: | ||
row: 9 | ||
column: 0 | ||
end_location: | ||
row: 12 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UseContextlibSuppress: Exception | ||
location: | ||
row: 14 | ||
column: 0 | ||
end_location: | ||
row: 17 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This might be a hard one to autofix right now because it depends on
contextlib
being imported already.