Skip to content

Commit 950a8d5

Browse files
committed
Update to ruff_python_parser 0.11
1 parent a6b4ef7 commit 950a8d5

13 files changed

Lines changed: 83 additions & 74 deletions

File tree

Cargo.lock

Lines changed: 40 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ rustpython-stdlib = { path = "stdlib", default-features = false, version = "0.4.
122122
rustpython-sre_engine = { path = "vm/sre_engine", version = "0.4.0" }
123123
rustpython-doc = { git = "https://github.com/RustPython/__doc__", tag = "0.3.0", version = "0.3.0" }
124124

125-
ruff_python_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "v0.4.10" }
126-
ruff_python_ast = { git = "https://github.com/astral-sh/ruff.git", tag = "v0.4.10" }
127-
ruff_text_size = { git = "https://github.com/astral-sh/ruff.git", tag = "v0.4.10" }
128-
ruff_source_file = { git = "https://github.com/astral-sh/ruff.git", tag = "v0.4.10" }
129-
ruff_python_codegen = { git = "https://github.com/astral-sh/ruff.git", tag = "v0.4.10" }
125+
ruff_python_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
126+
ruff_python_ast = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
127+
ruff_text_size = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
128+
ruff_source_file = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
129+
ruff_python_codegen = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
130130
# rustpython-literal = { version = "0.4.0" }
131131
# rustpython-parser-core = { version = "0.4.0" }
132132
# rustpython-parser = { version = "0.4.0" }

Lib/test/test_grammar.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,8 +1619,6 @@ def test_nested_front():
16191619
self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
16201620
('Macdonalds', 'Cheeseburger')])
16211621

1622-
# TODO: RUSTPYTHON
1623-
@unittest.expectedFailure
16241622
def test_genexps(self):
16251623
# generator expression tests
16261624
g = ([x for x in range(10)] for x in range(1))

