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] Implement import-replacement rule (UP035) #2049

Merged
merged 50 commits into from
Jan 31, 2023
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
cf9155f
Basic groundwork laid
colin99d Jan 20, 2023
6b5c4aa
Going to WIPE libcst
colin99d Jan 21, 2023
04ac882
Fixed conversion
colin99d Jan 21, 2023
9ac1642
Need to switch
colin99d Jan 21, 2023
34b28fc
Merged
colin99d Jan 21, 2023
ac38462
Better indentation tests
colin99d Jan 21, 2023
1c1d391
Fixed second from indentation issue
colin99d Jan 21, 2023
7035102
Fixed fmt and nightly
colin99d Jan 21, 2023
51f6d5c
Added a bunch more things to replace
colin99d Jan 21, 2023
1980519
Added some fixes
colin99d Jan 21, 2023
1fbd14c
Fixed a bug in formatting
colin99d Jan 21, 2023
b7016fa
Fixed formatting error
colin99d Jan 21, 2023
0448418
Fixed formatting errors
colin99d Jan 21, 2023
3bf08dd
Got base tests going
colin99d Jan 21, 2023
b0d2456
Got the mods in
colin99d Jan 21, 2023
860a1d1
Finished up a rough draft, still has the libcst bug
colin99d Jan 21, 2023
4a8263a
Fixed some formatting
colin99d Jan 21, 2023
08c2174
Fixed clippy
colin99d Jan 21, 2023
8e98545
Merge branch 'main' into ImportReplacements
colin99d Jan 22, 2023
6b4d68a
STARTED TRANSITION FROM LIBCST TO CUSTOM
colin99d Jan 22, 2023
30a7180
An MVP with no known bugs
colin99d Jan 22, 2023
f811a84
Added more tests for six.moves
colin99d Jan 22, 2023
b326a29
Handled one last issue
colin99d Jan 22, 2023
af1af04
Fixed some more stuff
colin99d Jan 22, 2023
e8a55a7
Added some fixes
colin99d Jan 22, 2023
679a134
Fixed clippy
colin99d Jan 22, 2023
fd8f418
Deleted unused snapshots
colin99d Jan 22, 2023
ac95e2c
Fixed typos
colin99d Jan 22, 2023
e9bd812
Fixed typos
colin99d Jan 22, 2023
943585c
Added some tests
colin99d Jan 23, 2023
42ae4c5
Fixed tests
colin99d Jan 23, 2023
50c85ac
Merge branch 'main' into ImportReplacements
colin99d Jan 26, 2023
04cd251
Recommended changes
colin99d Jan 26, 2023
65cc26a
Merge branch 'main' into ImportReplacements
colin99d Jan 27, 2023
9d67f07
Merge branch 'main' into ImportReplacements
charliermarsh Jan 29, 2023
603c10b
Rewrite parts of the check
charliermarsh Jan 30, 2023
f7cbd58
Removed six stuff
colin99d Jan 30, 2023
880ebca
Merge branch 'ImportReplacements' of https://github.com/colin99d/ruff…
colin99d Jan 30, 2023
91c38b6
Merge branch 'main' into ImportReplacements
colin99d Jan 30, 2023
0c0a098
removed six
colin99d Jan 30, 2023
3d820f0
Merge branch 'ImportReplacements' of https://github.com/colin99d/ruff…
colin99d Jan 30, 2023
fd53580
Merge branch 'main' into ImportReplacements
charliermarsh Jan 30, 2023
c071895
Merge branch 'charlie/import-replacements' into ImportReplacements
charliermarsh Jan 30, 2023
ef4d532
Small refactors; add one failing test
charliermarsh Jan 30, 2023
2af94db
Avoid bad multi-line fix
charliermarsh Jan 30, 2023
cd7ea96
Merge branch 'main' into ImportReplacements
charliermarsh Jan 30, 2023
1cb84fc
Always flag
charliermarsh Jan 30, 2023
53d6b1f
Flag multi
charliermarsh Jan 30, 2023
dad535f
Use pyupgrade-like token based removal
charliermarsh Jan 30, 2023
f17c2ff
Merge branch 'main' into ImportReplacements
charliermarsh Jan 30, 2023
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
Fixed conversion
  • Loading branch information
colin99d committed Jan 21, 2023
commit 04ac882c4496a8091809e3d38fb6fe28c93d7613
149 changes: 65 additions & 84 deletions src/rules/pyupgrade/rules/import_replacements.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::cst::matchers::{match_import, match_import_from, match_module};
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::violations;
use libcst_native::{Codegen, CodegenState, ImportAlias, ImportNames, NameOrAttribute};
use rustpython_ast::{AliasData, Located, Stmt, StmtKind};

type Aliases<'a> = Vec<ImportAlias<'a>>;

