Skip to content

Commit

Permalink
Upgrade RustPython (#4747)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored May 31, 2023
1 parent 06bcb85 commit 6c1ff6a
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 93 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ proc-macro2 = { version = "1.0.51" }
quote = { version = "1.0.23" }
regex = { version = "1.7.1" }
rustc-hash = { version = "1.1.0" }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65", default-features = false, features = ["all-nodes-with-ranges"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65" }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "335780aeeac1e6fcd85994ba001d7b8ce99fcf65", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd", default-features = false, features = ["all-nodes-with-ranges"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd" }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "7a3eedbf6fb4ea7068a1bf7fe0e97e963ea95ffd", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
schemars = { version = "0.8.12" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93", features = ["preserve_order"] }
Expand Down
11 changes: 6 additions & 5 deletions crates/ruff/src/autofix/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ pub(crate) fn remove_argument(
mod tests {
use anyhow::Result;
use ruff_text_size::TextSize;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;

use ruff_python_ast::source_code::Locator;

Expand All @@ -433,19 +434,19 @@ mod tests {
#[test]
fn find_semicolon() -> Result<()> {
let contents = "x = 1";
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert_eq!(trailing_semicolon(stmt, &locator), None);

let contents = "x = 1; y = 1";
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(5)));

let contents = "x = 1 ; y = 1";
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(6)));
Expand All @@ -455,7 +456,7 @@ x = 1 \
; y = 1
"#
.trim();
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert_eq!(trailing_semicolon(stmt, &locator), Some(TextSize::from(10)));
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/importer/insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,17 @@ fn match_docstring_end(body: &[Stmt]) -> Option<TextSize> {
mod tests {
use anyhow::Result;
use ruff_text_size::TextSize;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::lexer::LexResult;
use rustpython_parser::Parse;

use ruff_python_ast::newlines::LineEnding;
use ruff_python_ast::source_code::{Locator, Stylist};

use super::Insertion;

fn insert(contents: &str) -> Result<Insertion> {
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let tokens: Vec<LexResult> = ruff_rustpython::tokenize(contents);
let locator = Locator::new(contents);
let stylist = Stylist::from_tokens(&tokens, &locator);
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/rules/eradicate/detection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// See: [eradicate.py](https://github.com/myint/eradicate/blob/98f199940979c94447a461d50d27862b118b282d/eradicate.py)
use once_cell::sync::Lazy;
use regex::Regex;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;

static ALLOWLIST_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
Expand Down Expand Up @@ -78,7 +79,7 @@ pub(crate) fn comment_contains_code(line: &str, task_tags: &[String]) -> bool {
}

// Finally, compile the source code.
parser::parse_program(&line, "<filename>").is_ok()
Suite::parse(&line, "<filename>").is_ok()
}

/// Returns `true` if a line is probably part of some multiline code.
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/isort/rules/add_required_imports.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::error;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser as parser;
use rustpython_parser::ast::{self, Stmt, Suite};
use rustpython_parser::Parse;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -140,7 +140,7 @@ pub(crate) fn add_required_imports(
.required_imports
.iter()
.flat_map(|required_import| {
let Ok(body) = parser::parse_program(required_import, "<filename>") else {
let Ok(body) = Suite::parse(required_import, "<filename>") else {
error!("Failed to parse required import: `{}`", required_import);
return vec![];
};
Expand Down
37 changes: 19 additions & 18 deletions crates/ruff/src/rules/mccabe/rules/function_is_too_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ pub(crate) fn function_is_too_complex(
#[cfg(test)]
mod tests {
use anyhow::Result;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;

use super::get_complexity_number;

Expand All @@ -172,7 +173,7 @@ mod tests {
def trivial():
pass
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand All @@ -183,7 +184,7 @@ def trivial():
def expr_as_statement():
0xF00D
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand All @@ -196,7 +197,7 @@ def sequential(n):
s = k + n
return s
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand All @@ -212,7 +213,7 @@ def if_elif_else_dead_path(n):
else:
return "smaller than or equal to three"
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 3);
Ok(())
}
Expand All @@ -229,7 +230,7 @@ def nested_ifs():
else:
return "smaller than or equal to three"
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 3);
Ok(())
}
Expand All @@ -241,7 +242,7 @@ def for_loop():
for i in range(10):
print(i)
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
Expand All @@ -255,7 +256,7 @@ def for_else(mylist):
else:
print(None)
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
Expand All @@ -269,7 +270,7 @@ def recursive(n):
else:
return n
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
Expand All @@ -286,7 +287,7 @@ def nested_functions():
a()
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 3);
Ok(())
}
Expand All @@ -304,7 +305,7 @@ def try_else():
else:
print(4)
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 4);
Ok(())
}
Expand All @@ -321,7 +322,7 @@ def nested_try_finally():
finally:
print(3)
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand All @@ -338,7 +339,7 @@ async def foobar(a, b, c):
async for x in a:
pass
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 3);
Ok(())
}
Expand All @@ -349,7 +350,7 @@ async def foobar(a, b, c):
def annotated_assign():
x: Any = None
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand Down Expand Up @@ -385,7 +386,7 @@ class Class:
return ServiceProvider(Logger())
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 9);
Ok(())
}
Expand All @@ -399,7 +400,7 @@ def process_detect_lines():
finally:
pass
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}
Expand All @@ -414,7 +415,7 @@ def process_detect_lines():
if res:
errors.append(f"Non-zero exit code {res}")
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
Expand All @@ -427,7 +428,7 @@ def with_lock():
if foo:
print('bar')
"#;
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff/src/rules/pylint/rules/too_many_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ pub(crate) fn too_many_branches(
#[cfg(test)]
mod tests {
use anyhow::Result;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;

use super::num_branches;

fn test_helper(source: &str, expected_num_branches: usize) -> Result<()> {
let branches = parser::parse_program(source, "<filename>")?;
let branches = Suite::parse(source, "<filename>")?;
assert_eq!(num_branches(&branches), expected_num_branches);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ pub(crate) fn too_many_return_statements(
#[cfg(test)]
mod tests {
use anyhow::Result;
use rustpython_parser as parser;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;

use super::num_returns;

fn test_helper(source: &str, expected: usize) -> Result<()> {
let stmts = parser::parse_program(source, "<filename>")?;
let stmts = Suite::parse(source, "<filename>")?;
assert_eq!(num_returns(&stmts), expected);
Ok(())
}
Expand Down
Loading

0 comments on commit 6c1ff6a

Please sign in to comment.