compiler/codegen/src/compile.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,8 +2029,7 @@ impl Compiler<'_> {
20292029
if self.future_annotations {
20302030
// FIXME: codegen?
20312031
let ident = Default::default();
2032-
let codegen =
2033-
ruff_python_codegen::Generator::new(&ident, Default::default(), Default::default());
2032+
let codegen = ruff_python_codegen::Generator::new(&ident, Default::default());
20342033
self.emit_load_const(ConstantData::Str {
20352034
value: codegen.expr(annotation),
20362035
});
@@ -3595,18 +3594,19 @@ impl ToU32 for usize {
35953594
#[cfg(test)]
35963595
mod tests {
35973596
use super::*;
3597+
use ruff_python_ast::name::Name;
35983598
use ruff_python_ast::*;
35993599

36003600
/// Test if the compiler can correctly identify fstrings containing an `await` expression.
36013601
#[test]
36023602
fn test_fstring_contains_await() {
36033603
let range = TextRange::default();
3604-
let flags = FStringFlags::default();
3604+
let flags = FStringFlags::empty();
36053605

36063606
// f'{x}'
36073607
let expr_x = Expr::Name(ExprName {
36083608
range,
3609-
id: "x".to_owned(),
3609+
id: Name::new("x"),
36103610
ctx: ExprContext::Load,
36113611
});
36123612
let not_present = &Expr::FString(ExprFString {
@@ -3631,7 +3631,7 @@ mod tests {
36313631
range,
36323632
value: Box::new(Expr::Name(ExprName {
36333633
range,
3634-
id: "x".to_owned(),
3634+
id: Name::new("x"),
36353635
ctx: ExprContext::Load,
36363636
})),
36373637
});
@@ -3655,14 +3655,14 @@ mod tests {
36553655
// f'{x:{await y}}'
36563656
let expr_x = Expr::Name(ExprName {
36573657
range,
3658-
id: "x".to_owned(),
3658+
id: Name::new("x"),
36593659
ctx: ExprContext::Load,
36603660
});
36613661
let expr_await_y = Expr::Await(ExprAwait {
36623662
range,
36633663
value: Box::new(Expr::Name(ExprName {
36643664
range,
3665-
id: "y".to_owned(),
3665+
id: Name::new("y"),
36663666
ctx: ExprContext::Load,
36673667
})),
36683668
});

compiler/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn _compile(
119119
mode: Mode,
120120
opts: CompileOpts,
121121
) -> Result<CodeObject, CompileError> {
122-
let parsed = parser::parse(source_code.text, mode.into())
122+
let parsed = parser::parse(source_code.text, parser::Mode::from(mode).into())
123123
.map_err(|err| CompileError::from_ruff_parse_error(err, &source_code))?;
124124
let ast = parsed.into_syntax();
125125
compile::compile_top(ast, source_code, mode, opts).map_err(|e| e.into())
@@ -145,9 +145,8 @@ pub fn _compile_symtable(
145145
symboltable::SymbolTable::scan_program(&ast.into_syntax(), source_code.clone())
146146
}
147147
Mode::Eval => {
148-
let ast =
149-
ruff_python_parser::parse(source_code.text, ruff_python_parser::Mode::Expression)
150-
.map_err(|e| CompileError::from_ruff_parse_error(e, &source_code))?;
148+
let ast = ruff_python_parser::parse(source_code.text, parser::Mode::Expression.into())
149+
.map_err(|e| CompileError::from_ruff_parse_error(e, &source_code))?;
151150
symboltable::SymbolTable::scan_expr(
152151
&ast.into_syntax().expect_expression(),
153152
source_code.clone(),

src/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod helper;
22

33
use rustpython_compiler::{
4-
CompileError, ParseError, parser::ParseErrorType, parser::lexer::LexicalErrorType,
4+
CompileError, ParseError, parser::LexicalErrorType, parser::ParseErrorType,
55
};
66
use rustpython_vm::{
77
AsObject, PyResult, VirtualMachine,

vm/src/stdlib/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub(crate) fn parse(
242242
mode: parser::Mode,
243243
) -> Result<PyObjectRef, CompileError> {
244244
let source_code = SourceCodeOwned::new("".to_owned(), source.to_owned());
245-
let top = parser::parse(source, mode)
245+
let top = parser::parse(source, mode.into())
246246
.map_err(|parse_error| ParseError {
247247
error: parse_error.error,
248248
location: text_range_to_source_range(&source_code, parse_error.location)

vm/src/stdlib/ast/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
255255
value: ruff::StringLiteralValue::single(ruff::StringLiteral {
256256
range,
257257
value,
258-
flags: ruff::StringLiteralFlags::default().with_prefix(prefix),
258+
flags: ruff::StringLiteralFlags::empty().with_prefix(prefix),
259259
}),
260260
})
261261
}
@@ -265,7 +265,7 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
265265
value: ruff::BytesLiteralValue::single(ruff::BytesLiteral {
266266
range,
267267
value,
268-
flags: Default::default(), // TODO
268+
flags: ruff::BytesLiteralFlags::empty(), // TODO
269269
}),
270270
})
271271
}
@@ -293,7 +293,7 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
293293
// idk lol
294294
func: Box::new(ruff::Expr::Name(ruff::ExprName {
295295
range: TextRange::default(),
296-
id: "frozenset".to_owned(),
296+
id: ruff::name::Name::new_static("frozenset"),
297297
ctx: ruff::ExprContext::Load,
298298
})),
299299
arguments: ruff::Arguments {

vm/src/stdlib/ast/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ impl Node for ruff::ExprName {
977977
object: PyObjectRef,
978978
) -> PyResult<Self> {
979979
Ok(Self {
980-
id: get_node_field(vm, &object, "id", "Name")?.try_into_value(vm)?,
980+
id: Node::ast_from_object(vm, source_code, get_node_field(vm, &object, "id", "Name")?)?,
981981
ctx: Node::ast_from_object(
982982
vm,
983983
source_code,

vm/src/stdlib/ast/other.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@ impl Node for ruff::ConversionFlag {
1919
}
2020

2121
// /// This is just a string, not strictly an AST node. But it makes AST conversions easier.
22-
// impl Node for ruff::name::Name {
23-
// fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
24-
// vm.ctx.new_str(self.as_str()).to_pyobject(vm)
25-
// }
22+
impl Node for ruff::name::Name {
23+
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
24+
vm.ctx.new_str(self.as_str()).to_pyobject(vm)
25+
}
2626

27-
// fn ast_from_object(vm: &VirtualMachine, source_code: &SourceCodeOwned, object: PyObjectRef) -> PyResult<Self> {
28-
// match object.downcast::<PyStr>() {
29-
// Ok(name) => Ok(Self::new(name)),
30-
// Err(_) => Err(vm.new_value_error("expected str for name".to_owned())),
31-
// }
32-
// }
33-
// }
27+
fn ast_from_object(
28+
vm: &VirtualMachine,
29+
_source_code: &SourceCodeOwned,
30+
object: PyObjectRef,
31+
) -> PyResult<Self> {
32+
match object.downcast::<PyStr>() {
33+
Ok(name) => Ok(Self::new(name)),
34+
Err(_) => Err(vm.new_value_error("expected str for name".to_owned())),
35+
}
36+
}
37+
}
3438

3539
impl Node for ruff::Decorator {
3640
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {

0 commit comments

Comments
 (0)