const BAD_MODULES: &[&str] = &[
"collections",
"pipes",
Expand Down Expand Up @@ -49,97 +45,76 @@ const COLLECTIONS_TO_ABC: &[&str] = &[
"ValuesView",
];

/// Fixes the new list of imports to have correct formatting
fn correct_formatting<'a>(original: &Aliases<'a>, new: &mut Aliases<'a>) {
// If the new list is empty, there is nothing to format
if new.is_empty() {
return;

struct FixImports<'a> {
module: &'a str,
multi_line: bool,
names: &'a [AliasData],
indent: &'a str,
}

impl<'a> FixImports<'a> {
fn new(module: &'a str, multi_line: bool, names: &'a [AliasData], indent: &'a str) -> Self {
Self {
module,
multi_line,
names,
indent,
}
}
// If there were less than two items in the original, there is nothing to format
if original.len() < 2 {
return;

fn check_replacement(&self) -> Option<String> {
match self.module {
"collections" => self.create_new_str(COLLECTIONS_TO_ABC, "collections.abc"),
_ => return None,
}
}
let first = original.first().unwrap();
let middle = original.get(1).unwrap();
let last = original.last().unwrap();

let mut i = 0;
for mut name in new {
if i == 0 {
name.comma = first.comma.clone();
/// Converts the string of imports into new one
fn create_new_str(&self, matches: &[&str], replace: &str) -> Option<String> {
let (matching_names, unmatching_names) = self.get_import_lists(matches);
let unmatching = self.get_str(&unmatching_names, self.module);
let matching = self.get_str(&matching_names, replace);
if !unmatching.is_empty() && !matching.is_empty() {
Some(format!("{unmatching}\n{matching}"))
} else if !unmatching.is_empty() {
Some(unmatching)
} else if !matching.is_empty() {
Some(matching)
} else {
None
}
i += 1;
}
}

/// Returns a list of imports that does and does not have a match in the given list of matches
fn get_import_lists<'a>(names: &ImportNames<'a>, matches: &[&str]) -> (Aliases<'a>, Aliases<'a>) {
let mut unmatching_names: Aliases<'a> = vec![];
let mut matching_names: Aliases<'a> = vec![];
/// Returns a list of imports that does and does not have a match in the given list of matches
fn get_import_lists(&self, matches: &[&str]) -> (Vec<AliasData>, Vec<AliasData>) {
let mut unmatching_names: Vec<AliasData> = vec![];
let mut matching_names: Vec<AliasData> = vec![];

if let ImportNames::Aliases(names) = names {
for name in names {
if let NameOrAttribute::N(sub_item) = &name.name {
if matches.contains(&sub_item.value) {
matching_names.push(name.clone());
} else {
unmatching_names.push(name.clone());
}
for name in self.names {
if matches.contains(&name.name.as_str()) {
matching_names.push(name.clone());
} else {
unmatching_names.push(name.clone());
}
}
correct_formatting(names, &mut matching_names);
correct_formatting(names, &mut unmatching_names);
(matching_names, unmatching_names)
}
(matching_names, unmatching_names)
}

/// Converts the string of imports into new one
fn create_new_str(original: &str, matches: &[&str], replace: &str) -> Option<String> {
let mut tree = match_module(original).unwrap();
let old_import = match_import_from(&mut tree).unwrap();
println!("{:?}", old_import);
let (matching_names, unmatching_names) = get_import_lists(&old_import.names, matches);
let unmatching = if unmatching_names.is_empty() {
"".to_string()
} else {
let mut unmatching_imports = old_import.clone();
unmatching_imports.names = ImportNames::Aliases(unmatching_names);
let mut state = CodegenState::default();
unmatching_imports.codegen(&mut state);
state.to_string()
};
let matching = if matching_names.is_empty() {
"".to_string()
} else {
let mut matching_imports = old_import.clone();
if let Some(NameOrAttribute::N(name)) = &matching_imports.module {
println!("{:?}", name);
let mut new_name = name.clone();
new_name.value = replace;
matching_imports.module = Some(NameOrAttribute::N(new_name));
} else {
return None
fn get_str(&self, names: &[AliasData], module: &str) -> String {
if names.is_empty() {
return String::new();
}
matching_imports.names = ImportNames::Aliases(matching_names);
let mut state = CodegenState::default();
matching_imports.codegen(&mut state);
state.to_string()
};
if !unmatching.is_empty() && !matching.is_empty() {
Some(format!("{unmatching}\n{matching}"))
} else if !unmatching.is_empty() {
Some(unmatching)
} else if !matching.is_empty() {
Some(matching)
} else {
None
}
}

fn check_repalcement(module: &str, original: &str) -> Option<String> {
match module {
"collections" => create_new_str(original, COLLECTIONS_TO_ABC, "collections.abc"),
_ => return None,
let mut full_names: Vec<String> = vec![];
for name in names {
let asname_str = match &name.asname {
Some(item) => format!(" as {}", item),
None => String::new(),
};
let final_string = format!("{}{}", name.name, asname_str);
full_names.push(final_string);
}
format!("from {} import {}", module, full_names.join(", "))
}
}

Expand All @@ -158,9 +133,15 @@ pub fn import_replacements(
if !BAD_MODULES.contains(&clean_mod.as_str()) {
return;
}
let mut clean_names: Vec<AliasData> = vec![];
for name in names {
clean_names.push(name.node.clone());
}
let module_text = checker
.locator
.slice_source_code_range(&Range::from_located(stmt));
let clean_result = check_repalcement(clean_mod, &module_text);
let is_mulit_line = module_text.contains('\n');
let fixer = FixImports::new(clean_mod, is_mulit_line, &clean_names, "");
let clean_result = fixer.check_replacement();
println!("{:?}", clean_result);
}