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

PyUpgrade: Turn errors into OSError #1434

Merged
merged 37 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
100bdd4
Base sutff added
colin99d Dec 28, 2022
f30cada
Laying more groundwork
colin99d Dec 28, 2022
28f677b
Removed blocking errors
colin99d Dec 28, 2022
78c7e7d
Progressing
colin99d Dec 28, 2022
7bc66d4
Finished merge
colin99d Dec 28, 2022
d34aa41
Basic idea works, for some reason it is WAY overreporting errors fixed
colin99d Dec 29, 2022
40ae04b
Got a lot more tests working
colin99d Dec 29, 2022
786e734
Added some test fixes
colin99d Dec 29, 2022
a04b761
Fixed one issue, for some reason showing test 62-65 twice
colin99d Dec 29, 2022
1c27ec7
The actual tests
colin99d Dec 29, 2022
f8e6b0f
Got everything in 24_0 working
colin99d Dec 29, 2022
4c521b7
Got everyrthing in 24_1 working
colin99d Dec 29, 2022
248bcbe
refactoted to allow for raise
colin99d Dec 29, 2022
e97db11
Got raise statements working
colin99d Dec 29, 2022
4320e97
Expanded what could be handled
colin99d Dec 29, 2022
42b74d8
Got raise working
colin99d Dec 29, 2022
674b4f0
Merge branch 'main' into osalias
colin99d Dec 29, 2022
3b976dd
Switched to Range::new
colin99d Dec 30, 2022
cdbdb2f
Merge branch 'main' into osalias
charliermarsh Dec 30, 2022
87ffcaa
Merge branch 'main' into osalias
charliermarsh Dec 30, 2022
594ebb2
Tweak error message
charliermarsh Dec 30, 2022
3526098
Switched to Expr
colin99d Dec 30, 2022
8f5d21d
Merge branch 'osalias' of https://github.com/colin99d/ruff into osalias
colin99d Dec 30, 2022
7a64309
Added fix
colin99d Dec 30, 2022
f0da50d
Simplifications that work
colin99d Dec 30, 2022
e7e13db
Added ignore
colin99d Dec 30, 2022
a48b977
Simplified
colin99d Dec 30, 2022
2a46ef6
Simplified
colin99d Dec 30, 2022
6d89fe6
Switched to Expr
colin99d Dec 30, 2022
1a394cb
Got fmt working
colin99d Dec 30, 2022
6680a4b
Got everything cleaned up
colin99d Dec 30, 2022
e1d6d48
Merge branch 'main' into osalias
colin99d Dec 30, 2022
4cf37cb
Got things working
colin99d Dec 30, 2022
3139530
Merge branch 'main' into osalias
colin99d Dec 30, 2022
a4b5d54
Merge branch 'main' into osalias
colin99d Dec 31, 2022
2813091
Merge branch 'main' into osalias
charliermarsh Dec 31, 2022
bdf8060
Fix clippy
charliermarsh Dec 31, 2022
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
Prev Previous commit
Next Next commit
Expanded what could be handled
  • Loading branch information
colin99d committed Dec 29, 2022
commit 4320e973affd0ab129afef3292dbefe615e1b8b1
7 changes: 6 additions & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,9 @@ where
}
}
if self.settings.enabled.contains(&CheckCode::UP024) {
pyupgrade::plugins::os_error_alias(self, exc);
if let Some(item) = exc {
pyupgrade::plugins::os_error_alias(self, item);
}
}
}
StmtKind::AugAssign { target, .. } => {
Expand Down Expand Up @@ -1688,6 +1690,9 @@ where
if self.settings.enabled.contains(&CheckCode::UP022) {
pyupgrade::plugins::replace_stdout_stderr(self, expr, keywords);
}
if self.settings.enabled.contains(&CheckCode::UP024) {
pyupgrade::plugins::os_error_alias(self, expr);
}

// flake8-print
if self.settings.enabled.contains(&CheckCode::T201)
Expand Down
60 changes: 48 additions & 12 deletions src/pyupgrade/plugins/os_error_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,8 @@ fn handle_making_changes(checker: &mut Checker, target: &Located<ExprKind>, befo
// ignored. Let me know if you want me to go with a more
// complicated solution that avoids this.
if contents.contains(").") {
println!("GOT TRIGGERED");
return;
}
println!("Before: {:?}", before_replace);
println!("Replacements: {:?}\n", replacements);
let mut final_str: String;
if replacements.len() == 1 {
final_str = replacements.get(0).unwrap().to_string();
Expand Down Expand Up @@ -159,29 +156,68 @@ impl OSErrorAliasChecker for &Vec<Excepthandler> {
}
}

impl OSErrorAliasChecker for &Option<Box<Located<ExprKind>>> {
impl OSErrorAliasChecker for &Box<Located<ExprKind>> {
fn check_error(&self, checker: &mut Checker) {
let target = match self {
Some(expr) => expr,
None => return,
};
let mut replacements: Vec<String>;
let mut before_replace: Vec<String>;
match &target.node {
match &self.node {
ExprKind::Name { id, .. } => {
(replacements, before_replace) = check_module(checker, &target);
(replacements, before_replace) = check_module(checker, &self);
if replacements.is_empty() {
let new_name = get_correct_name(&id);
replacements.push(new_name);
before_replace.push(id.to_string());
}
}
ExprKind::Attribute { .. } => {
(replacements, before_replace) = check_module(checker, &target);
(replacements, before_replace) = check_module(checker, &self);
},
_ => return
}
handle_making_changes(checker, target, before_replace, replacements);
handle_making_changes(checker, self, before_replace, replacements);
}
}

impl OSErrorAliasChecker for &Located<ExprKind> {
fn check_error(&self, checker: &mut Checker) {
let mut replacements: Vec<String>;
let mut before_replace: Vec<String>;
let change_target: &Located<ExprKind>;
match &self.node {
ExprKind::Name { id, .. } => {
change_target = &self;
(replacements, before_replace) = check_module(checker, &self);
if replacements.is_empty() {
let new_name = get_correct_name(&id);
replacements.push(new_name);
before_replace.push(id.to_string());
}
}
ExprKind::Attribute { .. } => {
change_target = &self;
(replacements, before_replace) = check_module(checker, &self);
},
ExprKind::Call { func, args, keywords } => {
change_target = &func;
match &func.node {
ExprKind::Name { id, .. } => {
(replacements, before_replace) = check_module(checker, &func);
if replacements.is_empty() {
let new_name = get_correct_name(&id);
replacements.push(new_name);
before_replace.push(id.to_string());
}
}
ExprKind::Attribute { .. } => {
(replacements, before_replace) = check_module(checker, &func);
},
_ => return
}
println!("{:?}", func);
}
_ => return
}
colin99d marked this conversation as resolved.
Show resolved Hide resolved
handle_making_changes(checker, change_target, before_replace, replacements);
}
}

Expand Down