Skip to content

Commit 6a64f22

Browse files
authored
Rename Magic* to IpyEscape* (#6395)
## Summary This PR renames the `MagicCommand` token to `IpyEscapeCommand` token and `MagicKind` to `IpyEscapeKind` type to better reflect the purpose of the token and type. Similarly, it renames the AST nodes from `LineMagic` to `IpyEscapeCommand` prefixed with `Stmt`/`Expr` wherever necessary. It also makes renames from using `jupyter_magic` to `ipython_escape_commands` in various function names. The mode value is still `Mode::Jupyter` because the escape commands are part of the IPython syntax but the lexing/parsing is done for a Jupyter notebook. ### Motivation behind the rename: * IPython codebase defines it as "EscapeCommand" / "Escape Sequences": * Escape Sequences: https://github.com/ipython/ipython/blob/292e3a23459ca965b8c1bfe2c3707044c510209a/IPython/core/inputtransformer2.py#L329-L333 * Escape command: https://github.com/ipython/ipython/blob/292e3a23459ca965b8c1bfe2c3707044c510209a/IPython/core/inputtransformer2.py#L410-L411 * The word "magic" is used mainly for the actual magic commands i.e., the ones starting with `%`/`%%` (https://ipython.readthedocs.io/en/stable/interactive/reference.html#magic-command-system). So, this avoids any confusion between the Magic token (`%`, `%%`) and the escape command itself. ## Test Plan * `cargo test` to make sure all renames are done correctly. * `grep` for `jupyter_escape`/`magic` to make sure all renames are done correctly.
1 parent 3bf1c66 commit 6a64f22

26 files changed

+867
-864
lines changed

crates/ruff/src/jupyter/notebook.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,11 @@ print("after empty cells")
571571
}
572572

573573
#[test]
574-
fn test_line_magics() -> Result<()> {
575-
let path = "line_magics.ipynb".to_string();
574+
fn test_ipy_escape_command() -> Result<()> {
575+
let path = "ipy_escape_command.ipynb".to_string();
576576
let (diagnostics, source_kind, _) = test_notebook_path(
577577
&path,
578-
Path::new("line_magics_expected.ipynb"),
578+
Path::new("ipy_escape_command_expected.ipynb"),
579579
&settings::Settings::for_rule(Rule::UnusedImport),
580580
)?;
581581
assert_messages!(diagnostics, path, source_kind);

crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__line_magics.snap renamed to crates/ruff/src/jupyter/snapshots/ruff__jupyter__notebook__tests__ipy_escape_command.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff/src/jupyter/notebook.rs
33
---
4-
line_magics.ipynb:cell 1:5:8: F401 [*] `os` imported but unused
4+
ipy_escape_command.ipynb:cell 1:5:8: F401 [*] `os` imported but unused
55
|
66
3 | %matplotlib inline
77
4 |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,13 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
653653
| Expr::Await(_)
654654
| Expr::Yield(_)
655655
| Expr::YieldFrom(_) => self.unconditional_next_block(after),
656-
Expr::LineMagic(_) => todo!(),
656+
Expr::IpyEscapeCommand(_) => todo!(),
657657
}
658658
}
659659
// The tough branches are done, here is an easy one.
660660
Stmt::Return(_) => NextBlock::Terminate,
661661
Stmt::TypeAlias(_) => todo!(),
662-
Stmt::LineMagic(_) => todo!(),
662+
Stmt::IpyEscapeCommand(_) => todo!(),
663663
};
664664

665665
// Include any statements in the block that don't divert the control flow.
@@ -903,7 +903,7 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
903903
| Stmt::TryStar(_)
904904
| Stmt::Assert(_) => true,
905905
Stmt::TypeAlias(_) => todo!(),
906-
Stmt::LineMagic(_) => todo!(),
906+
Stmt::IpyEscapeCommand(_) => todo!(),
907907
}
908908
}
909909

@@ -936,7 +936,7 @@ fn is_control_flow_stmt(stmt: &Stmt) -> bool {
936936
| Stmt::Break(_)
937937
| Stmt::Continue(_) => true,
938938
Stmt::TypeAlias(_) => todo!(),
939-
Stmt::LineMagic(_) => todo!(),
939+
Stmt::IpyEscapeCommand(_) => todo!(),
940940
}
941941
}
942942

