Skip to content

Commit d1f0700

Browse files
authored
Rename Notebook related symbols (#6862)
This PR renames the following symbols: * `PySourceType::Jupyter` -> `PySourceType::Ipynb` * `SourceKind::Jupyter` -> `SourceKind::IpyNotebook` * `JupyterIndex` -> `NotebookIndex`
1 parent 61b2ffa commit d1f0700

File tree

11 files changed

+41
-41
lines changed

11 files changed

+41
-41
lines changed

crates/ruff/src/jupyter/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/// When we lint a jupyter notebook, we have to translate the row/column based on
44
/// [`ruff_text_size::TextSize`] to jupyter notebook cell/row/column.
55
#[derive(Clone, Debug, Eq, PartialEq)]
6-
pub struct JupyterIndex {
6+
pub struct NotebookIndex {
77
/// Enter a row (1-based), get back the cell (1-based)
88
pub(super) row_to_cell: Vec<u32>,
99
/// Enter a row (1-based), get back the row in cell (1-based)
1010
pub(super) row_to_row_in_cell: Vec<u32>,
1111
}
1212

13-
impl JupyterIndex {
13+
impl NotebookIndex {
1414
/// Returns the cell number (1-based) for the given row (1-based).
1515
pub fn cell(&self, row: usize) -> Option<u32> {
1616
self.row_to_cell.get(row).copied()

crates/ruff/src/jupyter/notebook.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ruff_source_file::{NewlineWithTrailingNewline, UniversalNewlineIterator};
1717
use ruff_text_size::{TextRange, TextSize};
1818

1919
use crate::autofix::source_map::{SourceMap, SourceMarker};
20-
use crate::jupyter::index::JupyterIndex;
20+
use crate::jupyter::index::NotebookIndex;
2121
use crate::jupyter::schema::{Cell, RawNotebook, SortAlphabetically, SourceValue};
2222
use crate::rules::pycodestyle::rules::SyntaxError;
2323
use crate::IOError;
@@ -82,8 +82,8 @@ impl Cell {
8282
Cell::Code(cell) => &cell.source,
8383
_ => return false,
8484
};
85-
// Ignore cells containing cell magic. This is different from line magic
86-
// which is allowed and ignored by the parser.
85+
// Ignore cells containing cell magic as they act on the entire cell
86+
// as compared to line magic which acts on a single line.
8787
!match source {
8888
SourceValue::String(string) => string
8989
.lines()
@@ -106,7 +106,7 @@ pub struct Notebook {
106106
source_code: String,
107107
/// The index of the notebook. This is used to map between the concatenated
108108
/// source code and the original notebook.
109-
index: OnceCell<JupyterIndex>,
109+
index: OnceCell<NotebookIndex>,
110110
/// The raw notebook i.e., the deserialized version of JSON string.
111111
raw: RawNotebook,
112112
/// The offsets of each cell in the concatenated source code. This includes
@@ -368,7 +368,7 @@ impl Notebook {
368368
///
369369
/// The index building is expensive as it needs to go through the content of
370370
/// every valid code cell.
371-
fn build_index(&self) -> JupyterIndex {
371+
fn build_index(&self) -> NotebookIndex {
372372
let mut row_to_cell = vec![0];
373373
let mut row_to_row_in_cell = vec![0];
374374

@@ -395,7 +395,7 @@ impl Notebook {
395395
row_to_row_in_cell.extend(1..=line_count);
396396
}
397397

398-
JupyterIndex {
398+
NotebookIndex {
399399
row_to_cell,
400400
row_to_row_in_cell,
401401
}
@@ -413,7 +413,7 @@ impl Notebook {
413413
/// The index is built only once when required. This is only used to
414414
/// report diagnostics, so by that time all of the autofixes must have
415415
/// been applied if `--fix` was passed.
416-
pub(crate) fn index(&self) -> &JupyterIndex {
416+
pub(crate) fn index(&self) -> &NotebookIndex {
417417
self.index.get_or_init(|| self.build_index())
418418
}
419419

@@ -473,7 +473,7 @@ mod tests {
473473
use anyhow::Result;
474474
use test_case::test_case;
475475

476-
use crate::jupyter::index::JupyterIndex;
476+
use crate::jupyter::index::NotebookIndex;
477477
use crate::jupyter::schema::Cell;
478478
use crate::jupyter::Notebook;
479479
use crate::registry::Rule;
@@ -561,7 +561,7 @@ print("after empty cells")
561561
);
562562
assert_eq!(
563563
notebook.index(),
564-
&JupyterIndex {
564+
&NotebookIndex {
565565
row_to_cell: vec![0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 7, 7, 8],
566566
row_to_row_in_cell: vec![0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 1, 1, 2, 1],
567567
}
@@ -666,13 +666,13 @@ print("after empty cells")
666666
fn test_no_cell_id() -> Result<()> {
667667
let path = "no_cell_id.ipynb".to_string();
668668
let source_notebook = read_jupyter_notebook(path.as_ref())?;
669-
let source_kind = SourceKind::Jupyter(source_notebook);
669+
let source_kind = SourceKind::IpyNotebook(source_notebook);
670670
let (_, transformed) = test_contents(
671671
&source_kind,
672672
path.as_ref(),
673673
&settings::Settings::for_rule(Rule::UnusedImport),
674674
);
675-
let linted_notebook = transformed.into_owned().expect_jupyter();
675+
let linted_notebook = transformed.into_owned().expect_ipy_notebook();
676676
let mut writer = Vec::new();
677677
linted_notebook.write_inner(&mut writer)?;
678678
let actual = String::from_utf8(writer)?;

crates/ruff/src/linter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn check_path(
147147
match ruff_python_parser::parse_program_tokens(
148148
tokens,
149149
&path.to_string_lossy(),
150-
source_type.is_jupyter(),
150+
source_type.is_ipynb(),
151151
) {
152152
Ok(python_ast) => {
153153
if use_ast {

crates/ruff/src/message/grouped.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use colored::Colorize;
77
use ruff_source_file::OneIndexed;
88

99
use crate::fs::relativize_path;
10-
use crate::jupyter::{JupyterIndex, Notebook};
10+
use crate::jupyter::{Notebook, NotebookIndex};
1111
use crate::message::diff::calculate_print_width;
1212
use crate::message::text::{MessageCodeFrame, RuleCodeAndBody};
1313
use crate::message::{
@@ -92,7 +92,7 @@ struct DisplayGroupedMessage<'a> {
9292
show_source: bool,
9393
row_length: NonZeroUsize,
9494
column_length: NonZeroUsize,
95-
jupyter_index: Option<&'a JupyterIndex>,
95+
jupyter_index: Option<&'a NotebookIndex>,
9696
}
9797

9898
impl Display for DisplayGroupedMessage<'_> {

crates/ruff/src/message/text.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ruff_source_file::{OneIndexed, SourceLocation};
1111
use ruff_text_size::{TextRange, TextSize};
1212

1313
use crate::fs::relativize_path;
14-
use crate::jupyter::{JupyterIndex, Notebook};
14+
use crate::jupyter::{Notebook, NotebookIndex};
1515
use crate::line_width::{LineWidth, TabSize};
1616
use crate::message::diff::Diff;
1717
use crate::message::{Emitter, EmitterContext, Message};
@@ -161,7 +161,7 @@ impl Display for RuleCodeAndBody<'_> {
161161

162162
pub(super) struct MessageCodeFrame<'a> {
163163
pub(crate) message: &'a Message,
164-
pub(crate) jupyter_index: Option<&'a JupyterIndex>,
164+
pub(crate) jupyter_index: Option<&'a NotebookIndex>,
165165
}
166166

167167
impl Display for MessageCodeFrame<'_> {

crates/ruff/src/rules/pyflakes/rules/yield_outside_function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub(crate) fn yield_outside_function(checker: &mut Checker, expr: &Expr) {
7070
// `await` is allowed at the top level of a Jupyter notebook.
7171
// See: https://ipython.readthedocs.io/en/stable/interactive/autoawait.html.
7272
if scope.kind.is_module()
73-
&& checker.source_type.is_jupyter()
73+
&& checker.source_type.is_ipynb()
7474
&& keyword == DeferralKeyword::Await
7575
{
7676
return;

crates/ruff/src/source_kind.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::jupyter::Notebook;
44
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
55
pub enum SourceKind {
66
Python(String),
7-
Jupyter(Notebook),
7+
IpyNotebook(Notebook),
88
}
99

1010
impl SourceKind {
11-
/// Return the [`Notebook`] if the source kind is [`SourceKind::Jupyter`].
11+
/// Return the [`Notebook`] if the source kind is [`SourceKind::IpyNotebook`].
1212
pub fn notebook(&self) -> Option<&Notebook> {
13-
if let Self::Jupyter(notebook) = self {
13+
if let Self::IpyNotebook(notebook) = self {
1414
Some(notebook)
1515
} else {
1616
None
@@ -20,10 +20,10 @@ impl SourceKind {
2020
#[must_use]
2121
pub(crate) fn updated(&self, new_source: String, source_map: &SourceMap) -> Self {
2222
match self {
23-
SourceKind::Jupyter(notebook) => {
23+
SourceKind::IpyNotebook(notebook) => {
2424
let mut cloned = notebook.clone();
2525
cloned.update(source_map, new_source);
26-
SourceKind::Jupyter(cloned)
26+
SourceKind::IpyNotebook(cloned)
2727
}
2828
SourceKind::Python(_) => SourceKind::Python(new_source),
2929
}
@@ -32,7 +32,7 @@ impl SourceKind {
3232
pub fn source_code(&self) -> &str {
3333
match self {
3434
SourceKind::Python(source) => source,
35-
SourceKind::Jupyter(notebook) => notebook.source_code(),
35+
SourceKind::IpyNotebook(notebook) => notebook.source_code(),
3636
}
3737
}
3838
}

crates/ruff/src/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ pub(crate) fn test_notebook_path(
6969
) -> Result<TestedNotebook> {
7070
let source_notebook = read_jupyter_notebook(path.as_ref())?;
7171

72-
let source_kind = SourceKind::Jupyter(source_notebook);
72+
let source_kind = SourceKind::IpyNotebook(source_notebook);
7373
let (messages, transformed) = test_contents(&source_kind, path.as_ref(), settings);
7474
let expected_notebook = read_jupyter_notebook(expected.as_ref())?;
75-
let linted_notebook = transformed.into_owned().expect_jupyter();
75+
let linted_notebook = transformed.into_owned().expect_ipy_notebook();
7676

7777
assert_eq!(
7878
linted_notebook.cell_offsets(),
@@ -86,7 +86,7 @@ pub(crate) fn test_notebook_path(
8686

8787
Ok(TestedNotebook {
8888
messages,
89-
source_notebook: source_kind.expect_jupyter(),
89+
source_notebook: source_kind.expect_ipy_notebook(),
9090
linted_notebook,
9191
})
9292
}

crates/ruff_cli/src/diagnostics.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub(crate) fn lint_path(
293293
SourceKind::Python(transformed) => {
294294
write(path, transformed.as_bytes())?;
295295
}
296-
SourceKind::Jupyter(notebook) => {
296+
SourceKind::IpyNotebook(notebook) => {
297297
notebook.write(path)?;
298298
}
299299
},
@@ -308,10 +308,10 @@ pub(crate) fn lint_path(
308308
stdout.write_all(b"\n")?;
309309
stdout.flush()?;
310310
}
311-
SourceKind::Jupyter(dest_notebook) => {
311+
SourceKind::IpyNotebook(dest_notebook) => {
312312
// We need to load the notebook again, since we might've
313313
// mutated it.
314-
let src_notebook = source_kind.as_jupyter().unwrap();
314+
let src_notebook = source_kind.as_ipy_notebook().unwrap();
315315
let mut stdout = io::stdout().lock();
316316
for ((idx, src_cell), dest_cell) in src_notebook
317317
.cells()
@@ -409,7 +409,7 @@ pub(crate) fn lint_path(
409409
);
410410
}
411411

412-
let notebooks = if let SourceKind::Jupyter(notebook) = source_kind {
412+
let notebooks = if let SourceKind::IpyNotebook(notebook) = source_kind {
413413
FxHashMap::from_iter([(
414414
path.to_str()
415415
.ok_or_else(|| anyhow!("Unable to parse filename: {:?}", path))?
@@ -567,9 +567,9 @@ impl LintSources {
567567
let source_type = PySourceType::from(path);
568568

569569
// Read the file from disk.
570-
if source_type.is_jupyter() {
570+
if source_type.is_ipynb() {
571571
let notebook = notebook_from_path(path).map_err(SourceExtractionError::Diagnostics)?;
572-
let source_kind = SourceKind::Jupyter(notebook);
572+
let source_kind = SourceKind::IpyNotebook(notebook);
573573
Ok(LintSources {
574574
source_type,
575575
source_kind,
@@ -593,10 +593,10 @@ impl LintSources {
593593
) -> Result<LintSources, SourceExtractionError> {
594594
let source_type = path.map(PySourceType::from).unwrap_or_default();
595595

596-
if source_type.is_jupyter() {
596+
if source_type.is_ipynb() {
597597
let notebook = notebook_from_source_code(&source_code, path)
598598
.map_err(SourceExtractionError::Diagnostics)?;
599-
let source_kind = SourceKind::Jupyter(notebook);
599+
let source_kind = SourceKind::IpyNotebook(notebook);
600600
Ok(LintSources {
601601
source_type,
602602
source_kind,

crates/ruff_python_ast/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum PySourceType {
5858
#[default]
5959
Python,
6060
Stub,
61-
Jupyter,
61+
Ipynb,
6262
}
6363

6464
impl PySourceType {
@@ -70,16 +70,16 @@ impl PySourceType {
7070
matches!(self, PySourceType::Stub)
7171
}
7272

73-
pub const fn is_jupyter(&self) -> bool {
74-
matches!(self, PySourceType::Jupyter)
73+
pub const fn is_ipynb(&self) -> bool {
74+
matches!(self, PySourceType::Ipynb)
7575
}
7676
}
7777

7878
impl From<&Path> for PySourceType {
7979
fn from(path: &Path) -> Self {
8080
match path.extension() {
8181
Some(ext) if ext == "pyi" => PySourceType::Stub,
82-
Some(ext) if ext == "ipynb" => PySourceType::Jupyter,
82+
Some(ext) if ext == "ipynb" => PySourceType::Ipynb,
8383
_ => PySourceType::Python,
8484
}
8585
}

crates/ruff_python_parser/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl AsMode for PySourceType {
313313
fn as_mode(&self) -> Mode {
314314
match self {
315315
PySourceType::Python | PySourceType::Stub => Mode::Module,
316-
PySourceType::Jupyter => Mode::Jupyter,
316+
PySourceType::Ipynb => Mode::Jupyter,
317317
}
318318
}
319319
}

0 commit comments

Comments
 (0)