Upgrade RustPython (#4747)

This commit is contained in:
Micha Reiser 2023-05-31 10:26:35 +02:00 committed by GitHub
parent 06bcb85f81
commit 6c1ff6a85f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 104 additions and 93 deletions

View file

@ -1596,8 +1596,9 @@ mod tests {
use anyhow::Result;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser as parser;
use rustpython_ast::Suite;
use rustpython_parser::ast::Cmpop;
use rustpython_parser::Parse;
use crate::helpers::{
elif_else_range, else_range, first_colon_range, has_trailing_content, identifier_range,
@ -1608,25 +1609,25 @@ mod tests {
#[test]
fn trailing_content() -> 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!(!has_trailing_content(stmt, &locator));
let contents = "x = 1; y = 2";
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert!(has_trailing_content(stmt, &locator));
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!(!has_trailing_content(stmt, &locator));
let contents = "x = 1 # Comment";
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
assert!(!has_trailing_content(stmt, &locator));
@ -1636,7 +1637,7 @@ x = 1
y = 2
"#
.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!(!has_trailing_content(stmt, &locator));
@ -1647,7 +1648,7 @@ y = 2
#[test]
fn extract_identifier_range() -> Result<()> {
let contents = "def f(): pass".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!(
@ -1661,7 +1662,7 @@ def \
pass
"#
.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!(
@ -1670,7 +1671,7 @@ def \
);
let contents = "class Class(): pass".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!(
@ -1679,7 +1680,7 @@ def \
);
let contents = "class Class: pass".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!(
@ -1693,7 +1694,7 @@ class Class():
pass
"#
.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!(
@ -1702,7 +1703,7 @@ class Class():
);
let contents = r#"x = 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!(
@ -1759,7 +1760,7 @@ else:
pass
"#
.trim();
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
let range = else_range(stmt, &locator).unwrap();
@ -1793,7 +1794,7 @@ elif b:
...
"
.trim_start();
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
let range = elif_else_range(stmt, &locator).unwrap();
@ -1807,7 +1808,7 @@ else:
...
"
.trim_start();
let program = parser::parse_program(contents, "<filename>")?;
let program = Suite::parse(contents, "<filename>")?;
let stmt = program.first().unwrap();
let locator = Locator::new(contents);
let range = elif_else_range(stmt, &locator).unwrap();

View file

@ -1456,7 +1456,8 @@ impl<'a> Generator<'a> {
#[cfg(test)]
mod tests {
use rustpython_parser as parser;
use rustpython_ast::Suite;
use rustpython_parser::Parse;
use crate::newlines::LineEnding;
use crate::source_code::stylist::{Indentation, Quote};
@ -1466,7 +1467,7 @@ mod tests {
let indentation = Indentation::default();
let quote = Quote::default();
let line_ending = LineEnding::default();
let program = parser::parse_program(contents, "<filename>").unwrap();
let program = Suite::parse(contents, "<filename>").unwrap();
let stmt = program.first().unwrap();
let mut generator = Generator::new(&indentation, quote, line_ending);
generator.unparse_stmt(stmt);
@ -1479,7 +1480,7 @@ mod tests {
line_ending: LineEnding,
contents: &str,
) -> String {
let program = parser::parse_program(contents, "<filename>").unwrap();
let program = Suite::parse(contents, "<filename>").unwrap();
let stmt = program.first().unwrap();
let mut generator = Generator::new(indentation, quote, line_ending);
generator.unparse_stmt(stmt);

View file

@ -3,8 +3,7 @@ use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser as parser;
use rustpython_parser::{lexer, Mode, ParseError};
use rustpython_parser::{ast, lexer, Mode, Parse, ParseError};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -26,7 +25,7 @@ mod stylist;
/// Run round-trip source code generation on a given Python code.
pub fn round_trip(code: &str, source_path: &str) -> Result<String, ParseError> {
let locator = Locator::new(code);
let python_ast = parser::parse_program(code, source_path)?;
let python_ast = ast::Suite::parse(code, source_path)?;
let tokens: Vec<_> = lexer::lex(code, Mode::Module).collect();
let stylist = Stylist::from_tokens(&tokens, &locator);
let mut generator: Generator = (&stylist).into();

View file

@ -1,7 +1,7 @@
use anyhow::Result;
use ruff_text_size::{TextLen, TextRange};
use rustpython_parser as parser;
use rustpython_parser::ast::Expr;
use rustpython_parser::Parse;
use crate::relocate::relocate_expr;
use crate::source_code::Locator;
@ -35,7 +35,7 @@ pub fn parse_type_annotation(
// isn't the case, e.g., for implicit concatenations, or for annotations that contain
// escaped quotes.
let leading_quote = str::leading_quote(expression).unwrap();
let expr = parser::parse_expression_starts_at(
let expr = Expr::parse_starts_at(
value,
"<filename>",
range.start() + leading_quote.text_len(),
@ -43,7 +43,7 @@ pub fn parse_type_annotation(
Ok((expr, AnnotationKind::Simple))
} else {
// Otherwise, consider this a "complex" annotation.
let mut expr = parser::parse_expression(value, "<filename>")?;
let mut expr = Expr::parse(value, "<filename>")?;
relocate_expr(&mut expr, range);
Ok((expr, AnnotationKind::Complex))
}