Skip to content

Commit

Permalink
Integrate the new Jupyter AST nodes in Ruff (#6086)
Browse files Browse the repository at this point in the history
## Summary

This PR adds the implementation for the new Jupyter AST nodes i.e.,
`ExprLineMagic` and `StmtLineMagic`.

## Test Plan

Add test cases for `unparse` containing magic commands

resolves: #6087
  • Loading branch information
dhruvmanila committed Jul 26, 2023
1 parent 1fdadee commit 025fa4e
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 19 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 @@ -51,11 +51,11 @@ wsl = { version = "0.1.0" }
# v1.0.1
libcst = { git = "https://github.com/Instagram/LibCST.git", rev = "3cacca1a1029f05707e50703b49fe3dd860aa839", default-features = false }

ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "4d03b9b5b212fc869e4cfda151414438186a7779" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "4d03b9b5b212fc869e4cfda151414438186a7779" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "4d03b9b5b212fc869e4cfda151414438186a7779", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "4d03b9b5b212fc869e4cfda151414438186a7779", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "4d03b9b5b212fc869e4cfda151414438186a7779" , default-features = false, features = ["full-lexer", "num-bigint"] }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "5ef4ccd6322a8b7c5e8ead0ae3c89426d7be7d72" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "5ef4ccd6322a8b7c5e8ead0ae3c89426d7be7d72" , default-features = false, features = ["num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "5ef4ccd6322a8b7c5e8ead0ae3c89426d7be7d72", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "5ef4ccd6322a8b7c5e8ead0ae3c89426d7be7d72", default-features = false }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "5ef4ccd6322a8b7c5e8ead0ae3c89426d7be7d72" , default-features = false, features = ["full-lexer", "num-bigint"] }

[profile.release]
lto = "fat"
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff/src/rules/ruff/rules/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,13 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
| Expr::Await(_)
| Expr::Yield(_)
| Expr::YieldFrom(_) => self.unconditional_next_block(after),
Expr::LineMagic(_) => todo!(),
}
}
// The tough branches are done, here is an easy one.
Stmt::Return(_) => NextBlock::Terminate,
Stmt::TypeAlias(_) => todo!(),
Stmt::LineMagic(_) => todo!(),
};

// Include any statements in the block that don't divert the control flow.
Expand Down Expand Up @@ -922,6 +924,7 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
| Stmt::TryStar(_)
| Stmt::Assert(_) => true,
Stmt::TypeAlias(_) => todo!(),
Stmt::LineMagic(_) => todo!(),
}
}

Expand Down Expand Up @@ -957,6 +960,7 @@ fn is_control_flow_stmt(stmt: &Stmt) -> bool {
| Stmt::Break(_)
| Stmt::Continue(_) => true,
Stmt::TypeAlias(_) => todo!(),
Stmt::LineMagic(_) => todo!(),
}
}

Expand Down
30 changes: 30 additions & 0 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,12 @@ pub struct ExprSlice<'a> {
step: Option<Box<ComparableExpr<'a>>>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprLineMagic<'a> {
kind: ast::MagicKind,
value: &'a str,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparableExpr<'a> {
BoolOp(ExprBoolOp<'a>),
Expand Down Expand Up @@ -681,6 +687,7 @@ pub enum ComparableExpr<'a> {
List(ExprList<'a>),
Tuple(ExprTuple<'a>),
Slice(ExprSlice<'a>),
LineMagic(ExprLineMagic<'a>),
}

impl<'a> From<&'a Box<ast::Expr>> for Box<ComparableExpr<'a>> {
Expand Down Expand Up @@ -925,6 +932,14 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
upper: upper.as_ref().map(Into::into),
step: step.as_ref().map(Into::into),
}),
ast::Expr::LineMagic(ast::ExprLineMagic {
kind,
value,
range: _range,
}) => Self::LineMagic(ExprLineMagic {
kind: *kind,
value: value.as_str(),
}),
}
}
}
Expand Down Expand Up @@ -1155,6 +1170,12 @@ pub struct StmtExpr<'a> {
value: ComparableExpr<'a>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StmtLineMagic<'a> {
kind: ast::MagicKind,
value: &'a str,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparableStmt<'a> {
FunctionDef(StmtFunctionDef<'a>),
Expand All @@ -1181,6 +1202,7 @@ pub enum ComparableStmt<'a> {
ImportFrom(StmtImportFrom<'a>),
Global(StmtGlobal<'a>),
Nonlocal(StmtNonlocal<'a>),
LineMagic(StmtLineMagic<'a>),
Expr(StmtExpr<'a>),
Pass,
Break,
Expand Down Expand Up @@ -1440,6 +1462,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
}) => Self::Nonlocal(StmtNonlocal {
names: names.iter().map(ast::Identifier::as_str).collect(),
}),
ast::Stmt::LineMagic(ast::StmtLineMagic {
kind,
value,
range: _range,
}) => Self::LineMagic(StmtLineMagic {
kind: *kind,
value: value.as_str(),
}),
ast::Stmt::Expr(ast::StmtExpr {
value,
range: _range,
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ where
.map_or(false, |value| any_over_expr(value, func))
}
Expr::Name(_) | Expr::Constant(_) => false,
Expr::LineMagic(_) => false,
}
}

Expand Down Expand Up @@ -583,6 +584,7 @@ where
range: _range,
}) => any_over_expr(value, func),
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) => false,
Stmt::LineMagic(_) => false,
}
}

Expand Down
Loading

0 comments on commit 025fa4e

Please sign in to comment.