Skip to content

Commit 00c27b8

Browse files
committed
Implementation for new Jupyter Magic AST nodes
1 parent 51d8fc1 commit 00c27b8

File tree

8 files changed

+178
-3
lines changed

8 files changed

+178
-3
lines changed

crates/ruff/src/rules/ruff/rules/unreachable.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,13 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
671671
| Expr::Await(_)
672672
| Expr::Yield(_)
673673
| Expr::YieldFrom(_) => self.unconditional_next_block(after),
674+
Expr::LineMagic(_) => todo!(),
674675
}
675676
}
676677
// The tough branches are done, here is an easy one.
677678
Stmt::Return(_) => NextBlock::Terminate,
678679
Stmt::TypeAlias(_) => todo!(),
680+
Stmt::LineMagic(_) => todo!(),
679681
};
680682

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

@@ -957,6 +960,7 @@ fn is_control_flow_stmt(stmt: &Stmt) -> bool {
957960
| Stmt::Break(_)
958961
| Stmt::Continue(_) => true,
959962
Stmt::TypeAlias(_) => todo!(),
963+
Stmt::LineMagic(_) => todo!(),
960964
}
961965
}
962966

crates/ruff_python_ast/src/comparable.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ pub struct ExprSlice<'a> {
652652
step: Option<Box<ComparableExpr<'a>>>,
653653
}
654654

655+
#[derive(Debug, PartialEq, Eq, Hash)]
656+
pub struct ExprLineMagic<'a> {
657+
kind: ast::MagicKind,
658+
value: &'a str,
659+
}
660+
655661
#[derive(Debug, PartialEq, Eq, Hash)]
656662
pub enum ComparableExpr<'a> {
657663
BoolOp(ExprBoolOp<'a>),
@@ -681,6 +687,7 @@ pub enum ComparableExpr<'a> {
681687
List(ExprList<'a>),
682688
Tuple(ExprTuple<'a>),
683689
Slice(ExprSlice<'a>),
690+
LineMagic(ExprLineMagic<'a>),
684691
}
685692

686693
impl<'a> From<&'a Box<ast::Expr>> for Box<ComparableExpr<'a>> {
@@ -925,6 +932,14 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
925932
upper: upper.as_ref().map(Into::into),
926933
step: step.as_ref().map(Into::into),
927934
}),
935+
ast::Expr::LineMagic(ast::ExprLineMagic {
936+
kind,
937+
value,
938+
range: _range,
939+
}) => Self::LineMagic(ExprLineMagic {
940+
kind: *kind,
941+
value: value.as_str(),
942+
}),
928943
}
929944
}
930945
}
@@ -1155,6 +1170,12 @@ pub struct StmtExpr<'a> {
11551170
value: ComparableExpr<'a>,
11561171
}
11571172

1173+
#[derive(Debug, PartialEq, Eq, Hash)]
1174+
pub struct StmtLineMagic<'a> {
1175+
kind: ast::MagicKind,
1176+
value: &'a str,
1177+
}
1178+
11581179
#[derive(Debug, PartialEq, Eq, Hash)]
11591180
pub enum ComparableStmt<'a> {
11601181
FunctionDef(StmtFunctionDef<'a>),
@@ -1181,6 +1202,7 @@ pub enum ComparableStmt<'a> {
11811202
ImportFrom(StmtImportFrom<'a>),
11821203
Global(StmtGlobal<'a>),
11831204
Nonlocal(StmtNonlocal<'a>),
1205+
LineMagic(StmtLineMagic<'a>),
11841206
Expr(StmtExpr<'a>),
11851207
Pass,
11861208
Break,
@@ -1440,6 +1462,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
14401462
}) => Self::Nonlocal(StmtNonlocal {
14411463
names: names.iter().map(ast::Identifier::as_str).collect(),
14421464
}),
1465+
ast::Stmt::LineMagic(ast::StmtLineMagic {
1466+
kind,
1467+
value,
1468+
range: _range,
1469+
}) => Self::LineMagic(StmtLineMagic {
1470+
kind: *kind,
1471+
value: value.as_str(),
1472+
}),
14431473
ast::Stmt::Expr(ast::StmtExpr {
14441474
value,
14451475
range: _range,

crates/ruff_python_ast/src/helpers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ where
262262
.map_or(false, |value| any_over_expr(value, func))
263263
}
264264
Expr::Name(_) | Expr::Constant(_) => false,
265+
Expr::LineMagic(_) => false,
265266
}
266267
}
267268

@@ -564,6 +565,7 @@ where
564565
range: _range,
565566
}) => any_over_expr(value, func),
566567
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) => false,
568+
Stmt::LineMagic(_) => false,
567569
}
568570
}
569571

0 commit comments

Comments
 (0)