crates/ruff_python_ast/src/comparable.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,8 @@ pub struct ExprSlice<'a> {
672672
}
673673

674674
#[derive(Debug, PartialEq, Eq, Hash)]
675-
pub struct ExprLineMagic<'a> {
676-
kind: ast::MagicKind,
675+
pub struct ExprIpyEscapeCommand<'a> {
676+
kind: ast::IpyEscapeKind,
677677
value: &'a str,
678678
}
679679

@@ -706,7 +706,7 @@ pub enum ComparableExpr<'a> {
706706
List(ExprList<'a>),
707707
Tuple(ExprTuple<'a>),
708708
Slice(ExprSlice<'a>),
709-
LineMagic(ExprLineMagic<'a>),
709+
IpyEscapeCommand(ExprIpyEscapeCommand<'a>),
710710
}
711711

712712
impl<'a> From<&'a Box<ast::Expr>> for Box<ComparableExpr<'a>> {
@@ -936,11 +936,11 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
936936
upper: upper.as_ref().map(Into::into),
937937
step: step.as_ref().map(Into::into),
938938
}),
939-
ast::Expr::LineMagic(ast::ExprLineMagic {
939+
ast::Expr::IpyEscapeCommand(ast::ExprIpyEscapeCommand {
940940
kind,
941941
value,
942942
range: _,
943-
}) => Self::LineMagic(ExprLineMagic {
943+
}) => Self::IpyEscapeCommand(ExprIpyEscapeCommand {
944944
kind: *kind,
945945
value: value.as_str(),
946946
}),
@@ -1165,8 +1165,8 @@ pub struct StmtExpr<'a> {
11651165
}
11661166

11671167
#[derive(Debug, PartialEq, Eq, Hash)]
1168-
pub struct StmtLineMagic<'a> {
1169-
kind: ast::MagicKind,
1168+
pub struct StmtIpyEscapeCommand<'a> {
1169+
kind: ast::IpyEscapeKind,
11701170
value: &'a str,
11711171
}
11721172

@@ -1193,7 +1193,7 @@ pub enum ComparableStmt<'a> {
11931193
ImportFrom(StmtImportFrom<'a>),
11941194
Global(StmtGlobal<'a>),
11951195
Nonlocal(StmtNonlocal<'a>),
1196-
LineMagic(StmtLineMagic<'a>),
1196+
IpyEscapeCommand(StmtIpyEscapeCommand<'a>),
11971197
Expr(StmtExpr<'a>),
11981198
Pass,
11991199
Break,
@@ -1394,11 +1394,11 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
13941394
names: names.iter().map(ast::Identifier::as_str).collect(),
13951395
})
13961396
}
1397-
ast::Stmt::LineMagic(ast::StmtLineMagic {
1397+
ast::Stmt::IpyEscapeCommand(ast::StmtIpyEscapeCommand {
13981398
kind,
13991399
value,
14001400
range: _,
1401-
}) => Self::LineMagic(StmtLineMagic {
1401+
}) => Self::IpyEscapeCommand(StmtIpyEscapeCommand {
14021402
kind: *kind,
14031403
value: value.as_str(),
14041404
}),

crates/ruff_python_ast/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ where
108108
| Expr::Subscript(_)
109109
| Expr::Yield(_)
110110
| Expr::YieldFrom(_)
111-
| Expr::LineMagic(_)
111+
| Expr::IpyEscapeCommand(_)
112112
)
113113
})
114114
}
@@ -247,7 +247,7 @@ where
247247
.is_some_and(|value| any_over_expr(value, func))
248248
}
249249
Expr::Name(_) | Expr::Constant(_) => false,
250-
Expr::LineMagic(_) => false,
250+
Expr::IpyEscapeCommand(_) => false,
251251
}
252252
}
253253

@@ -534,7 +534,7 @@ where
534534
Stmt::Nonlocal(_) => false,
535535
Stmt::Expr(ast::StmtExpr { value, range: _ }) => any_over_expr(value, func),
536536
Stmt::Pass(_) | Stmt::Break(_) | Stmt::Continue(_) => false,
537-
Stmt::LineMagic(_) => false,
537+
Stmt::IpyEscapeCommand(_) => false,
538538
}
539539
}
540540

0 commit comments

Comments
 (0)